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:

  - platform: template
        friendly_name: "First light"
          service: script.no_need_to_exist
          service: script.no_need_to_exist
        friendly_name: "Second light"
          service: script.no_need_to_exist
          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?