Template sensor - counting devices in area

Hi - I want to count how many lights are on in an area, e.g. kitchen. I have created the following helper template yaml but struggling to limit it to areas?

Thanks in advance


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

"{{ area_entities('the area') | select('search', '^light.') | select('is_state','on') | list | count }}"

Super quick reply - so thank you! What if I want to exclude certain items? Also does this work for floors (which have multiple areas)?

Thanks so much!!!

it does not work with floors. If you want to exclude things, use reject.

Thanks - this seems to work but I noticed when I turned some lights on (after having no lights on) it counted more than one going on and then when I turned off it had these phantom count as still showing up. Thoughts?

You likely need to remove your grouped lights using reject

Thank you!

I tried this

{{
area_entities(‘kitchen’) | select(‘search’, ‘^light.’) | rejectattr(‘entity_id’, ‘search’, ‘light.all_kitchen’) | select(‘is_state’,‘on’) | list | count }}

But I am getting ‘str object’ has no attribute ‘entity_id’’ error. Do you know why?

Thanks (And apologies)

use
| expand

{{ area_entities('the area') | select('search', '^light.') | reject('eq','light.all_kitchen') | select('is_state','on') | list | count }}

thank you @petro and @10der

1 Like