Counts the lights on

hi, thanks for your quick reply!

tried it. there are two lights on but showing 0 !!

make sure your area is actually named “Bedroom 1” and that the “Bedroom 1” contains those bedroom lights.

EDIT Also found a typo, review my original post again with the updates

area name : Bedroom 1
Area ID: bedroom_1

see my edit

YES! that works.

I am also trying to get light brightness and when it was last on? could you please help me.
thanks.

A post was split to a new topic: How do I display count in lovelace using decluttering card

I also want to mention what the status of my garage door is (cover_garagedoor).
Any idea how i show the state (open or closed) in the markdown card?

Edit: found a solution to use the position sensor:
De garagepoort is {% if is_state(‘sensor.garagepoort_positie’, ‘0’) %} gesloten {% else %} open {% endif %}

This is really an interesting thread!
I’m trying to count the lights on my different floors (Gelijkvloers, Verdieping, Zolder, Buiten).

I have following light groups:

# Light groups
light:
  - platform: group
    name: Lichten Gelijkvloers
    entities:
      - light.hue_go_1
      - xxx
  - platform: group
    name: Lichten Verdieping
    entities:
      - light.hue_lightstrip_plus_bad
      - xxx
  - platform: group
    name: Lichten Zolder
    entities:
      - light.bureau_tim
      -  xxx
  - platform: group
    name: Lichten Buiten
    entities:
      - light.buitenverlichting_keuken
      - xxx

I have following template as mentioned somewhere here::

template:
  - sensor:
      - name: Lichten Aan Gelijkvloers
        state: "{{ state_attr('light.lichten_gelijkvloers’,’entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
      - name: Lichten Aan Verdieping
        state: "{{ state_attr('light.lichten_verdieping’,’entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
      - name: Lichten Aan Zolder
        state: "{{ state_attr('light.lichten_zolder’,’entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"
      - name: Lichten Aan Buiten
        state: "{{ state_attr('light.lichten_buiten’,’entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}"

However I get the following error in the log:

Invalid config for [template]: invalid template (TemplateAssertionError: No filter named 'expand'.) for dictionary value @ data['sensor'][0]['state']. Got "{{ state_attr('light.lichten_gelijkvloers’,’entity_id') | expand | selectattr('state', 'eq', 'on') | list | count }}". (See /config/configuration.yaml, line 267).

Anyone has an idea what’s wrong?

you copied and pasted unformatted code, as a result you have “fancy” quotes. Those do not work with templates.

Many thanks. Indeed, I just changed it.
However the error is still present.

Is this just a copy error or is it wrong indented?

Hi, it’s just a copy error on the forum … in the yaml config it’s right. There were many light included in the light groups, so i deleted most of it for posting it here :slight_smile: . Many thanks for your remark!
I’ll look a little bit further :wink:

Tried and couldn’t get your code to work. This variant works:


  - sensor:
      - name: light group
        state: >-
          {{ expand(state_attr('light.your_light_group', 'entity_id') )
            | selectattr('state', 'eq', 'on') 
            | list 
            | count
          }}

4 Likes

You are a hero! :wink: All is working well now!
Many thanks!!!

Hi Guys.
I am totally new to HA and trying to implement this idea in my ecosystem.
Unsuccesfuly.
Can someone direct me step by step where to put those codes to create this sensor counting lights on?
I did put it in template editor and it counts lights but cannot find the sensor to use.
Thanks in advance

Robert, are you comfortable with yaml? if so, type this in at the bottom of configuration.yaml and restart:

sensors: 
  - platform: template
    count_lights_on:
      friendly_name: "# Lights on"
      unit_of_measurement: 'on'
      value_template: "{{ states.light |selectattr('state','equalto','on') |list |count }}"

You should then have a sensor called sensor.count_lights_on

1 Like

Done it, went to check configuration before start and got this message: Configuration invalid! Integration error: sensors - Integration ‘sensors’ not found.
I can paste pic of this part of my config file, if that helps

1 Like

What he posted is not correct.

sensor:
- platform: template
  sensors:
    count_lights_on:
      friendly_name: "# Lights on"
      unit_of_measurement: 'on'
      value_template: "{{ states.light | selectattr('state', 'eq', 'on') | list | count }}"
5 Likes

Apologies - I have it in a separate .yaml and messed up with a typo and incorrect tabbing when copy/pasting to be be in the configuration.yaml

Thank you so much, you are the stars, guys. All works perfectly.
Just one more thing, got some switches also controlling lights, them entities are coming like: switch.relay_switch_1x3kw. How I could add them to total count of lights? I don’t see any issue to add entities to the list of lights in config.yaml, if that would be the easiest way. This is because, some switches are controling lights and some of them, different devices (not lights), so I believe list of entities would be easiest way.
Thank you