Total number of lights ON excluding a specific device

Hello,
I’m trying to make a sensor that shows how many lights I have on and searching the forum I found some examples that I copied/pasted in configuration.yaml

The code is working, but there’s one light I’d like to exclude (the 3d printer light)
light.p1s_01p00a3c0700772_chamber_light since it makes the lights count off by 1 so I tried adding a line rejectattr('entity','search','light.p1s_01p00a3c0700772_chamber_light') to exclude it.

This doesn’t work and the count is still off, so I guess it’s wrong. Can anyone help me fix the code, please?

  - platform: template
    sensors:
      number_lights_on:
        friendly_name: Total lights on
        value_template: >-
          {{ states.light
          | rejectattr('attributes.is_deconz_group', 'eq', true)
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('entity','search','light.p1s_01p00a3c0700772_chamber_light')
          | selectattr('state', 'eq', 'on')
          | list | count }}
        icon_template: mdi:lightbulb-group

Use entity_id in your rejectattr search, not entity.

If you paste that template into Developer Tools / Template, it’d give you a warning:

'homeassistant.helpers.template.TemplateState object' has no attribute 'entity'

The UI template sensor helpers are very good now: no need to use configuration.yaml for this sort of thing.

1 Like

In addition to using entity_id instead of entity, I recommend you take this opportunity to convert your Template Sensor from legacy format to modern format.

For more information, refer to this guide:

First of all, thank you.

Use entity_id in your rejectattr search, not entity.

Thank you!
That seems to have fixed it.

The UI template sensor helpers are very good now: no need to use configuration.yaml for this sort of thing.

Yes, I saw that you can make a sensor with helpers UI, but it seems to me that the interface still needs a lot of work: I tried to use it for this case, after your suggestion (I might need to use it in the future) but I couldn’t find a single choice that worked.

Combine the state of several sensors is just for sensors state obviously
Counter is just an empty counter
Group lets you group different devices, but you cannot enumerate them
Number just creates a number
and so on.

Seems to me that adding a number to count an active amount of entities (lights/plugs/computers) shouldn’t be too complicated, but I guess I’m wrong.

After a while I gave up, besides I already fixed the problem, thanks to your suggestion.

I think the UI is gonna be the future, it’s certainly easier than writing some code, but maybe it’s a bit too early to use it (or I wasn’t able to in this case).

Thank you again!

In addition to using entity_id instead of entity, I recommend you take this opportunity to convert your Template Sensor from legacy format to modern format.

For more information, refer to this guide:
Deprecation of legacy template entities in 2025.12

Thank you, I’ll take a look at that in the future, but tbh it scares me a lot the idea of having to rewrite what’s already working and I can potentially bork.

If you’re running a recent release of Home Assistant, you should have already received Repair notices showing how to convert your Template entities.

You have until June to convert them. It would be prudent to start converting them well before the deadline.

Template > Sensor. Then just paste in the template (only the the bits between, and including {{ ... }}) into the State box.

1 Like

Thank you!