Add Attribute to Template State Sensor

If it’s possible, how can I add attributes to this template sensor to tell me which lights are on (Matches condition for sensor)? When I configure the sensor, at the bottom the Preview says “This template listens for the following state changed events:” And then lists the entities it’s counting. My goal is for the sensor to tell me how many lights are on, which is does, but then show me which lights are on when I open the sensor to see more information.

{%- set search_state = 'on' %} 
{%- set search_areas = ['Living Room', 'Bedroom', 'Front Door', 'Bathroom','Garage','Office','Kitchen'] %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) %}
  {%- for area in search_areas %}
    {% if area_name(light.entity_id) == area not in state_attr(light.entity_id, "friendly_name") %}
      {%- set ns.lights = ns.lights + [ light.entity_id ] %}
    {% endif%}
    {%- endfor %}
{%- endfor %}
{{ ns.lights| list | length }} 


image

The Template Helper does not currently support advanced features like availability, templated attributes, or triggers. If you want to use those features you need to configure your sensor in YAML.

1 Like

Use this template to report the number of lights that are on in the seven areas.

{{ ['Living Room', 'Bedroom', 'Front Door', 'Bathroom','Garage','Office','Kitchen']
  | map('area_entities') | sum(start=[])
  | select('match', 'light') | map('states')
  | select('eq', 'on') | list | count }}

Use this template to report a comma-delimited list of friendly_names for all lights that are on in the seven areas.

{{ ['Living Room', 'Bedroom', 'Front Door', 'Bathroom','Garage','Office','Kitchen']
  | map('area_entities') | sum(start=[])
  | select('match', 'light') | expand
  | selectattr('state', 'eq', 'on')
  | map(attribute='name') | list | join(', ') }}

Copy-paste both templates into the Template Editor and confirm they report the desired results. Use them in a Template Sensor configured in YAML.

Be advised that if you have any Light Groups or Philips Hue groups then additional filters are needed in the templates to prevent double-counting.

3 Likes

Thank you! This is exactly what I was looking for, I additionally used Browser Mod to make the tap action for the number entity display the list entity.

2 Likes