Script: saving entity state and restore it at the end of the script

So I’m new to HA scripting, and I’m finding the documentation to be pretty lacking.

What I’m trying to do seems simple enough: at the start of the script, save the state of an entity (light) and, after all the other actions, restore that light to its previous state.

This is where I’m at:

example_script:
    sequence:
      - alias: "Set variables"
        variables:
          state_one:
            value: light.one.state

And, at the end:

       - alias: "Restore one"
         service: light.turn_on
         target:
           entity_id: >
             {% if state_one == 'on' %}
             light.one
             {% endif %}

I’m not even sure if I’m on the right track or not. Can anyone help, please?

Thank you.

2 Likes

To be fair to the numerous volunteers who create and maintain the documentation, it focuses on explaining how to use the tools, not necessarily how to build things with them.

I suggest you review the section on Templating - States because the second example is incorrect and suggests you may have missed the part explaining how to get an entity’s state value.

In addition, the template needs to be redesigned because if the light’s state value is not on the template reports nothing for entity_id. That’s invalid and will cause the service call to fail (i.e. it will generate an error).

@123

Thanks for pointing me in the right direction. As for getting the state of the light, I’m now using states('light.one').

As for the last bit, can a variable be used to define a service? ie:

      - alias: "Return one"
        service: >
            {% if state_one == 'on' %}
            light.turn_on
            {% else %}
            light.turn_off
            {% endif %}
        target:
          entity_id: light.one

Give a second thought about;

  • saving your entity state in a scene
  • restoring your scene at the end

Might be easier

2 Likes

although i do agree that should work, lets be honest. thats not really how it should work imho.

what if you dont use scenes? like nowhere, then you have to get intoo that because of storing a variable.

that makes no sense xD

any way, storing state in variable in a script works fine.

lamp_id: light.lamp1
lamp_state: '{{ states(lamp_id) }}'

- service: light.turn_on
    target:
      entity_id: '{{ lamp_id }}'

- choose:
      - conditions:
          - condition: template
            value_template: '{{ lamp_state== ''off'' }}'

this even works for sending them to another script etc.

that works fine for me :slight_smile:

You didn’t get my proposal, you don’t have to create a scene manually, you can call a scene service just to store the state of specific entities. Moreover, your current use case is basic (on/off), what would you do to include brightness and rgb values?

How to create a scene on the fly and take snapshot of entities; Scenes - Home Assistant

And let’s be honest, your message doesn’t sound respectful

5 Likes
example_script:
    sequence:
      - alias: "Set variables"
        variables:
          state_one: "{{ states('light.one') }}"
      - alias: "Return one"
        service: "light.turn_{{ state_one }}"
        target:
          entity_id: light.one
3 Likes

@123

Thank you very much, I had no idea you could concatenate strings with variables that easily. It works flawlessly.

Is there any resource you recommend to learn more about this notation (ie double brackets, bracket + percentage, etc).

You’re welcome!

I suggest reviewing the following section in the documentation: Templating


Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

1 Like

How can you save the entity state in a scene?

That’s cool and I learned something here.
However, this is just for the use case that I want to save a state inside a script.
What if I want to save a state of, say, an input_boolean helper to another global input_boolean entity. Outside the script, so to speak.
:flushed:

Answered in your other post.

1 Like