Counts the lights on

This will return a list

{%- set search_state = 'on' %}
{%- set search_area = 'Living Room' %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) if area_name(light.entity_id) == search_area %}
  {%- set ns.lights = ns.lights + [ light.entity_id ] %}
{%- endfor %}
{{ ns.lights }}
3 Likes

Works like a charm, thanks a lot. When trying to convert to count, i get it working but when i want to use it as a sensor, it fails (of course i hardcoded the search state search area and such instead of configurables, but can’t get it working). What am I doing wrong?

this works for sensor:

          {{ states | selectattr('entity_id','in', ['light.laundry_room','light.hobby_room_spot_2','light.hobby_room_spot_3','light.hobby_room_spot_1','light.living_room_color_1_table' ) |selectattr('state','eq','on') | list | count }}

but as soon as I add area_id or area_name to it, it fails to give me results.
Please assist

just use my template and change the last line to

{{ ns.lights | count }}

And how can I add that into a sensor? That’s whats not working, since ns.lights is something you predefined earlier, right?

you use the whole template in the template sensor

1 Like

works perfectly, thanks so much!

Is it possible to avoid light groups to be counted (I can’t see an attribute like is_group or so) without hardcoding?

add a rejectattr('attributes.entity_id', '!=', undefined) to the for loop line before the if statement

2 Likes

Try this in the Template Editor (replace 'Kitchen' with the name of your area). I tested a similar version on my system and it correctly reported the number of switches (that are on) in the Kitchen area.

{{ expand('light.laundry_room', 'light.hobby_room_spot_2', 
          'light.hobby_room_spot_3', 'light.hobby_room_spot_1', 
          'light.living_room_color_1_table') 
   | selectattr('state', 'eq', 'on')
   | map(attribute='entity_id')
   | map('area_name')
   | select('eq', 'Kitchen')
   | list | count }}
1 Like

That will work if you don’t want entity_id’s

Yes, that’s right but should be adequate for martheijnen who, unless I am mistaken, only requested a count.

you’re not mistaken, I was providing a list of entities

I have this code

sensor:
  - platform: template
    sensors:
      lucesencendidas:
        entity_id:
          - light.habitacion
          - light.mesita
          - light.sala
          - light.comedor
          - light.lectura
        #  - light.gateway_light_7811dcfab6cc
        #  #- switch.cocina
         # - switch.sonoff_1000649ca0 
         # - switch.sonoff_10005b8eb0 
        value_template: "{{  (states | selectattr('entity_id','in',state_attr('group.luces','entity_id')) | selectattr('state','eq','on') | list | count) }}"

It works but i don’t know why from 0 to 1 it counts inmediately, to update to other counts it takes a minute or so. Also happens when turning off (going down…). It takes time.
Anything about this issue?
I think that one two years ago (v.0.92) (i didn’t updated HA) it was working fine.

1 Like

Does group.luces contain any of these entities?
- light.habitacion
- light.mesita
- light.sala
- light.comedor
- light.lectura

Yes all of these.

Whenever you use states in a template, the template is throttled to 1 update per minute.

Fix it by using this template instead:

        value_template: "{{  expand('group.luces') | selectattr('state','eq','on') | list | count) }}"

Yes all of these.

group:
  luces:
    name: IluminaciĂłn
    entities:
      - sensor.lucesencendidas
      - light.habitacion
      - light.mesita 
      - light.sala  
      - light.comedor
      - light.lectura
      - light.gateway_light_7811dcfab6cc
      - sensor.illumination_7811dcfab6cc
      - sensor.illumination_158d0003029a7a

Just trying… tell you in a minute after restarting.

Perfect !!! Thank you so much.
Two hours trying to figure out the problem…until i decided to post it here.
Now…no delay.

If they’re all in the group then petro’s suggestion is the way to go.