Counts the lights on

Works. Thank you very much for helping.

Try with entity-filter and if you want custom.toggle-control-button-row (there is also a version of this custom item for light if you want)

And half of it is completely useless. The automation is not needed and wrong. Please see the sticky note. Sharing chat gpt as answers is strictly prohibited because it outputs nonsense like that.

Hi there! Thanks to this topic, I have a working counter for groups of lights for my various rooms, and it works for the most part! It counts the amount of lights that are on, and that should be more then I can ask for. However, I have a problem where the amount of lights on does not update fast enough I guess, and takes a refresh to get the light count accurate. Hereā€™s an example:


In the above image, four lights are on, but it only shows one. But when I manually refresh, it shows correctly as four, so it is counting correctly. My code in my configuration.yaml looks like this:

template:
  - sensor:
      - name: Living Room Lights On
        state: "{{ state_attr('light.living_room_lights','entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
  - sensor:
      - name: Jacobs Office Lights On
        state: "{{ state_attr('light.jacobs_office_lights','entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
  - sensor:
      - name: Aislins Office Lights On
        state: "{{ state_attr('light.aislins_office_lights','entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
  - sensor:
      - name: Bedroom Lights On
        state: "{{ state_attr('light.bedroom_lights','entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"

Is there anything that I can change in my code to have the counter be more accurate, faster? The issue I have isnā€™t a dealbreaker, but it is kinda annoying. Thanks!

This worked perfectly for me

Canā€™t get sensor value. I used your configuration and substituted my lamps. When I look at the sensor I see the number of lights on, but I donā€™t see them when I use the mushroom card chip template

When you have a group within a group, is it possible to use the expand function and include the nested group entity but not the entities inside that nested group?

I have a light group that contains multiple light entities and also contains a second light group inside of it which is a group of bulbs. I want to count the lights that are on, but not the individual bulbs. I want that bulb group to be counted as 1.

Using this code, I get a count of each individual bulb which Iā€™m trying to avoid.

{{expand(state_attr('light.master_bedroom_light_group', 'entity_id'))| selectattr('state','eq','on')| map(attribute='entity_id')|list|count}}

I figured I could just reject any entities containing bulb. This excludes the bulbs but doesnā€™t count the light group they belong to.

{{expand(state_attr('light.master_bedroom_light_group', 'entity_id'))| selectattr('state','eq','on')| rejectattr('entity_id', 'search', '_bulb')| map(attribute='entity_id')|list|count}}

Manually assigning the light entities to a variable does work

{% set lights = [
  states.light.master_bedroom_fan_lights,
  states.light.closet_light,
  states.light.master_bathroom_counter_lights,
  states.light.master_bathroom_shower_light,
  states.light.master_bathroom_light
  ] %}
{{ lights | selectattr('state','eq','on') | map(attribute='entity_id')|list|count }}

But doing it this way when I go to implement a whole home light count will be 100+ lines of code and a bit of a pain to update and maintain if anything ever changes.

I feel this may be a limitation to the expand function and I may have no other choice. I figure other people have wanted a count of lights on but donā€™t want individual bulbs counted so I figured Iā€™d check before manually assigning every light entity in the house to a variable.

You can hack it with some subtraction. In my example my kitchen table has 3 bulbs. I created a kitchen table light group. Then I created a kitchen light group with the bulb group, kitchen light, and light strip.

{% set zeeGroup = 'light.kitchen_light_group' %}
{{ expand(state_attr(zeeGroup, 'entity_id'))|
selectattr('state','eq','on')|
map(attribute='entity_id')|list|count - expand(state_attr(zeeGroup, 'entity_id'))|
selectattr('state','eq','on')|
rejectattr('entity_id', 'search', '_bulb')|
map(attribute='entity_id')|list|count}}

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

This is the simple, correct answer, for those that scrolled too far.

Hello Everyone,

for the gods sake can someone please explain this to me step by step, I watched videos and read thousands of forum entries but as a beginner I canā€™t find: 1st where should I place the file that contains yaml? If directory is doesnā€™t matter just created a file in the same directory as configuration.yaml and typed the some of the examples (I adapted them if in my case necessary) but when I want to add a card, I canā€™t find any entity that gives me the count of turned on lights. If someone can explain me the steps I will be glad. Thank you in advance!

The easiest way is to add the following line sensor: !include sensors.yaml in the file /config/configuration.yaml. Add the code in the sensors.yaml file and restart HA.

2 Likes

I will try and reply. Thank you for your answer. Regards!

Edit: It worked, thank you very much @ValMarDav !

Regards.

Iā€™m going to jump in on this. If thatā€™s okay.
Iā€™ve been digging this post but canā€™t find whats wrong.

Currently iā€™m using the code below:

# Total Lights On
- platform: template
  sensors:
    total_lights:
      unique_id: total_light
      friendly_name: "Total Lights On"
      value_template: >-
        {{ states.light
          |selectattr('state','eq','on')
          |rejectattr('attributes.entity_id','defined')
          |list |count }}
      attribute_templates:
        light_entities: >-
          {% set domain = 'light' %}
          {% set entities = states[domain] | selectattr('state','eq','on') | map(attribute='entity_id') | list %}
          {%- for s in entities -%}
          - {{ s }},
          {%- endfor %}

So far so good.
The strange thing is: there is always 1 light onā€¦ ??

Can this be a group i have?. There is litterely nothing on in the house.
Were and how do i exclude some lights in this code so i can start debugging things?.

French language :

I have created this code in configurations.yaml, works perfectly you can try if you wish.
Just replace the light entities name corresponding to yourā€™s

template:
  - sensor:
      - name: "lights on"
        unique_id: lights_on
        icon: mdi:lightbulb-on
        state: >
          {% set lights = [
              states('light.living_room_bulb'),
              states('light.hallway_bulb'),
              states('light.stairs_1st_floor_bulb'),
              states('light.stairs_2nd_floor_bulb'),
              states('light.salt_lamp')
              ] %}
          {{ lights | select('eq', 'on') | list | count }}

This is great. Itā€™s working to count them up, but is it possible to have the state of the sensor report as just the number. Currently itā€™s reporting as ā€œstate: > x)ā€.

Would love for this to just be a whole number that I can use to display in various parts of my dashboard.

Can you try this ā€¦

...
          {{ lights | select('eq', 'on') | list | count | int }}

[/quote]

Maybe you should post your template, as itā€™s hard to know which template you refers to , with your particular ā€œoutcomeā€

So with the new update Iā€™ve had to remove the YAML code for the system monitor since Home Assistant now natively integrates it, which is great. However, I used the above code (as seen in my screenshot below) and initially it was just underneath the system monitor code and it worked perfectly. After removing the system monitor code I just get an invalid config error when restarting Home Assistant.

So there has to be something wrong with my code but I just canā€™t figure out exactly what. Any help will be greatly appreciated!