Sonos join if playing in other room

Hello all

Wanted to share this with you, and ask for your suggestions for optimizing into one
script, as I cannot figure out to make it work in one script 😉
It works perfectly as is, but the clumsiness is disturbing my OCD brain


I have an automation that looks for movement via a Philips Hue motion detector
in my bathroom. What it does is : (explanation via scripting-language)

If movement detected in bathroom
	If Sonos is NOT playing in bathroom 
		Turn on tablet screen with HADashboard via MQTT for 3 minutes
		If music is playing in kitchen
			Join bathroom Sonos to kitchen source, unjoin after 3 minutes with
 no motion
		Else
            Turn on Sonos player with specific source, pause after 3 minutes with no motion
	Else
		Do nothing for 3 minutes with no motion

How do I combine the two automation ID’s “Musik pĂ„ badevĂŠrelse ON - Join KĂžkken” and “Musik pĂ„ badevĂŠrelse ON” into one ?
I guess it another nested “If” loop, but am struggling.
  • id: Musik pĂ„ badevĂŠrelse ON - Join KĂžkken
    alias : Musik pÄ badevÊrelse ON - Join KÞkken
    trigger:
    • platform : state
      entity_id: binary_sensor.motion_badevaerelse_sensor
      to: ‘on’
      condition:
    • condition: time
      after: ‘06:00:00’
      before: ‘21:30:00’
    • condition: time
      weekday:
      • mon
      • tue
      • wed
      • thu
      • fri
      • sat
      • sun
    • condition : template
      value_template: >
      {% if is_state(‘media_player.badevaerelse’, ‘playing’) %}
      false
      {% else %}
      true
      {% endif %}
      action:
      #---------
    • condition: state
      entity_id: media_player.kokken
      state: ‘playing’
    • service: media_player.sonos_join
      data:
      master: media_player.kokken
      entity_id:
      • media_player.badevaerelse
    • service: media_player.volume_set
      data_template:
      entity_id: “media_player.badevaerelse”
      volume_level: 0.10

#----------------------------------------------------------------------------------------------------------------------------------

  • id: Musik pĂ„ badevĂŠrelse ON
    alias : Musik pÄ badevÊrelse ON
    trigger:
    • platform : state
      entity_id: binary_sensor.motion_badevaerelse_sensor
      to: ‘on’
      condition:
    • condition: time
      after: ‘06:00:00’
      before: ‘21:30:00’
    • condition: time
      weekday:
      • mon
      • tue
      • wed
      • thu
      • fri
      • sat
      • sun
    • condition : template
      value_template: >
      {% if is_state(‘media_player.badevaerelse’, ‘playing’) %}
      false
      {% else %}
      true
      {% endif %}
      action:
      #---------
    • condition: state
      entity_id: media_player.kokken
      state: ‘paused’
    • service: media_player.select_source
      data:
      entity_id: media_player.badevaerelse
      source: “P4”
    • service: media_player.media_play_pause
      entity_id: media_player.badevaerelse
    • service: media_player.volume_set
      data_template:
      entity_id: “media_player.badevaerelse”
      volume_level: 0.12
      #----------------------------------------------------------------------------------------------------------------------------------
  • id: Musik pĂ„ badevĂŠrelse OFF
    alias : Musik pÄ badevÊrelse OFF
    trigger:
    platform: state
    entity_id: binary_sensor.motion_badevaerelse_sensor
    to: ‘off’
    for:
    minutes: 3
    condition:
    • condition: time
      after: ‘06:00:00’
      before: ‘21:30:00’
    • condition: time
      weekday:
      • mon
      • tue
      • wed
      • thu
      • fri
      • sat
      • sun
        action:
    • service: media_player.sonos_unjoin
      entity_id: “media_player.badevaerelse”
    • service: media_player.media_pause
      entity_id: media_player.badevaerelse

#----------------------------------------------------------------------------------------------------------------------------------

  • alias: ‘BadTablet Screen On’
    trigger:
    platform: state
    entity_id: binary_sensor.motion_badevaerelse_sensor
    to: ‘on’
    condition:
    • condition: time
      after: ‘06:32:00’
      before: ‘21:30:00’
    • condition: time
      weekday:
      • mon
      • tue
      • wed
      • thu
      • fri
      • sat
      • sun
    • condition : template
      value_template: >
      {% if is_state(‘media_player.badevaerelse’, ‘playing’) %}
      false
      {% else %}
      true
      {% endif %}
      action:
    • service: mqtt.publish
      data:
      topic: ‘wallpanel/badtablet/command’
      payload: ‘{“wake”:true}’

No worries all. I figured this out by my self. I wasnt aware that I could do conditions within the action section of an automation, but it works flawlessly !

Michael