Template Sensor to check how many lights are on

Hi,
I’m trying to create a template sensor to check the number of lights in a group that are on.
I’ve created the group with all the lights in it and then put this in the sensor template state field

Number of lights currently on in group: {{ states.group.all_lights.attributes.light.all_lights | select('in', states.light | map(attribute='entity_id')) | selectattr('state', 'eq', 'on') | list | count }}

but the sensor appears to be always “unavailable”. I cannot understand what I’m doing wrong.
The group ID is light.all_lights, I’ve put it in the wrong way?
I’ve also tried to do it for the all home without the group with this code

{{ states.light | selectattr('state', 'eq', 'on') | list | count }}

in the second case it works, almost, because actually return a number but is always the double of the actual on lights.

Thanks in advance.

Try this…

All lights

{{ expand(states.light) | selectattr('state', 'eq', 'on') | list | count }}

For a light group

{{ state_attr('light.your_group, 'entity_id') | select('is_state','on') | list | count }}

You don’t need to use expand on states.light. states.light will already give you the state objects.

For the group you could also use:

{{ 'light.your_group' | expand | selectattr('state', 'eq', 'on') | list | count }}

Using expand on a group will expand recursively until there are only single entities, while using the entity_id attribute will still use light groups in case you nested them.

Agreed, I included it because the OP was seeing duplicate entities. As I am sure you already know, expand listed the entities individually as well. It felt like this was a group vs an individual light issue.

{{ states.light | selectattr('state', 'eq', 'on') | list | count }}

I should have been more clear about that…

Thanks a lot to you both. If I use the group it work as expected, instead if I use the “all lights” strangely I have +1 light if > 0. Basically, everything off is correctly 0, after that first light give me 2 as results, 2 lights 3, and so on.
I really don’t understand why.

Not sure what lights you’re using. But:

Some integrations, like Hue, add an extra light entity for each room or zone.

So, for example, if you have two lights in your “Living Room” zone, named “lamp1” and “lamp2”, you might find three entities in Home Assistant: light.lamp1, light.lamp2, and light.living_room (or something similar depending on what you called your Living Room in the app). light.living_room might report ON if either of the 2 lamp_* entities is on.

In fact, even if you had just one single light in your Living Room, if your lighting configuration associates lights with zones, you might find that an extra light entity appears in HA. Or if you assigned that single light to multiple zones, you might find additional entities in HA for each zone.

Shooting from the hip here, but hopefully it’s helpful.

It should be something like that. I’ve a BTicino system and I don’t see any more light entity that the ones that I can on/off but well, no real issue. I’m using a group with inside all the lights I want to track and controlling that.

Thanks a lot folks.

If you use states.light it will also count the group itself, as that is also a light entity.

1 Like