AppDaemon get_state() in initialize()

Generically, I have an appdaemon App with a single state variable.

Specifically, it’s “app.vacuum” and can be either “dirty” or “clean”. When the vacuum starts, it’s changed to “dirty” and when the vacuum is empty, “clean”.

My initialize(self): method calls self.get_state("app.vacuum") and if it returns None, sets the state to clean. I observe that on restart of HA, the state always reads as None even if there was a state set before. Thus, restarts cause the state to flip from “dirty” to “clean”.

Is there a better way to persist data in an app between restarts?

Yes, the best way to persist data across restarts is to use UDN (User Defined Namespaces) in AD. Here is a link to the documentation on using namespaces. Writing AppDaemon Apps — AppDaemon 4.2.1 documentation

1 Like

Thanks @proggie this did the trick.

If anyone else stumbles upon this, there were two other things I didn’t realize at first that may save someone some trouble

  1. The namespaces: bit in appdaemon.yaml needs to be indented under the appdaemon: bit, otherwise there will be errors that indicate the namespace doesn’t exist.
  2. You need to restart appdaemon to pick up the namespace (this was actually obvious, but maybe not to everyone).
  3. Just calling .set_namespace('userspace') means that you end up no longer being able to read and set states from home assistant, so you really need to flip back and forth.

One thing I still don’t know is if there is a dashboard or other place where I can see the current user namespace states, like I can by looking at home assistant’s “states” tab.

Hey @Gregable, you can use HADashboard’s Admin UI to view the different entities in any namespace (include default which is usually Home Assistant). The UI runs on port 5050 by default if you have it enabled in the config.

Additionally, if you use what is called “the new way” of developing AD apps, which means doing an import adbase and creating a class that inherits adbase.ADBase then you can work with entities across all namespaces in one place without having to constantly switch between namespaces.

You can also use the self.get_entity() method, specify a namespace once for that entity and work with it throughout your app without having to constantly specify the namespace.

Feel free to join our discord group and we’d be happy to help you work through any of this. Take care!