Skip to content

Instantly share code, notes, and snippets.

@finder39
Forked from Qonstrukt/sonos_connect_sync.yaml
Last active October 27, 2023 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finder39/40d5a12ddc55e4b3e5cbd32b604f84d5 to your computer and use it in GitHub Desktop.
Save finder39/40d5a12ddc55e4b3e5cbd32b604f84d5 to your computer and use it in GitHub Desktop.
blueprint:
name: Sonos Connect Sync - V2
description: Sync volume and source control between Sonos Connect and the receiver
it's connected to.
domain: automation
source_url: https://gist.github.com/Qonstrukt/ca1e761b2ec0a2d52fdb8c86490fbcbd
input:
sonos_connect:
name: Sonos
selector:
entity:
integration: sonos
domain:
- media_player
multiple: false
receiver:
name: Receiver
selector:
entity:
domain:
- media_player
multiple: false
receiver_source:
name: Receiver source name
description: The source name of the receiver associated with your Sonos.
default: Sonos
selector:
text: {}
turn_off_delay:
name: Receiver turn off delay
description: Delay before the receiver will turn off after Sonos stopped playing.
default: 300
selector:
number:
min: 0.0
max: 3600.0
unit_of_measurement: seconds
step: 1.0
mode: slider
sync_volume:
name: Synchronise volume
description: Synchronise volume between Sonos and Receiver.
default: true
selector:
boolean: {}
sonos_volume_on_power_on:
name: Set receiver to Sonos volume on power on
description: During power on, make sure the receiver is set to the Sonos volume. If the receiver volume changes, change it back to the Sonos volume. This will be for the first 8 seconds.
default: false
selector:
boolean: {}
sonos_volume_on_power_on_buffer_time:
name: Buffer time for turned on receiver set to sonos volume
description: After power on, how long after should, if the receiver volume changes, change it back to the Sonos volume. This will be for the first X seconds.
default: 15
selector:
number:
min: 1
max: 60
unit_of_measurement: seconds
step: 1.0
mode: slider
sync_volume_buffer_time:
name: Buffer time between sync
description: Time to wait before synchronizing the volume between devices.
default: 1000
selector:
number:
min: 500.0
max: 3000.0
unit_of_measurement: milliseconds
step: 1.0
mode: slider
mode: queued
variables:
receiver_entity: !input receiver
receiver_target_source: !input receiver_source
receiver_current_source: '{{ state_attr(receiver_entity, ''source'') }}'
receiver_current_volume: '{{ state_attr(receiver_entity, ''volume_level'') | float(0)}}'
sonos_entity: !input sonos_connect
sonos_current_volume: '{{ state_attr(sonos_entity, ''volume_level'') | float(0) }}'
sync_volume: !input sync_volume
sonos_volume_on_power_on: !input sonos_volume_on_power_on
sonos_volume_on_power_on_buffer_time: !input sonos_volume_on_power_on_buffer_time
receiver_just_turned_on: '{{ expand(receiver_entity)[0].last_changed < now() - timedelta(seconds=sonos_volume_on_power_on_buffer_time) }}'
is_initial_receiver_volume_change: '{{ sonos_volume_on_power_on and not receiver_just_turned_on }}'
trigger:
- platform: state
entity_id: !input sonos_connect
to: playing
id: sonos_playing
- platform: state
entity_id: !input sonos_connect
from: playing
for:
seconds: !input turn_off_delay
id: sonos_paused
- platform: state
entity_id: !input receiver
attribute: source
from: !input receiver_source
id: receiver_source_changed
- platform: state
entity_id: !input sonos_connect
attribute: volume_level
for:
milliseconds: !input sync_volume_buffer_time
id: sonos_volume
- platform: state
entity_id: !input receiver
attribute: volume_level
for:
milliseconds: !input sync_volume_buffer_time
id: receiver_volume
action:
- choose:
- alias: Switch receiver source
conditions:
- condition: template
value_template: '{{ trigger.id == "sonos_playing" }}'
- condition: template
value_template: '{{ receiver_current_source != receiver_target_source }}'
sequence:
- service: media_player.select_source
entity_id: !input receiver
data:
source: !input receiver_source
- delay:
milliseconds: !input sync_volume_buffer_time
- service: media_player.volume_set
entity_id: !input receiver
data:
volume_level: '{{ sonos_current_volume }}'
- alias: Turn off receiver
conditions:
- condition: template
value_template: '{{ trigger.id == "sonos_paused" }}'
- condition: template
value_template: '{{ receiver_current_source == receiver_target_source }}'
sequence:
- service: media_player.turn_off
entity_id: !input receiver
- alias: Switch Sonos playing state
conditions:
- condition: template
value_template: '{{ trigger.id == "receiver_source_changed" }}'
sequence:
- service: media_player.media_pause
entity_id: !input sonos_connect
- alias: Change receiver volume
conditions:
- condition: template
value_template: '{{ trigger.id == "sonos_volume" or (trigger.id == "receiver_volume" and is_initial_receiver_volume_change) }}'
- condition: template
value_template: '{{ sync_volume }}'
- condition: template
value_template: '{{ receiver_current_source == receiver_target_source }}'
- condition: template
value_template: '{{ sonos_current_volume != receiver_current_volume }}'
sequence:
- service: media_player.volume_set
entity_id: !input receiver
data:
volume_level: '{{ sonos_current_volume }}'
- alias: Change Sonos volume
conditions:
- condition: template
value_template: '{{ trigger.id == "receiver_volume" and not is_initial_receiver_volume_change }}'
- condition: template
value_template: '{{ sync_volume }} '
- condition: template
value_template: '{{ receiver_current_source == receiver_target_source }}'
- condition: template
value_template: '{{ trigger.to_state.attributes["volume_level"] != sonos_current_volume}}'
sequence:
- service: media_player.volume_set
entity_id: !input sonos_connect
data:
volume_level: '{{ trigger.to_state.attributes["volume_level"] }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment