How to get all entity_ids from a light group?

I have this code that works perfectly for groups:

{%- for x in states.group.salon.attributes.entity_id -%}
{{ x }}{% if not loop.last %}, {%- endif -%}
{%- endfor -%}

But this doesn’t work for light groups, only for normal groups. How can I iterate over the entity_id of light groups?

Doesn’t appear that you can. The state object is identical to a normal light.

EDIT. There is a new expand method. It might work.

{{ expand('light.xx') }}

Thanks for your reply. Converting “salon” into a light-group and running this code:

{% for x in expand('light.salon') %}
{{x.entity_id}}
{% endfor %}

Just returns the entity_id of the light-group (light.salon in my case).

If you want a comma-delimited list of the group’s members, then you don’t need to iterate through expand’s result (i.e. don’t use a for-loop with expand).

Try this in the Template Editor:

{{ expand('group.salon') | map(attribute='entity_id') | join(', ') }}

It expands group.salon, selects all entity_id’s, then joins the results using a comma and space as a delimiter.


EDIT
My template is not applicable in this case because it’s designed for use with a standard group (not a light group).

1 Like

He’s trying this with a light group, which doesn’t have the entity_id attribute. It’s a shot in the dark if a light group works with expand and judging by his last post, I would say it doesn’t.

Oops! My mistake; I’ve never used light groups and made an incorrect assumption.

Well, the smaller code for filtering groups will come handy since it seems I’ll have to use groups anyway, so thanks for that @123 . Which brings me to another question, how can I add brightness and (for some groups) temperature controls to a group in the UI?

you’d have to get inventive with automations and input_numbers. Or use that light group. And there is a universal climate component too. But you still won’t be able to break out he entity_id’s from those devices because they emulate a physical device, which doesn’t have those attributes.

Thanks for this - I use standard groups for all my lights - the only advantage of light_group was the dialog to adjust them which I never use.

This has solved my problem perfectly.

Leaving this here because this is the main thread that comes up when searching:

{% set a = ((expand('light.light_group')
| map(attribute='attributes.entity_id')
| first) | join(' ')).split() %}
{{ expand(a) | selectattr('state','eq','on')
| map(attribute='entity_id') | list }}
1 Like

That’s alittle over complicated, all you need is:

{{ state_attr('light.light_group', 'entity_id') }}

and for lights that are on

{{ state_attr('light.light_group', 'entity_id') 
   | expand | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}
3 Likes