Is there a better way to do this (binary template sensor with multiple entities)?

I’m using this code to change an icon on my main home status page. I’m in no way a template expert but feel this can’t be the optimal way. Wondering if I can use the value_template result to trigger the icon state?

binary_sensor:
  - platform: template
    sensors:
      lights_on:
        friendly_name: "Lights are On"
        device_class: light
        value_template: >-
          {{ is_state('switch.buffet_receptacl_switch',       'on') 
          or is_state('switch.coach_lights_switch',           'on')
          or is_state('switch.lanai_fan_switch',              'on') 
          or is_state('switch.kitchen_counter_lights_switch', 'on') }}  
        icon_template: >-                                                                                                                                 
          {% if is_state('switch.buffet_receptacl_switch',       'on') 
             or is_state('switch.coach_lights_switch',           'on') 
             or is_state('switch.lanai_fan_switch',              'on') 
             or is_state('switch.kitchen_counter_lights_switch', 'on') %}   
            mdi:lightbulb-on
          {% else %}                                                                                                                                   
            mdi:lightbulb-outline
          {% endif %}

I’d make a group containing those lights and use it in the templates… That way if you want to add more lights later just add them to the group.

1 Like

Ah, can I use is_states with a group? Assume if one is true it evaluates to true?

Correct. A group is an “or” so any single light on in the group will cause the group to report on. That would cover your use case above.

is_state('group.all_lights','on')

1 Like

Awesome. Thanks!

By default a group, all_lights is created so if you want to act on any and all lights it’s the one to use. I also define groups per room so it’s easy to turn off anything left on from a per-room perspective, and also groups per floor.