Counts the lights on

You removed the sensor field. Add it back.

Oh yeah, that worked!

Thanks!

I tried your code. Works but i have also 1 light on when every light is of. Did you find a solution for this?

Hi again. Found it, my integration to my asus router has led on off, there it was.

Please don’t post pictures of code. We are not going to rewrite the code from scratch to assist.

Someone asked this earlier but don’t think it was answered. I have a code currently that checks all light on but while most lights are smart some aren’t and a few of them would turn on using a single relay. These would obviously show as 1 light on.

How do I get these to show the actual number so they match the way my other lights show? Thanks.

Create a template-sensor / helper, which shows amount of entities ( as state ) connected to this particular relay ( condition relay = on ) else (state) 0

And as you haven’t posted your current code , i suggest you simply add this , the state of the template-sensor , to your current code ( sum )

Trigger relay to on , state = X else 0

Result of your code ( sum ) + ( result of your template-sensor / helper ) ( sum )

Sorry this is my code currently:

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

I’m new to templates so please direct, much appreciated.

Ps: where should I add this and how do I point this code to the relay?

Hello oyvindhaug,

I saw this whilst desperately looking for such a solution. Could you please show me how you use a custom button card with the “lights on” counter.
I would appreciate your assistance on this.

Thanks

Kwame

Hey,
is it possible to create a lights on counter, but only for certain lights. Or only for a group of lights? I rejected every light and light group except the one I need.

I’ve tried this but I can’t get it to work in the file editor:
i have one there already. Is it the indintations?

thanks,



Well, a simpel solution ( Not the most optimal )

Template-sensor 1 ( In i.e Configuration.yaml )

  - sensor:
      - name: "relay"
        state: > 
          {{'5' if is_state('switch.my-relay-switch', 'on') else '0' }}

Template: ( -name: “Total Lights Count Template” )

{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list |count + states('sensor.relay') | float | round(0) }}

Edit: So you could place these 2 in config.yaml, and replace your current template-sensor

Hi,

How can I exclude scenes and groups from this template? Thanks!

{{ expand(area_entities('woonkamer')) | rejectattr('attributes.entity_id', 'defined') | rejectattr('attributes.entity_id', '!=', undefined) | selectattr('domain','eq','light') | selectattr('state','eq','on') | list | count}}

I copied this code from community, it works but when turning only one light on the count shows as two?

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.friendly_name','search','All')| list | count }}

Probably you added the lights in a group. Here is a way that eliminates groups. If you do not have a Philips Hue lights you can remove the following line :

|rejectattr('attributes.is_hue_group', 'true')


template:

# Nombre de lumières allumées
          
  - sensor:
      - name: "Nombre de lumières allumées"
        unique_id: "nombre_de_lumieres_allumees"          
        state: >
           {{ states.light
              |rejectattr('attributes.is_hue_group', 'true')
              |rejectattr('attributes.entity_id', 'defined')
              |selectattr('state', 'eq', 'on')
              |list
              |count
           }} 

2 Likes

@ValMarDav is almost certainly right. if you’ve got a light group containing that light, when you turn it on, you are turning on that light and the light group.

one way you can check is to put your code with out the |count into dev tools->template … it will tell you exactly what lights are considered on:

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.friendly_name','search','All')| list }}

1 Like

Thanks so much for the revised code. Everything is working perfectly now.

One more question. I was able to use the above code for my ceiling fans too changing “states.fan” I would like to add a third chip for my HVACs. I created a group in yaml and the group entity is seen when setting up chip. Using code above “state.climate” does not produce a count. What needs to be changed to achieve?

Climate devices generally don’t have a state of on. They have heat' cool` etc…

Check developer tools to see your modes

Thanks for the guidance. I have updated the code and changed to ‘state.thermostat’. I now have a value showing in the chip of ‘0’ I have one HVAC currently running. Any thoughts on what I’ve missed?

{{ states.climate | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'cool', 'heat', 'dry') | list | count }}

group.yaml

thermostats:
  name: Thermostats
  entities:
    - climate.device_0279b3
    - climate.device_0276f8
    - climate.device_02731d
    - climate.device_02786c

Turn you thermostat to heat, do you get a count of 1?

{{ states.climate | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'heat') | list | count }}