Skip to content

Instantly share code, notes, and snippets.

@Qonstrukt
Last active May 24, 2024 21:28
Show Gist options
  • Save Qonstrukt/ca1e761b2ec0a2d52fdb8c86490fbcbd to your computer and use it in GitHub Desktop.
Save Qonstrukt/ca1e761b2ec0a2d52fdb8c86490fbcbd to your computer and use it in GitHub Desktop.
blueprint:
name: Sonos Connect Sync
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
receiver:
name: Receiver
selector:
entity:
domain: media_player
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
max: 3600
unit_of_measurement: seconds
sync_volume:
name: Synchronise volume
description: Synchronise volume between Sonos and Receiver.
default: true
selector:
boolean:
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
max: 3000
unit_of_measurement: milliseconds
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
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" }}
- 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"] != receiver_current_volume }}
sequence:
- service: media_player.volume_set
entity_id: !input receiver
data:
volume_level: >
{{ trigger.to_state.attributes["volume_level"] }}
- alias: "Change Sonos volume"
conditions:
- condition: template
value_template: >
{{ trigger.id == "receiver_volume" }}
- 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"] }}
@justinshuls
Copy link

Any way to eliminate delay and work with receiver that has volume limit set? For example if volume limit on the receiver is 80 you could manually set in blueprint than place volume on sonos into passthrough for smoother sync. Current problem is that sonos will always go to 100 and receiver may not making the last 20% a dead zone. perhaps 50% of sonos volume can equate to 40% of receiver if limit set to 80 in blueprint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment