Get entity states of a scene

Hello!

I want to create an automation which listens for off->on state changes of any lights, and sets the brightness and color of that light to the corresponding state of the light in a specific scene, depending on the time of day. This way, whenever someone turns on a light, it can turn on to the appropriate scene setting for that time of the day.

I already know how I can create the trigger, where I am stuck is getting the state of a specific light in a scene. I know I can call state_attr to get all entities of a scene, but it doesn’t seem I can read the entities state that way.

This gets me a list of entities: {{state_attr("scene.morning", "entity_id")}}

But the list only contains the entity ids, not the state which they’re in the scene.

Is it possible to get the state of all entities within a scene? The data has to be stored somewhere so I would assume it’s possible somehow.

Thanks!

You would need to iterate the entity ids like this.

{% for entity in state_attr("scene.morning", "entity_id") %}
  {{- states(entity) }}
{% endfor %}
1 Like

Thanks for the reply! But wouldn’t that simply get the current state of the entity (light)? I want to get the state the entity has within the scene.

Correct. It reports a list of entities controlled by the scene and you can then check the current state of each entity but that’s not what you want (you want the entity’s state set by the scene).

Unfortunately, I’m unaware of any existing template function that can reveal this information.

Look in the file scenes.yaml.

Thanks for the reply! That’s a bummer. I guess I can’t use scenes in this scenario. I will have to define all the brightness and color data within the automations for the lights. It’s a shame because it would be so nice to have it all stored in scenes. Do you have any other ideas how I might accomplish my goal of recalling a specific brightness and color for any light when it is turned on?

Thanks!

Use a script instead of a scene.

Your automation can call the appropriate script depending on the time of day and/or whatever other criteria you require.

Yeah that seems the best. I imagine storing the light states for the different times of day in a dictionary variable of some kind in the script? It would have been cool if a scene could be used just for that storing of different light states, since it’s a much nicer experience editing a scene than editing YAML.

Anyways, thanks a lot for your input! :slightly_smiling_face:

Correct. You have highlighted one of the advantages of a script vs scene and that’s the ability to make decisions based on conditions.