Hi i have on my dashboard some buttons which are triggering a scene on click.
This works very well.
But now it would be grate to see which scene is active.
Is it possible to check if a scene is active and then that the button is highlighted?
There is no such thing as turning off a scene. After all, what should the state of the entities be, when turning off a scene?
If you want them to be, what they were before you activated the scene (which could be different every time) you would have to create a “before” scene on the fly before activating a scene. Then you could go back to the “before” scene.
By the way: For unknown reasons, the event-data in my previous example passes the name of the scene in different formats, depending on how it is called.
So you would be better off using this line:
Ok solved in now in another way with a workaround which works very nice for me
I created for every scene an input_boolean in the config.yaml
Then in the scene itself i set the several inputs to on or off
Afterwards if a scene is executed i got the state of all scences
In my dashboard i can use the input_boolean as an entity for my button. So that the correct activated scene shows on or off:
I’m trying to do the same thing but I’m not sure exactly what you have done here - would you be able to show me an example of what you put n the config.yaml file?
Thanks for posting all that - I think my requirement was slightly less complicated than your and ended up achieving it using templates in the config to simply modify the specific device class icons.
Appreciate this thread has been around for while, but following reading this and a bit of experimentation thought I’d share my take on achieving this:
each room has a series of scenes (scene’s are set independently and at different times so the room distinction is important for me)
all lights that belong to scene in a room also belong to a light group
lights are only enabled by scenes (e.g. a manual button press still triggers a scene not an individual light)
therefore, if a light group is on, then a scene is active – if the group is off no scene is active
I created my required scenes and following a common format:
Name: [Room] – [Description] e.g. “Office – Bright” or “Office – Working”
Entity_id: scene.[room]_[description]
For each room I created a template sensor that checks the state timestamp of the scenes for that room to establish the most recently activated scene. I do this by checking for the [room] name in the entity_id (in this case “office”):
- name: Current Scene Office
state: >
{%- set latest = namespace(time = 0) %}
{%- set selected = namespace(item='') %}
{%- if is_state('light.office_light_group', 'on') %}
{%- for item in states.scene if 'office' in item.entity_id %}
{% if latest.time < as_timestamp(item.state) %}
{% set selected.item = item.attributes.friendly_name %}
{% set latest.time = as_timestamp(item.state) %}
{% endif %}
{%- endfor %}
{{ selected.item.split('- ')[1] }}
{%- endif %}
The sensor then stores the name of the latest scene (this was because at one stage I was texturally outputting it on my dashboard (i.e. “The current office scene is Working”). If the light group is off the sensor will return null.
I then have a button for each scene (by room) using custom button. The tap action actives the scene the button represents and for visual display, I check the status of the template sensor above, and if its state equals the scene the button represents I turn the button “on”.
As I had a template sensor for about 10 rooms there was a lot of repeating logic just changing the name of the room each time. To make things “nicer” I moved to using macros (2023.04 release).
This meant I could compress each room current_scene sensor to the following:
- name: Current Scene Lounge
state: >
{% from 'current_scene.jinja' import current_scene %}
{{ current_scene('light.lounge_light_group', 'lounge') }}
- name: Current Scene Hall
state: >
{% from 'current_scene.jinja' import current_scene %}
{{ current_scene('light.hall_light_group', 'hall') }}
... etc ...
With each of these calling the following macro:
{% macro current_scene(light_group, room_name) %}
{%- set selected = namespace(name=none,time=0) %}
{%- if is_state (light_group, 'on') %}
{%- for item in states.scene if room_name in item.entity_id %}
{% if selected.time < as_timestamp(item.state) %}
{% set selected.name = item.attributes.friendly_name %}
{% set selected.time = as_timestamp(item.state) %}
{% endif %}
{%- endfor %}
{{ selected.name.split('- ')[1] }}
{%- else %}
Off
{%- endif %}
{% endmacro %}
Necessary, No - interesting to do, Yes - maintainable and scalable for future, Yes. As with everything Home Assistant related its never really done, I’m sure at some point in the next 6 months I’ll rip this out and revise with something different!