Select all lights on one floor with a Template

Your comments will break those templates. Comments in Jinja need to be enclosed in comment delimiters {# #}.

As Taras showed in the previous post, there is no need to use all those loops since the area_entities function can be applied across the list of areas using map, and the list can be flattened with sum:

type: custom:mushroom-template-card
primary: Ground Floor
icon: >
  {% set x = floor_areas('ground_floor') | map('area_entities') | sum(start=[])
  | select('match', 'light') | select('is_state', 'on') | list  | count %}
  {{ 'mdi:lightbulb-on' if x > 0 else 'mdi:lightbulb-off' }}
icon_color: >-
  {% set x = floor_areas('ground_floor') | map('area_entities') | sum(start=[])
  | select('match', 'light') | select('is_state', 'on') | list  | count %}
  {{ 'yellow' if x > 0 else 'gray' }}
secondary: >
  {% set x = floor_areas('ground_floor') | map('area_entities') | sum(start=[])
  | select('match', 'light') | select('is_state', 'on') | list | count %}
  There are {{ x }} lights on the ground floor, {{user}}!
tap_action:
  action: call-service
  service: script.light_ground_floor_off
  target: {}
multiline_secondary: false
hold_action:
  action: call-service
  service: script.light_ground_floor_on
  target: {}
1 Like