Count lights on in an room

Hi,

Is there a code that allows me to see how many lights are on in one room?

With this I can see the total number of lights that are on:

{{ states.switch|selectattr('state','equalto','on')|list|length }} Aan

With this I only see the number when all the lights of the identiti are on

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.studio_count','search','All')| list | count }}

But I would like to know how many lamps are on in my studio, for example. In the studio light1 on, light2 on and light3 off. So if 2 lights are on in the studio i like to have an number of 2. Now i get 0 or all the lights in my house.

Assuming you have an actual “Studio” area, where you put your lights:

{{ 
  expand(states.light) 
  |selectattr('state', 'eq', 'on') 
  |selectattr('entity_id', 'in', area_entities('Studio'))
  |list
  |count
}}
{{ area_entities('Studio')
   |select('match','light')
   |select('is_state','on')
   |list
   |count }}

thnx all i going to try that. first have an problem with

{{ states.light|selectattr('state','equalto','on')|list|length }} On

If everithis is off then it is 0, if one light is on it counts 2 (no dubble light) it seems it is counting also the light groups. Is there an way to only count the real lights?

By rejecting entities that are light groups. A light group has an attribute named entity_id so you have to filter out entities containing that specific attribute.

{{ expand(area_entities('Studio') | select('match', 'light'))
   | rejectattr('attributes.entity_id', 'defined')
   | selectattr('state', 'eq', 'on')
   | list | count }}

If you have Hue groups, the template will need another filter.

Thnx 123! Work a little bit better but it is stil counting the hue groups. The Ikea groups works perfect. How can i also filter the Hue groups?

I have now:

primary: >-
  {{ states.light 
  |rejectattr('attributes.entity_id', 'defined')
  |selectattr('state','equalto','on') 
  |list|length }} Aan
{{ expand(area_entities('Studio') | select('match', 'light'))
   | rejectattr('attributes.entity_id', 'defined')
   | rejectattr('attributes.is_hue_group', 'defined')
   | selectattr('state', 'eq', 'on')
   | list | count }}

Or if you want to count all lights on and not just the ones in the Studio area:

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

Thnx 123! Works great :grinning:

1 Like