Template sensor for areas instead of individual lights

I use the below template sensor to report individual lights that are turned on but is it possible to have a template sensor to list the areas that have lights on?

- platform: template
  sensors:
    lights_on:
      friendly_name: "Lights On"
      value_template: "{{ expand('light.lights_all') | selectattr('state', 'eq', 'on') | map(attribute='name') |list }}"

Yes, it does, thank you.

If I could just ask, I’ve tested the template a number of times and, for some reason it’s reporting lights that are on correctly, but every time it’s also reporting a specific light that is not on. I’ve confirmed it’s status is off in Developer Tools >- States but, at the same time the template is showing its state as on.

Is there any part of the template that may be calling an erroneous device/entity?

If not, I will delete the device and re-add it to HA.

I’ve just figured out what the erroneous device is that the template is picking up and for some strange reason it is my zigbee co-ordinator that it is reporting as a light on.

This is a common issue when using Area-based methods… there are often entities that fall in the light domain that you will need to exclude from your template results. These can include indicator LEDs, light groups, and even tablet screens if you use Browser Mod. Depending on the devices you have and/or add, you may spend just as much time refining the template as you would maintaining a group.

Can the template be configured to ignore the zigbee co-ordinator entity to avoid this?

Yes. For ease of future additions, I would setup an exclusion list:

{% set exclude = ['light.example_1', 'light.example_2'] %}
{% set ns = namespace(x = []) %}
{% for a in areas() if area_entities(a) | select('match', 'light') 
| select('is_state', 'on') | reject('in', exclude) | list | count > 0 %}
  {% set ns.x = ns.x + [area_name(a)] %}
{% endfor %}
{{ ns.x | sort | join(', ') }}
1 Like

I can see the issue now. It was picking up all the zigbee lights groups but I’ve excluded each of those now and it reports correctly.

Thanks both for your help with this. Much appreciated.