Custom component: Saver

Hi everyone!

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.

8 Likes

Hi, thanks for releasing this custom component :slight_smile:
Could you explain the difference between this one and the scene service please?

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

4 Likes

You can do that with the core scene services.

  • Call the scene.create service.
  • Change to the scene you want.
  • Later recall the created scene.

See the second example here: Scenes - Home Assistant

2 Likes

@carloscae That is a good explanation :+1:

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

1 Like

Likewise with scenes. Any entity’s state can be saved. Lights, switches, media players, you name it.

I’m not knocking your efforts by the way, just pointing out to Carloscae that this is already in the core.

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.

2 Likes

Yeah the core scene create can definitely not do that.

One more difference is that data stored in saver survive a restart ot HA

4 Likes

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?

Thanks in advance!

It’s possible to overwrite saved state. Also please notice that saver.restore_state by default also deletes saved data for given entity.

1 Like

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?

Saver doesn’t restore a state by itself, it executes a provided script and provides saved data as a variables to it (see here).

You can also use saver.saver entity directly, as described here.

It took me a little while to sort out how to save/restore a Hue bulb color and brightness due to attr_hs_color being a tuple but here’s what I did:

  - service: saver.save_state
    data:
      entity_id: light.light_strip
DO STUFF
  - service: saver.restore_state
    data:
      entity_id: light.light_strip
      restore_script:
        - service: light.turn_on
          data_template:
            entity_id: light.light_strip
            hs_color: ["{{ attr_hs_color[0] }}", "{{ attr_hs_color[1] }}"]
            brightness: "{{ attr_brightness }}"

Actually, this doesn’t work if the light’s original state was off:

homeassistant.exceptions.TemplateError: UndefinedError: 'attr_hs_color' is undefined

Somehow I need to only restore state if the saved state was on.

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.

Yes. I am aware of that. if/else isn’t working for me in the restore block. Do you have an example that works?

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 }}" ]

You can add condition to execute light.turn_on only when state was on:

NOT TESTED WARNING

    - service: saver.restore_state
      data:
        entity_id: light.light_strip
        restore_script:
          - condition: template
            value_template: "{{ state == 'on' }}"
          - service: light.turn_on
            data_template:
              entity_id: light.light_strip
              brightness: "{{ attr_brightness }}"
              hs_color: [ "{{ attr_hs_color[0] }}", "{{ attr_hs_color[1] }}" ]
1 Like

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.

Thanks!