Status indicator for turned on devices

I wanted to create a status indicator which would turn on as soon as at least on or more devices in my home are turned on. Normally I could use the group state of my group group.all_rooms which works fine if you only have lights and devices using the on/off state. Unfortunately I have Sonos speakers which go into paused state as soon as you stop the music.

Therefore I created a binary template sensore which would map on/playing to on. This was quite a hassle to me as it involves a lot of Jinja coding. Unfortunately the template preview in the dev tools of home assistant are quite limited. Sometime I just got a blank output instead of an error message.

configuration.yaml

binary_sensor:
  - platform: template
    sensors:
      something_on:
        friendly_name: "Licht/Gerät an"
        value_template: >-
          {%- macro is_on(entity_id) -%}
            {%- if entity_id.split('.')[0] == 'group' -%}
              {%- set result = namespace(on=false) -%}
              {%- for child in state_attr(entity_id, 'entity_id') -%}
                {%- set result.on = result.on or is_on(child) == 'True' %}
              {%- endfor -%}
              {{- result.on -}}
            {%- else -%}
              {{- is_state(entity_id, 'on') or is_state(entity_id, 'playing') -}}
            {%- endif -%}
          {%- endmacro -%}
          
          {{ is_on('group.all_rooms') }}

I had to create an automation to trigger the sensor update every 5 seconds. Otherwise the template sensor would only be triggered on a state update of group.all_rooms. So far I couldn’t notice any impact on the performance of my Raspberry Pi 3.

automations.yaml

trigger_status_light_update:
  trigger:
    platform: time
    seconds: '/5'
  action:
    service: homeassistant.update_entity
    entity_id: binary_sensor.something_on

Just to complete the configuration, here are my groups.

groups.yaml

all_rooms:
  entities:
    - group.kitchen
    - group.living_room
    - group.bedroom
    - group.hallway
    - group.creative_room
    - group.bathroom

kitchen:
  ...

“Licht/Gerät an” represents my binary sensor. I also added buttons to turn of everything and to turn on/off the heating.

Hi can you help me with my Maxcio Wall switch ?