Store persistent state of many lights programatically

Hi there! First time commenter, long time user and fan of AppDaemon and HomeAssistant. I have a small conundrum about how to store state using AD, hope that someone can give me a hand.

I have three lights and a zigbee remote in the living room, this remote has 5 buttons reserved to store and restore states, and I use AppDaemon to handle all zigbee actions related to the remote.

My aim is, when I long press a state button, I want to store somewhere the state of all three lights as they are in that moment , and when I short press the same button, I want to restore the lights to that previous state.

Now, my question is “where and how to store that state?”. For other automations, I create a sensor or whatever in HA (statically in the .conf files if I want the state to persist across HA reboots, or dynamically in AppDaemon, if I don’t need that). However, this time, I feel like creating 15 different virtual lights in my conf files is a terrible hack. Ideally I would like to do something programmatically in AppDaemon. Does someone have a better idea?

I could be convinced to use the AppDaemon namespaces. However, I don’t like it much from the aesthetics POV (state should be a HA thing). Also, the last time I tried to use them, it worked well for a while, and then an update to AppDaemon hosed my whole system until I managed to work out that the issue was in the namespace I was storing state in, so after some hours of very frustrating debugging, I deleted the namespace and never looked back.

I use template lights for this:

light:
  - platform: template
    lights:
      first_light:
        friendly_name: "First light"
        turn_on:
          service: script.no_need_to_exist
        turn_off:
          service: script.no_need_to_exist
      second_light:
        friendly_name: "Second light"
        turn_on:
          service: script.no_need_to_exist
        turn_off:
          service: script.no_need_to_exist

Then you can copy the attributes in appdaemon with:

        attr = self.get_state("light.tomas_bed_lamp", attribute="all")
  
        self.set_state("light.first_light", attributes = attr['attributes'])

This way, all data is stored in HA.

The scene integration has a service call to create a scene on the fly. It can be used to store states.

First button create the scene, do stuff… second button apply the created scene. No app daemon just straight HA.

Thank you for your answer! I kind of wanted to avoid having to create an entry for each light and state as I would have to create 15 of them, and my yaml files are dirty enough as they are. Though TBH I hadn’t thought of using templates for that. Would that store other states beyond ’ on/off’? Like brightness or color.

Gotta say I’m allergic to YAML :p. You mean using the ‘scene.create’ service? Wouldn’t that scene be discarded when HA reboots?

Looking for the same thing, did you figure out a solution? Thx

I’m afraid I didn’t, the templating solution @tjntomas proposed does not persist through reboots of HA, which doesn’t work for my usecase.

I’ve been experimenting with the virtual components extension which looks like it should do what I want, but it’s quite unstable. Right now there is bug open due to the stable version not working with the 2023.11.2 version of HA. I’ll be waiting until the v0.9 of virtual components goes out of alpha to try again.

You had more luck?

No luck yet. Actually surprised this is not already part of the core “scene” Integration. Making scene.create persistent is all that’s needed… :confused:

I need to investigate the “saver” component as a potential workaround but didn’t have time yet: