Counting the total number of Lights & Switches that are ON

Hello everyone

I am trying to get the total number of Lights & Switches that are currently ON using below sensor template

 - sensor:
     - name: "Total_Devices_On"
       state: "{{ expand(states.light, states.switch) | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"       

however it returns the number of all devices (i.e. lights + switches) and not only those that are currently on.

On this other sensor below for the Lights only, it returns twice as many lights, meaning if 2 lights are ON, it returns 4 !

 - sensor:
     - name: "Total_Lights_On"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"                               

I have been spending few hours on these to no avail, so I have decided to post it here.

I appreciate any help. Thanks

Try this in the template editor to see which entities are being selected:

{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | map(attribute='entity_id')  | list | join(',' ) }}

Thank you so much for your help, I have 2 cameras and they define lights like this :

light.driveway_infra_red_lights_in_night_mode,
light.frontdoor_infra_red_lights_in_night_mode

I just didn’t really thought of those as Real lights :slight_smile:

Now I have to find a way to exclude them from the equation. Any tips on this?

@tom_l You don’t need the list filter preceding a join.

To filter out those infra red lights add this filter to your statement.

|rejectattr('entity','search','infra_red_lights_in_night_mode')

1 Like

Tom, Thanks a lot, I will add the filter. It seems I have bigger problems with switches.
Just for the records, using your tip, I now can see that Amazon Echo devices define various switches such as:

Entity: switch.everywhere_do_not_disturb_switch
Entity: switch.family_room_echo_do_not_disturb_switch
Entity: switch.familyroom_echo_show_do_not_disturb_switch
Entity: switch.familyroom_echo_show_repeat_switch
Entity: switch.familyroom_echo_show_shuffle_switch

I have plenty of them and other virtual switches. I think I need to revise these sensors and find a way to filter them based on a pattern with search on echo or something they might have in common.

Thanks again

This is an alternative approach and the one I use in my config. The benefit of doing it this way is the light entities are defined into a group and you’re not having to constantly iterate the states machine.

First you need to set some custom attributes for your light entities. You can categorize them any way that makes sense for your system.

Here’s an example from my config. You probably only need something like the type attribute.
This is the file in my config where I do this.

homeassistant:
  customize:
    light.hallway_potlights:
      node_id: 111
      type: dimmer
      notify_led: true
      presence_led: true
      commute_led: true
      night_lux: true
      scene: true

Then you need to set up an automation at startup and run a service call like this one to define your light group. This is the automation where I set up all my entity groups.

    - service: group.set
      data:
        object_id: lights
        entities: >
          {{ states.light
              |selectattr('attributes.type','defined')
              |selectattr('attributes.type','in',['dimmer','switch','bulb','group'])
              |rejectattr('attributes.rgb_light','in',['control','slave'])
              |map(attribute='entity_id')|list|sort }}

Now when you want to do something like count lights that are on or something like that you query your lights group instead of the states machine. You don’t need to bother filtering out unwanted entities every time because you did that when you created the group.

{{ expand('group.lights')|selectattr('state','eq','on')|map(attribute='entity_id')|list|sort }}
1 Like

Thank you Jason, I will try this later.