Sonos script join unjoin with need to optimize

I have created a script for our 6 sonos speakers. Is there any way to simplify or shorten this?
Is quite a lot of effort to adapt it to all other 5 speakers and if we add new speakers to our speakers everything must be adapted again.
I have to divide into two posts otherwise I will exceed the allowed number of characters.

Part 1


alias: "Sonos: Gruppierung Roam SL"
sequence:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: media_player.sonos_roam_sl
                state: playing
              - condition: state
                entity_id: binary_sensor.sonos_roam_sl_in_gruppe
                state: "on"
        sequence:
          - service: media_player.unjoin
            data: {}
            target:
              entity_id: media_player.sonos_roam_sl
          - service: media_player.media_pause
            data: {}
            target:
              entity_id: media_player.sonos_roam_sl
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_roam_sl
                    state: paused
                  - condition: state
                    entity_id: media_player.sonos_roam_sl
                    state: idle
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.sonos_roam_sl_in_gruppe
                state: "off"
            then:
              - service: media_player.volume_set
                data:
                  volume_level: 0.1
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.select_source
                target:
                  entity_id: media_player.sonos_roam_sl
                data:
                  source: >-
                    {{ state_attr('sensor.sonos_favorites', 'items').values() |
                    list | random }}
          - if:
              - condition: state
                entity_id: binary_sensor.sonos_roam_sl_in_gruppe
                state: "on"
            then:
              - service: media_player.unjoin
                data: {}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.volume_set
                data:
                  volume_level: 0.1
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.select_source
                target:
                  entity_id: media_player.sonos_roam_sl
                data:
                  source: >-
                    {{ state_attr('sensor.sonos_favorites', 'items').values() |
                    list | random }}
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_roam_sl
                    state: paused
                  - condition: state
                    entity_id: media_player.sonos_roam_sl
                    state: idle
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
        sequence:
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                target:
                  entity_id: media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                target:
                  entity_id: media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_kuche', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                target:
                  entity_id: media_player.sonos_kuche
          - if:
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_terrasse', 'volume_level')
                    }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                target:
                  entity_id: media_player.sonos_terrasse
          - if:
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_wc', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                target:
                  entity_id: media_player.sonos_wc
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_roam_sl
                    - media_player.sonos_esszimmer
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_kuche', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_kuche
          - if:
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_kuche', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_kuche
          - if:
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_terrasse', 'volume_level')
                    }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_terrasse
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_buro


Part 2


          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_kuche', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_kuche
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_wc
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_terrasse
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_esszimmer
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_kuche
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: "{{ state_attr('media_player.sonos_buro', 'volume_level') }}"
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_buro
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.sonos_buro
                    state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
          - if:
              - condition: state
                entity_id: media_player.sonos_buro
                state: playing
              - condition: state
                entity_id: media_player.sonos_esszimmer
                state: playing
              - condition: state
                entity_id: media_player.sonos_kuche
                state: playing
              - condition: state
                entity_id: media_player.sonos_terrasse
                state: playing
              - condition: state
                entity_id: media_player.sonos_wc
                state: playing
            then:
              - service: media_player.volume_set
                data:
                  volume_level: >-
                    {{ state_attr('media_player.sonos_esszimmer',
                    'volume_level') }}
                target:
                  entity_id: media_player.sonos_roam_sl
              - service: media_player.join
                data:
                  group_members:
                    - media_player.sonos_buro
                    - media_player.sonos_esszimmer
                    - media_player.sonos_kuche
                    - media_player.sonos_roam_sl
                    - media_player.sonos_terrasse
                    - media_player.sonos_wc
                target:
                  entity_id:
                    - media_player.sonos_esszimmer
mode: single
icon: mdi:speaker-wireless

Greetings
Moss

It’s challenging for me to definitely state that this script can be optimized, in any significant manner, because it seems to be a large collection of marginally related conditions and actions.

  • If this player is playing and this binary_sensor is on then unjoin another player and pause it.

  • Else if this player is paused or idle but these five players are not playing then if a binary_sensor is off then adjust the volume of another player and set its source to a random favorite. But if the binary_sensor is off then unjoin another player, set its volume and then set its source to a random favorite.

  • Else if this player is paused or idle but at least one of these five players is playing then if another player is playing but not these four players then set the volume of another player and then join it with yet another player. But if that player isn’t playing but some other player is playing and these four players aren’t playing then set the volume of another player and join it with some other player. But if that player is also not playing but another player is and these four players are not then …

The script goes on and on like this, checking the state of various combinations of players and then performs unique actions.

I can suggest a few minor things, to streamline in the templates, but it wouldn’t dramatically reduce the length of this very long script.

Perhaps someone else is willing to invest (a lot) more time to analyze this script and streamline it. Good luck.

Thank you for your reply.

What exactly are you trying to accomplish with all this? not interrupt music that is playing? If that’s the case, use the sonos.snapshot and sonos.restore.