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
-
import pycom the_value = pycom.nvs_get('the_key',None) if the_value: print(the_value)
-
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 _value
I'm not sure if that is the python way, but maybe that can help you!
-
@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.
-
@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
-
@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.
-
This post is deleted!
-
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 key
so 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 >>>
-
@philwilkinson No problem when I try, are you using an old firmware?
-
I was just using the REPL and it returned the error when I called nvs_get('count').
-
@philwilkinson Any reference to a line? I just tried your code and it works fine for me.
-
it returns the same 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: