List attributes from scene devices with state=on

Hi there,

This is a continuation of my post from yesterday.

Now I am struggling with extracting state_attr from the devices with the state: ‘on’. The line below ({{entity_names | map(‘states’) | select(‘eq’, ‘on’)| list}}) shows me only the “state=on” information. How can I modify the code below to extract a list with entity attributes (e.g brightness, color or friendly_name)

{% set scene_select = states("input_select.active_scene_wohnen") %}
{% set active_scene = "scene.wohnen_" ~ scene_select %}
{% set entity_names = state_attr(active_scene, "entity_id")%}
{% set entity_count = entity_names|count %}
{% set entity_on_count = entity_names | map('states') | select('eq', 'on')| list | count %}
{{entity_on_count}}/{{entity_count}}

{{entity_names | map('states') | select('eq', 'on')| list}}

Instead of mapping to the state then selecting, use select('is_state', 'on'). This will leave you with a list of entity IDs that can then be put through the expand filter to get a list of their state objects.

{{ entity_names | select('is_state', 'on') | expand }}

ok! But this gives everything and not a specific attribute with its value. eg. “friendly_name”

Can you be more specific about the end goal, i.e. what you want returned and how it should be formatted?

Without the expand you have a list of entity ID for entities that are “on”. From there, you can use the tools you’ve already learned to get individual attribute values in a list:

{{entity_names | select('is_state','on') 
| map('state_attr', 'friendly_name') | list}}

Hi @Didgeridrew ,

I have configured different light scenes on switches for my lamps in the living area. Depending on the lighting scene, the number and brightness of the luminaires vary. The name of the current scene with the corresponding parameters could then be visualized on the dashboard.

To be honest, I’ve tried the same code before, but obviously I missed the correct syntax.

Thank you very much.

I don’t know how you plan on displaying it, but one option would be a Markdown card. They’re not the most aesthetically pleasing cards, but they do allow templates, and can put data in columns.

{% set scene_select = states("input_select.active_scene_wohnen") %}
{% set active_scene = "scene.wohnen_" ~ scene_select %}
{% set entity_names = state_attr(active_scene, "entity_id")%}
{%- set entity_count = entity_names|count %}
{%- set on_ents = entity_names | select('is_state','on') | list %}
{%- set entity_on_count = on_ents|count %}

Active Scene: {{state_attr(active_scene, 'friendly_name')}}
{{entity_on_count}}/{{entity_count}}

| Name | Color | Brightness |
|:---- | :----: | :------:|
{%- for e in on_ents|expand %}
|{{ e.name }}| {{ e.attributes.rgb_color|default('',1) }}| {{ e.attributes.brightness|default('',1) }}|
{%- endfor %}

In a Markdown card that will give you something like:

@Didgeridrew ,

whow! This is exactly my usecase!
What I also notice when dealing with scenes is: If a light scene from the “scene network” is changed, the light scene is no longer valid and according to logic, no valid light scene should be displayed. A solution seems to be complicated, as brightness, state and color can be changed in many places. Maybe an automation can help that triggers when a device is changed from the network. I don’t know if this is possible in an uncomplicated way, since the trigger is indeterminate. If you have an idea, who this can be realized, please let me know!

That logic assumes that scenes track the states of their related entities, that is not true in Home Assistant. Scene are not “stateful” like that. There have been a number of requests to add such a feature but no one has volunteered to add it to core.

I’m not really a scene user… I have 4-5 scenes which I only created and use to help people with questions on here.

There is a custom integration, Stateful Scenes that you might want to take a look at. I’ve never used it, so I can’t vouch for it personally, but it seems to be actively maintained.

ok, I will have a look at it.

Last question on this topic:
Do you have an idea how to use the extracted RGB code to display the respective color instead of the code?

That requires a pretty complex template but, luckily, the Color Multi-Tool custom macro can already handle that. Once you have it installed, you would use something like:

{% from 'color_multi_tool.jinja' import rgb2name %}
{{ rgb2name(30, state_attr('light.example','rgb_color')|list) }}