Sync automatically two asychron echo's from keatontaylor's component

Hello Guys,
first, thanks @keatontaylor for this great component!

I created a feature wich syncs the music or radio between two echos if they are playing the same.
Until now they had an offset of half a second, if they are not using an multiroom group, this was very annoying.

We needed a sensor which looks for what is played at the moment:
sensor.yaml:

  kuche_playing_title:
    value_template: '{{ states.media_player.kuche_2.attributes.media_title }}'
  bad_playing_title:
    value_template: '{{ states.media_player.badezimmer_2.attributes.media_title }}'
  wohnzimmer_playing_title:
    value_template: '{{ states.media_player.wohnzimmer_2.attributes.media_title }}'
  kuche_playing_artist:
    value_template: '{{ states.media_player.kuche_2.attributes.media_artist }}'
  bad_playing_artist:
    value_template: '{{ states.media_player.badezimmer_2.attributes.media_artist }}'
  wohnzimmer_playing_artist:
    value_template: '{{ states.media_player.wohnzimmer_2.attributes.media_artist }}'

When I build those sensors I saw that the Radio station is sometimes displayed in “media_title” and sometimes in “media_artist”. That’s why we have to monitor both states.

Now we have to compare if in two rooms is played the same radio or music.
For this I have build a few binary_sensors.

binary_sensor.yaml:

wohnkuche_match:
"{{ ((states.sensor.wohnzimmer_playing_title.state) == (states.sensor.kuche_playing_title.state) or (states.sensor.wohnzimmer_playing_artist.state) == (states.sensor.kuche_playing_artist.state) or (states.wohnzimmer_playing_artist.state) == (states.kuche_playing_title.state) or (states.wohnzimmer_playing_title.state) == (states.wohnzimmer_playing_artist.state)) and ((states.media_player.wohnzimmer_2.state) == 'playing' and (states.media_player.kuche_2.state) == 'playing') }}"

badwohn_match:
"{{ ((states.sensor.wohnzimmer_playing_title.state) == (states.sensor.bad_playing_title.state) or (states.sensor.wohnzimmer_playing_artist.state) == (states.sensor.bad_playing_artist.state) or (states.sensor.wohnzimmer_playing_artist.state) == (states.sensor.bad_playing_title.state) or (states.sensor.wohnzimmer_playing_title.state) == (states.sensor.bad_playing_artist.state)) and ((states.media_player.wohnzimmer_2.state) == 'playing' and (states.media_player.badezimmer_2.state) == 'playing') }}"

badkuche_match:
"{{ ((states.sensor.kuche_playing_title.state) == (states.sensor.bad_playing_title.state) or (states.sensor.kuche_playing_artist.state) == (states.sensor.bad_playing_artist.state) or (states.sensor.kuche_playing_artist.state) == (states.sensor.bad_playing_title.state) or (states.sensor.kuche_playing_title.state) == (states.sensor.bad_playing_artist.state)) and ((states.media_player.kuche_2.state) == 'playing' and (states.media_player.badezimmer_2.state) == 'playing') }}"

uberall_match:
"{{ (states.binary_sensor.wohnkuche_match.state == 'on') and (states.binary_sensor.badkuche_match.state == 'on') and (states.binary_sensor.badwohn_match.state == 'on') }}"

Those binary_sensors comparing title states with title states, title states with artists states and so on.

Last we need an automation which switches the playing from two stand alone echos to one multiroom group.

automation.yaml:

 - alias: 'Autosync_music'
   initial_state: True
   trigger:
     - platform: state
       entity_id: binary_sensor.wohnkuche_match
       from: 'off'
       to: 'on'
     - platform: state
       entity_id: binary_sensor.badkuche_match
       from: 'off'
       to: 'on'
     - platform: state
       entity_id: binary_sensor.badwohn_match
       from: 'off'
       to: 'on'
   action:
     - service: script.turn_on
       data_template:
         entity_id: >
           {% if is_state('binary_sensor.wohnkuche_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.wohnzimmer_playing_title', '1 LIVE') or is_state('sensor.wohnzimmer_playing_artist', '1 LIVE'))) %}
             script.play_1live_wohnkuche
           {% elif is_state('binary_sensor.wohnkuche_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.wohnzimmer_playing_title', '1LIVE diggi') or is_state('sensor.wohnzimmer_playing_artist', '1LIVE diggi'))) %}
             script.play_1livediggi_wohnkuche
           {% elif is_state('binary_sensor.badkuche_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.bad_playing_title', '1 LIVE') or is_state('sensor.bad_playing_artist', '1 LIVE'))) %}
             script.play_1live_badkuche
           {% elif is_state('binary_sensor.badkuche_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.bad_playing_title', '1LIVE diggi') or is_state('sensor.bad_playing_artist', '1LIVE diggi'))) %}
             script.play_1livediggi_badkuche
           {% elif is_state('binary_sensor.badwohn_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.bad_playing_title', '1 LIVE') or is_state('sensor.bad_playing_artist', '1 LIVE'))) %}
             script.play_1live_badwohn
           {% elif is_state('binary_sensor.badwohn_match', 'on') and not is_state('binary_sensor.uberall_match', 'on') and ((is_state('sensor.bad_playing_title', '1LIVE diggi') or is_state('sensor.bad_playing_artist', '1LIVE diggi'))) %}
             script.play_1livediggi_badwohn
           {% endif %}

The called scripts in automations:

scripts.yaml

  play_1live_badkuche:
    alias: '1Live BadKuche'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live in kuchebad gruppe
          media_content_type: TUNEIN

  play_1livediggi_badkuche:
    alias: '1Live Diggi BadKuche'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live diggi in kuchebad gruppe
          media_content_type: TUNEIN

  play_1live_badwohn:
    alias: '1Live Badwohn'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live in badwohn group
          media_content_type: TUNEIN

  play_1livediggi_badwohn:
    alias: '1Live Diggi BadWohn'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live diggi in badwohn group
          media_content_type: TUNEIN

  play_1live_wohnkuche:
    alias: '1Live kucheWohn'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live in wohnkuche group
          media_content_type: TUNEIN

  play_1livediggi_wohnkuche:
    alias: '1Live Diggi KucheWohn'
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.wohnzimmer_2
          media_content_id: eins live diggi in wohnkuche group
          media_content_type: TUNEIN
1 Like