Count total lights on for a light group

Hello All,

I recently created light entities out of shelly switches and while i was at it I created light groups of those newly created light entities as well as existing light entities. I’m looking for a way to count total lights on per light group. I’ve seen posts on using if statements and manually adding each entity which defeats the purpose of my newly created light groups, same way expanding a standard group wont work as i did not make these standard groups.

I saw this code to expand a light group and it works great at expanding the light group

{{expand(state_attr('light.all_lights', 'entity_id'))|map(attribute='entity_id')|list}}

This is code I found to expand a standard group and get count of on state ,which appears to work.

{{ expand('group.davidsroom') | selectattr('state', 'eq', 'on') | list }}

I tried the below as a merge of both lines above but clearly I dont know if its meant to work or I’m missing anything. Code doesn’t error, it does return the list of entities in developer tools template but it does not give me a count (always shows 0)

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

Any assistance is greatly appreciated

Filters are applied in order left to right. You need to select all the entities that are on before mapping the entity id.

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

Tom thanks for your help. This works quite well. Is there a code to define specific open light entity name? With this code I can only get total numbers.

A what now?

  - service: notify.telegrambot
      message: >
        Evden Ayrıldın ama {{expand(state_attr('group.allhouse', 'entity_id'))|
        ışık açık
mode: single

The above code gives me the total number of lights whos state is on. I would like to know if we can change it as it can exactly state the name of the identity whic is currently on. For example a message like “Corridor hue light is open”.

1 Like

If you are using a legacy group you do not need to use the state_att() function.

{{ expand('group.allhouse')|selectattr('state','eq','on')|map(attribute='name')|list|join(', ') }}
1 Like

That did the trick. Lovely. Thank you very much Tom.


totally forgot to reply. I got so excited when I tested and it worked that I just went into why I needed this. Thanks a lot. Worked perfect first time.

1 Like

Thanks! This helped me with my challenge to show a counter for how many lights that are on both for indoors and outdoors!

Love this community!

I’m trying to get the following code to work but have not luck.

I’m wanting to count which ‘hue’ room has lights on but exclude my driveway lights.

I’ve tried 2 very different codes but one doesn’t work (1) and the other work but doesn’t exclude driveway lights.


{{ states.light | rejectattr('entity_id', 'equalto', 'light.driveway_lights') | selectattr('', 'eq', 'room') | selectattr('state','equalto','on') | selectattr('entity_id','search','lights') | list | count }}


{{ states.light | rejectattr('entity_id', 'equalto', 'light.driveway_lights') | selectattr('state','equalto','on') | selectattr('entity_id','search','lights') | list | count }}

Where am I going wrong?

Also I seem to not be posting the code in the correct format on here :man_facepalming: