Counts the lights on

Thank you so much.
Now both work, but the counting problem remains.
If I turn on the “table” light which would be “light.tavolo” present in the kitchen group, it signals me 1 light ON in this group and 2 ON in the other.
Why ???

You most likely have light groups that are skewing the numbers.

I’ve some light group, but they are not recalled in this list

# Lights count
sensor:  
  - platform: template
    sensors:
      lightsoncountopenspace:
        friendly_name: "Luci Accese nell'open Space"
        value_template: >
          {% set reject = [ 'light.hue_go_destra', 'light.hue_go_sinistra', 'light.hue_play_gradient_lightstrip_1', 'light.piantana', 'light.plafoniera_1', 'light.plafoniera_2_2' ] %}
          {{ states.light | rejectattr('object_id', 'in', reject) | selectattr('state','equalto','on')| list | length }}

The list rejects lights, it doesn’t select lights. So your count is counting all lights accept for what you have listed in your template.

My bad english.
Sorry and thanks for your patience

Sorry to bother, but I’ve a little problem.
I’ve three spotlights controlled by a switch (switch.zigbee_3_gang_switch_switch_1). How I can integrate in this count? (if is possible…1 switch 3 spotlights)

Hello everyone,
I think I’ve read every single reply in this thread now but I seem to miss something when implementing the count.
I unfortunately could not implement the easiest codes here since not all my lights are actually lights in HA, but switches (since they’re controlled with Shelly switches)
So I’ve come to this now, but the Developer Template Editor returns 0 lights being on (no error).

{% set lights = expand[
  states('switch.one'),
  states('switch.two'),
  states('switch.three'),
  states('switch.four'),
  states('switch.five'),
  states('light.six'),
  states('switch.seven'),
  states('switch.eight'),
  states('switch.nine'),
  states('light.ten'),
  states('switch.eleven'),
  states('switch.twelve')
  ] %}
  {{ states.lights
    | selectattr('state','eq','on') | list | count }}

Names changed to written numbers for readability.
The thing is, when I change the last line to

  {{ lights
    | selectattr('state','eq','on') | list | count }}

I do get the following error
UndefinedError: function object has no element ('off', 'unavailable', 'off', 'off', 'off', 'off', 'off', 'off', 'off', 'off', 'off', 'on')

where I can clearly see, which lights are on or off.

Any help would be greatly appreciated. Thank you!!
Best wishes

Try


        value_template: >
          {% set reject = [ 'light.hue_go_destra', 'light.hue_go_sinistra', 'light.hue_play_gradient_lightstrip_1', 'light.piantana', 'light.plafoniera_1', 'light.plafoniera_2_2' ] %}
          {% set l = states.light | rejectattr('object_id', 'in', reject) | selectattr('state','equalto','on')| list | length %}
          {% set add = states('switch.zigbee_3_gang_switch_switch_1') == 'on' %}
          {{ l + 3 if add else l }} 

1 Like

{% set lights = [
  'switch.one',
  'switch.two',
  'switch.twelve'
  ] %}
  {{ expand(lights)
    | selectattr('state','eq','on') | list | count }}

1 Like

Thank you so much! That did the trick!

The structure remains the same, I have only adjusted the content of variable l:


value_template: >
  {% set select = [ 'light.hue_color_candle_1', 'light.light', 'light.snack_strip', 'light.tavolo'] %}
  {% set l = states.light | selectattr('entity_id', 'in', select) | selectattr('state','equalto','on') | list | length %}
  {% set add = states('switch.zigbee_3_gang_switch_switch_1') == 'on' %}
  {{ l + 3 if add else l }}

What is not working?

It’s ok, I’ve managed a little because I’d changed original code, but it’s all ok.

Thank you so much!!!

Hi,

I’m pretty sure I have scrolled down this whole topic, but could not find a working solution.

All my lights are HUE based and the HUE integration creates a groups of lights based on the room the lights are in. See example below:
Scherm­afbeelding 2022-12-01 om 14.15.25

I like to have an entity per room with the number of light are on in the specific room.

Any help is more than welcome.

Hi,

I’m still a beginner at Home Assistant, but give this a try.

Greetings Moss


- platform: template
    sensors:
        friendly_name: "HAL Lights on"
        value_template: >
          {% set lights = [
            'light.piano_lamp',
            'light.filament_lamp',
            'light.vide',
            'light.hal',
            'light.plafond',
            ] %}
          {{ expand(lights) | selectattr('state','eq','on') | list | count }}

Alternatively, you can use a loop:


        {% set group = 'light.hal' %}
        {% set lights = state_attr(group, 'lights') |list %}
        {% set x = namespace(members=[]) %}
        {% for hue in lights %}
          {% set x.members = x.members + ['light.' ~ hue|slugify] |list %}
        {% endfor %}
        {% set on = expand(x.members) |selectattr('state', '==', 'on') |list |count %}
        {{ on }}

1 Like

Thanks,
But this way I have to code al the (HUE)lights (50+) and they are in a HEU light group allready

@Moss

I tried this, but the number of active lights stay 0.
The names of the lights listed in the attributes are not the entity names but the friendly names, I assume that’s the problem.

Based on your example, light.hal represents a Hue room containing four lights that are listed in its lights attribute:

  1. Piano Lamp
  2. Filament Lamp
  3. Vide
  4. Hal Plafond

You want to show a count of how many of those 4 lights are on?

The challenge is, as you said, that they are listed by their friendly_name and not by their entity_id.

Is the entity id of your HUE group light.hal ?

This is my HUE group:

The friendly names will be „converted“ to entity ids.

When you copy the code in Developer Tools —> Template and add


{{ x.members }}

at the end, what is the output?

Copy-paste the following template in the Template Editor and confirm it reports the correct number of lights that on in the Hal room.

{{ states.light | selectattr('name', 'in', state_attr('light.hal', 'lights') | list)
  | selectattr('state', 'eq', 'on') | list | count }}