Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    how to check if a non-existing key in nvs ?

    Scheduled Pinned Locked Moved Discussion
    dictkey valuenvsnvsget
    13 Posts 7 Posters 909 Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • philwilkinsonP Offline
      philwilkinson
      last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • J Offline
        johand
        last edited by

        You have to check if it is None or not, change to:

        if pycom.nvs_get("count") != None:

        P 2 Replies Last reply Reply Quote 0
        • philwilkinsonP Offline
          philwilkinson
          last edited by

          it returns the same error

          ValueError: No matching object for the provided key
          
          J 1 Reply Last reply Reply Quote 0
          • J Offline
            johand @philwilkinson
            last edited by

            @philwilkinson Any reference to a line? I just tried your code and it works fine for me.

            1 Reply Last reply Reply Quote 0
            • philwilkinsonP Offline
              philwilkinson
              last edited by

              I was just using the REPL and it returned the error when I called nvs_get('count').

              J 1 Reply Last reply Reply Quote 0
              • J Offline
                johand @philwilkinson
                last edited by

                @philwilkinson No problem when I try, are you using an old firmware?

                1 Reply Last reply Reply Quote 0
                • philwilkinsonP Offline
                  philwilkinson
                  last edited by philwilkinson

                  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
                  >>>
                  
                  1 Reply Last reply Reply Quote 0
                  • P Offline
                    PeterB @johand
                    last edited by PeterB

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      PeterB @johand
                      last edited by

                      @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.

                      C 1 Reply Last reply Reply Quote 0
                      • C Offline
                        crumble @PeterB
                        last edited by

                        @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
                        
                        John BairdJ 1 Reply Last reply Reply Quote 0
                        • John BairdJ Offline
                          John Baird @crumble
                          last edited by

                          @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.

                          1 Reply Last reply Reply Quote 1
                          • T Offline
                            thibault
                            last edited by

                            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!

                            1 Reply Last reply Reply Quote 0
                            • M Offline
                              Michael Baker
                              last edited by

                              import pycom
                              the_value = pycom.nvs_get('the_key',None)
                              if the_value:
                                  print(the_value)
                              
                              1 Reply Last reply Reply Quote 0

                              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
                              • 1 / 1
                              • First post
                                Last post

                              Pycom on Twitter

                              Pycom