I have finished my new custom_component that allows saving a state of any entity and restoring it later. This can significantly reduce a usage of dedicated helpers in automations and scripts.
Additionally it enables you to create and use variables without adding additional entities.
have a look on the readme.
you can save the state of an entity and recall it later.
all based on services, so you can use it on automations.
for example, you can save the state of your lights in the living room before calling the “Movie Scene”, then when movie is done, you recall the previous state.
I, for one, think this should be implemented natively on Home Assistant.
Amazing work here. <3
@tom_l To be honest I didn’t know this this feature, but my approach is much more universal. You can save a state of any entity, and use it in any way you want.
The main difference is what you can do with saved data. My approach is much more flexible (saving sensors, partial restoration, restoring to different entities).
But generally speaking you are right, most use cases can be completed using built-in scenes.
I think this is exactly what I need. When I use the saver.save_state of a particular sensor, do I need to use the saver.delete command or is it also possible to use the saver.save_state again to overwrite it?
Maybe I’m overthing that but how do I do that after saving the previous state?
What service do I call as restore_script in order to set the previous state?
service: saver.save_state
data:
entity_id: light.livingroom
service: saver.restore_state
data:
entity_id: light.livingroom
restore_script:
- What do I need here since 'restore_state' is required?
When the light is turned off it doesn’t have attr_hs_color attribute, so it doesn’t exist in saved state. You have to take it into account in restore_script.
This is what I have come up with. It does work, but I’m not really comfortable with the logic of attr_hs_color and attr_hs_color[0] for example. I would prefer to only execute a light.turn_on if the save attr_state was “on”, but I cannot get that if/else logic to work as I see in examples.
- service: saver.restore_state
data:
entity_id: light.light_strip
restore_script:
- service: light.turn_on
data_template:
entity_id: light.light_strip
brightness: "{{ attr_brightness or 0 }}"
hs_color: [ "{{ (attr_hs_color and attr_hs_color[0]) or 1 }}", "{{ (attr_hs_color and attr_hs_color[1]) or 1 }}" ]
That works. I am surprised to see value_template: "{{ state == 'on' }}"
and not value_template: "{{ attr_state == 'on' }}"
but the former works and the latter doesn’t.
Using state in that condition check would seem to be testing for the current state of the light and not the Saver saved state.