How to exclude Hidden Entities in entity count sensor (number of lights on) or Zigbee2MQTT group filter

I use a template sensor to count the number of lights on:


sensor:
- platform: template
  sensors:
    count_lights_on:
      unique_id: count_lights_on
      friendly_name: Home Lights On
      value_template: >-
        {{ states.light 
          | rejectattr('attributes.is_group', 'eq', true)
          | rejectattr('attributes.entity_id', 'defined') 
          | selectattr('state', 'eq', 'on')
          | list | count }}

This worked when I was using Deconz (which adds an attibute is_group for Deconz Groups). But I switched to Zigbee2MQTT so Zigbee groups no longer have this attribute and the resulting count is too high.
As the Zigbee groups are Hidden entities (Hidden in the entity properties) as they are part of a Home Assistant Light Group (to also include non-Zigbee lights) I have 2 options of filtering the entities:

  1. Filtering by an attribute exposed by Zigbee2MQTT. Is there an attribute for Zigbee Groups exposed?
  2. Filtering the Hidden entities.
    I do not know how to do either of them. Can anyone help? Any help appreciated.

Exactly the same problem here and would appreciate if someone could guide me/us to a solution.

Hi @kaosmagix Christian, I was wondering if you were ever able to solve this. I have a similar issue in my template counter, it is adding individual lights of a group and the group itself, in my case a light fixture with 3 bulbs are grouped into one group in home assistant. The group provided by zigbee (Hue) includes these lights as well so in my case the counter tells me 5 lights are on, while what I want it to tell me is only 1 light is on (the ceiling light with 3 bulbs)

light.group_provided_by_hue
light.ceiling_1
light.ceiling_2
light.ceiling_3
light.group_homeassistant_ceiling

template.sensor_light_count = 5 while I only want to see 1

No, I didn’t find a way of doing this yet. For the moment I changed my naming to filter out entites ending with _1, _2, etc. and the naming convention I used for the Zigbee groups.

I still hope to find a way to filter in a proper way (not depending on the entity name).

         value_template: >-
           {{ states.light 
             | selectattr('state', 'eq', 'on')
             | rejectattr('attributes.entity_id', 'defined') 
             | rejectattr('entity_id', 'search','.*_lights') 
             | rejectattr('entity_id', 'search','.*_\d+$') 
             | list | count }}

Thank you @koasmagix. Your response kind of gave my a thought, however it is not the easiest way, I would be able to add light group or something unique to each light entity that I want to count, or simply add the extension _count to each light entity that I want included. Very cumbersome, but it might work. Need to rethink if this is what really adds value to my monitoring though…

It would look like this:
light.group_provided_by_hue
light.ceiling_1
light.ceiling_2
light.ceiling_3
light.group_homeassistant_ceiling_include_in_count

If I then filter the lightcount template sensor by ‘*_include_in_count’ it would provide me
template.sensor_light_count = 1 for this instance.

In version 2023.4.0, a new Jinja2 filter was added that can help you determine if an entity is hidden.

Here’s how you can use it to reject hidden entities:

sensor:
- platform: template
  sensors:
    count_lights_on:
      unique_id: count_lights_on
      friendly_name: Home Lights On
      value_template: >-
        {{ states.light 
          | rejectattr('attributes.is_group', 'eq', true)
          | rejectattr('attributes.entity_id', 'defined')
          | rejectattr('entity_id', 'is_hidden_entity') 
          | selectattr('state', 'eq', 'on')
          | list | count }}
1 Like

This looks like the solution to my issue. I will check if this resolves my issue. Also good example to test the new GUI Template helper with :wink:

1 Like