Want to know the secret sauce that goes into Home Assistant, allowing you to remember the state of a light, or any other entity for that matter?
This illusive technique is easier than you might think!
There are many reasons that you might want to remember the state of a series of entities.
In this tutorial we will learn how to change the color of an LED strip momentarily before changing it back to its original colour.
There are many reasons why it might be beneficial to be able to remember the previous state and attributes.
For example, temporarily changing the colour of a light could be linked to a doorbell.
When someone is at the door, the lights in the room change colour for a moment and then return to their original colour. Awesome!
Table of Contents
Prerequisite
You should have Home Assistant installed and running and a basic understanding of YAML including how to edit configuration.yaml.
You should also understand how to use a service and entity within Home Assistant.
If you are new to Home Assistant or you are not yet familiar with editing YAML files then you should definitely check out my beginners tutorial on YAML first.
I would also recommend taking a look at my other tutorials on automation and scripts.
Configure Home Assistant
In order to demonstrate the principal, we will create a script that changes the colour of a LED strip, waits for 5 seconds and then changes it back to its original colour.
This is made possible by using the scene.create service. Our script will first create a new scene and then it will save the current settings within the new scene.
This scene is simply overwritten each time the script is called.
Following the creation of the scene we can change our LED strip however we desire before loading the scene that we just created.
This will restore the original state of the LED strip.
Create a script
First we need to open up our configuration.yaml file and add a new script.
We will call it led_notification_script for the purpose of this example, but you can name it however you prefer.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence:
Create a scene
Next we will specify the scene.create sequence. It needs a data_template with a scene_id, which will be the name of the scene we create.
In this case we will create the scene led_strip_state.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state
This scene will appear in Home Assistant like any other scene and can be accessed with scene.led_strip_state.
We also need to specify a list of entities to be added to the scene we are creating.
For this example we will add the entity light.ledstrip but you can choose as many entities as you like from your own configuration.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip:
Next is the clever bit! We will use a template to set the state and xy_color light attributes for our new scene.
This allows us to set these values as the current values for the particular entity.
First we need to read the current state of our light.ledstrip entity as we want to remember whether the LED strip was on or off.
We can read the current state using the following template.
{{ states('light.ledstrip') }}
Rather than specifying explicitly ‘on’ or ‘off’ for the state of our entity within the new scene, we can use the template.
This will set the desired state of the entity in the new scene as the entities current state.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip: state: "{{ states('light.ledstrip') }}"
Next we need to specify the xy_color attribute with two numerical values surrounded by single quotes and separated by a comma inside of square brackets.
xy_color: ['0.199','0.724']
We need to replace each of the constant values with template that fetches the current value.
We need to fetch this from states.light.ledstrip.attributes.xy_color.
The attribute xy_color is an array containing two values for x and y, [0] and [1] respectively. Therefore our data templates will be as follows.
{{states.light.ledstrip.attributes.xy_color[0]|float}} {{states.light.ledstrip.attributes.xy_color[1]|float}}
All we need to do is replace the constant values with the data templates. Don’t worry if you don’t fully understand it, just copy and paste it into your own code.
Templates are one of the more complex elements of Home Assistant and really need a tutorial or two of their own!
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip: state: "{{ states('light.ledstrip') }}" xy_color: ['{{states.light.ledstrip.attributes.xy_color[0]|float}}','{{states.light.ledstrip.attributes.xy_color[1]|float}}']
Change the colour
The next item in our sequence will be a simple light.turn_on event that switches the colour of our LED strip to red.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip: state: "{{ states('light.ledstrip') }}" xy_color: ['{{states.light.ledstrip.attributes.xy_color[0]|float}}','{{states.light.ledstrip.attributes.xy_color[1]|float}}'] # Change LED strip to full brightness red - service: light.turn_on data: entity_id: light.ledstrip rgb_color: [255,0,0]
Add a delay
After we change the colour of our light entity to red we need to create a delay.
For this example the script will pause for 5 seconds before it changes the LED strip back to the original saved state.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip: state: "{{ states('light.ledstrip') }}" xy_color: ['{{states.light.ledstrip.attributes.xy_color[0]|float}}','{{states.light.ledstrip.attributes.xy_color[1]|float}}'] # Change LED strip to full brightness red - service: light.turn_on data: entity_id: light.ledstrip rgb_color: [255,0,0] # Delay for 5 seconds - delay: seconds: 5
Load previous scene
Finally after the pause we can load the scene that we created at the beginning of the script the restore the LED strip back to its original state.
This is done using the scene.turn_on service and specifying the entity_id as the scene we created at the beginning of the script.
led_notification_script: alias: Change lights for a moment description: 'Makes LED strip red for a moment' sequence: # Save current LED strip settings as a new scene - service: scene.create data_template: scene_id: led_strip_state entities: light.ledstrip: state: "{{ states('light.ledstrip') }}" xy_color: ['{{states.light.ledstrip.attributes.xy_color[0]|float}}','{{states.light.ledstrip.attributes.xy_color[1]|float}}'] # Change LED strip to full brightness red - service: light.turn_on data: entity_id: light.ledstrip rgb_color: [255,0,0] # Delay for 5 seconds - delay: seconds: 5 # Switch lights back to their original state - service: scene.turn_on entity_id: scene.led_strip_state
That’s it! go ahead and run the script and you should see your specified light entity change to red for 5 seconds and then back to the original colour.
Conclusion
In this tutorial we have learnt how to save the state of a light entity by making use of the script.create service.
Although it is a basic example, you should now have the understanding to apply it to some more complex automation.
Why not go ahead and check out some of my other Home Assistant tutorials to get some ideas for the kind of things you could apply the state saving technique to!
Thanks so much for visiting my site! If this article helped you achieve your goal and you want to say thanks, you can now support my work by buying me a coffee. I promise I won't spend it on beer instead... 😏
Exactly what I was looking for and very good documented!
Thanks!