Counts the lights on

needs an attribute defined:


    value_template: "{{ states.binary_sensor |selectattr('state', 'eq', 'on')
                       |selectattr('entity_id', 'search', '_contact')
                       | list | count }}"

or


    value_template: "{{ states.binary_sensor |selectattr('state', 'eq', 'on')
                       |selectattr('object_id', 'search', '_contact')
                       | list | count }}"

object_id is what follows after the domain, for example

binary_sensor = domain

sensordooring_contact = object_id

1 Like

I have read the entire thread and I have listed the number of lights on.
image

I explored lovelace-auto-entities and I was able to create something like this: image
But it behaves like an entity, I have to click the entity, then click the dialog to turn off the light and close the dialog.

I want to have a card where the lights (that are ON) are displayed and I can turn them off in the card. Is that possible?

Probably you forgot to define the tap action.

You are right; now I have it working. Thank you
This is my code JIC:

type: custom:auto-entities
card:
  type: vertical-stack
card_param: cards
filter:
  include:
    - entity_id: light.*
      state: 'on'
      options:
        type: custom:mushroom-update-card
        entities:
          - this.entity_id
        tap_action:
          action: call-service
          service: homeassistant.turn_off
          service_data:
            entity_id: this.entity_id

1 Like

Now I want to take one step further and use a template because I already have a group with my lights. The problem is that I donā€™t know how to customize the card type or the tap_action. I have something like this:

type: custom:auto-entities
card:
  type: entities
filter:
  template: >-
    {{ expand('group.Lights_House') | selectattr('state','eq','on') |
    map(attribute='entity_id') | list }}

But it looks like this:
image

I prefer the previous look and feel of how to turn off the lights that are on.

Better to discuss here: šŸ”¹ Auto-entities - Automatically fill cards with entities

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 }}
5 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?.