how to check if a non-existing key in nvs ?
-
How can I check if a key value pair dict is saved in NVS?
def car_count(): if pycom.nvs_get('count'): pycom.nvs_set('count', (pycom.nvs_get('count')+1)) print('{} cars have passed so far this period'.format(pycom.nvs_get('count'))) else:#needed following initial start up pycom.nvs_set('count', 1) print('first car passed this way')according to the docs if a non-existing key is given the returned value will be None. However I get an error
ValueError: No matching object for the provided key -
You have to check if it is None or not, change to:
if pycom.nvs_get("count") != None:
-
it returns the same error
ValueError: No matching object for the provided key -
@philwilkinson Any reference to a line? I just tried your code and it works fine for me.
-
I was just using the REPL and it returned the error when I called nvs_get('count').
-
@philwilkinson No problem when I try, are you using an old firmware?
-
It is a bug. I have raised issue #306 on Github
On the latest firmware
Pycom MicroPython 1.18.2.r7 [v1.8.6-849-df9f237] on 2019-05-14; LoPy4 with ESP32>>> import pycom >>> pycom.nvs_get('anything')==None ValueError: No matching object for the provided keyso I used a previous version of the firmware
Pycom MicroPython 1.18.2.r2 [v1.8.6-849-5cf02dc] on 2019-03-07; LoPy4 with ESP32>>>import pycom >>>pycom.nvs_get('anything')==None True >>> -
This post is deleted! -
@johand said in how to check if a non-existing key in nvs ?:
You have to check if it is None or not, change to:
if pycom.nvs_get("count") != None:
Are you sure that will work? An exception is an exception and "pycom.nvs_get("count")" would not return anything to compare with. The exception would be generated before the compare can be executed.
-
@PeterB said in how to check if a non-existing key in nvs ?:
@johand said in how to check if a non-existing key in nvs ?:
You have to check if it is None or not, change to:
if pycom.nvs_get("count") != None:
Are you sure that will work? An exception is an exception and "pycom.nvs_get("count")" would not return anything to compare with. The exception would be generated before the compare can be executed.
Depends on the version. This must have changed over time.
You can simply use a variable and wrap the call, so that it shall run on all versions:try: _wake = pycom.nvs_get('wake'); except Exception as e: _wake = None if _wake==None: _wake = 103 -
@crumble I just ran into this today. Yes, I agree you can wrap the call and catch the exception - however, the current documentation states "If a non-existing key is given the returned value will be None." (See: https://docs.pycom.io/firmwareapi/pycom/pycom/). The general catching of unknown exceptions isn't good practice. While I think your workaround is sound, I'd upvote this as a bug that needs to be fixed.
-
in our code we use a such function to read memory:
def memory_get(name='MIR', default_value=1): ''' Retourne une valeur par défaut si la mémoire NVS ne contient pas l'élément recherché ''' try: _value = pycom.nvs_get(name) except Exception: _value = None if _value is None: _value = default_value return _valueI'm not sure if that is the python way, but maybe that can help you!
-
import pycom the_value = pycom.nvs_get('the_key',None) if the_value: print(the_value)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login