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