Template Sensor to check how many lights are on 2025

Hi,

I need a bit of help to change the count_lights_on: sensor that I have in the sensor yaml, to make it compatible with the new changes.

 - platform: template
    sensors:
     count_lights_on:
      friendly_name: "All Lights"
      unit_of_measurement: ''
      value_template: >-
        {{ 
          states.light
          | selectattr('state', 'eq', 'on')
          | rejectattr('attributes.is_group', 'eq', true)
          | rejectattr('entity_id', 'is_hidden_entity')
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('object_id', 'search', 'air_quality')
          | rejectattr('entity_id', 'in',  ['light.led_occupancy_gpio_hallway', 'light.tagreader_led']) 
          | list
          | count          
        }}

This still working, but I got a warning that I have to change it, otherwise will stop working.

I tried to replace it with

 - platform: template
    sensors:
     count_lights_on:
      friendly_name: "All Lights"
      unit_of_measurement: ''
      state: "{{ \n  states.light\n  | selectattr('state', 'eq', 'on')\n  | rejectattr('attributes.is_group',
      'eq', true)\n  | rejectattr('entity_id', 'is_hidden_entity')\n  | rejectattr('attributes.entity_id',
      'defined')\n  | rejectattr('object_id', 'search', 'air_quality')\n  | rejectattr('entity_id',
      'in',  ['light.led_occupancy_gpio_hallway', 'light.tagreader_led']) \n  | list\n
      \ | count          \n}}"

But I get a configuration error

Configuration warnings

Invalid config for ‘template’ from integration ‘sensor’ at sensor.yaml, line 71: required key ‘value_template’ not provided Invalid config for ‘template’ from integration ‘sensor’ at sensor.yaml, line 74: ‘state’ is an invalid option for ‘sensor.template’, check: sensors->count_lights_on->state

You still have this in the sensor: integration. It needs to be in template: integration.
Read thru this and then ask further questions there…

There is a custom integration you can install to fix this automagically I believe.

The legacy platform: template syntax for sensor is being removed. Please migrate count_lights_on to the modern template syntax.

Step 1 - Remove legacy configuration

Remove the count_lights_on template definition from the configuration.yaml sensor: section.

Note: If you are using sensor: !include <filename>.yaml in configuration.yaml, remove the sensor definition from the included <filename>.yaml.

Step 2 - Add the modern configuration

template:
- sensor:
  - unit_of_measurement: ''
    default_entity_id: sensor.count_lights_on
    name: All Lights
    state: "{{ \n  states.light\n  | selectattr('state', 'eq', 'on')\n  | rejectattr('attributes.is_group',
      'eq', true)\n  | rejectattr('entity_id', 'is_hidden_entity')\n  | rejectattr('attributes.entity_id',
      'defined')\n  | rejectattr('object_id', 'search', 'air_quality')\n  | rejectattr('entity_id',
      'in',  ['light.led_occupancy_gpio_hallway', 'light.tagreader_led']) \n  | list\n
      \ | count          \n}}"

This is the message.

This stops working in version 2026.6. Please address before upgrading.

I got it fixed. I removed it from sensor.yaml, I added template: !include templates.yaml in config.yaml and created the templetes.yaml and the code is

- sensor:
    - name: "Count Lights On"
      unique_id: count_lights_on
      unit_of_measurement: ""
      state: >
        {{
          states.light
          | selectattr('state', 'eq', 'on')
          | rejectattr('attributes.is_group', 'eq', true)
          | rejectattr('entity_id', 'is_hidden_entity')
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('object_id', 'search', 'air_quality')
          | rejectattr('entity_id', 'in', [
              'light.led_occupancy_gpio_hallway',
              'light.tagreader_led'
            ])
          | list
          | count
        }}

besides the migration, why not add a label to all individual lights, and use label_entities() in the template.
Way easier :wink:

that template is so 2017…

1 Like

Where do you add the label_entities()?

In the UI entities dashboard

I see what you mean. Where it say add lablel? create a label say lights?

Or obsolete AI slurp, regurgitated without consideration of many subsequent changes in HomeAssistant…

You guys saying that my template is obsolete? :grimacing: should I use labels instead?

Well, you could yes.

It will make it a 1 liner.

And yes, label light would do it . You can add more, like inside outside, whatever.

And make easy templates :wink:

{{label_entities('light')
             |select('is_state','on')|list|count}}

or

{{union(label_entities('binnen_lamp'),label_entities('buiten_lamp'))
|select('is_state','on')|list|count}}  

which was done previously like:

{{['binnen_lamp','buiten_lamp']
   |map('label_entities')
   |sum(start=[])
   |select('is_state','on')
   |list|count}}

still works of course :wink:

That is not really required…
Being that unfriendly I mean

1 Like