Confusion regarding how template map filter works

Hi.

I am trying to pull out attributes of a group, for example a light group

the attribute argument for map is confusing me. attribute=‘name’ appears to be the friendly name add tribute.

{{ expand(‘light.test_light_group’)
map(attribute=“name”) | list}}

yields something like…

[‘Kitchen Lights’, ‘Family Room Lights’]

My question is how can I actually pull out a specific attribute contained in a light. For my light, here are the listed attributes:

supported_color_modes: brightness
color_mode: brightness
brightness: 255
device_id: 4
zone_id: 2
icon: mdi:light-recessed
friendly_name: Kitchen Lights
supported_features: 32

Is there syntax for map that would give me the brightness for each, or the icon for each, etc?

Brightness and icon are members of the attributes list of an entity. Use:


|map(attribute='attributes.icon')

Thank you! That was actually the first thing I was trying, and kept getting nothing back. Tried several times, thinking I spelled something wrong. Looks like I did, because when I copied your syntax (which visually looks the same as mine), it worked.

Feel silly for have asked, but thank again!

You don’t have to feel silly.

Maybe it hasn’t worked because of the missing pipe (at least in your code example above).

1 Like

For future reference, the word attribute in Jinja’s map filter doesn’t mean the same thing as the word attributes in Home Assistant. The two words refer to different things.

Look at the list of properties in a State Object. Note that one of those properties is name.

That’s what the template is extracting when you use map(attribute='name'). It’s getting the value of the name property from each supplied State Object.

You’ll notice that there’s another property in a State Object called attributes. The value of attributes is a dictionary containing one or more instances of what everyone commonly calls “an attribute” (which serves to make everything even more confusing because that word is being used so many different ways).

So if you want map to get the value of one of the keys that are in the attributes dictionary, such as color_mode, you would use map(attribute='attributes.color_mode')

2 Likes

Yes, I know, but my language skills are so poor that it is a pain :see_no_evil:
Thank you for this good addition!

Actually, please accept my apologies. I intended to reply directly to smarthome01824 yet mistakenly clicked the reply button in your post. My explanation was meant for smarthome01824; I am confident you know the details of attribute/attributes. Plus there’s nothing wrong with your language skills.

Also, keep in mind that not all entities within a group will have all the same attributes or defined values for those attributes, so make sure to select for the attribute type you want or your template may fail due to errors.

{{ expand('light.test_light_group')
| selectattr('attributes.icon', 'defined')
| map(attribute='attributes.icon') | list }}
1 Like