Skip to content

Instantly share code, notes, and snippets.

@justinshuls
Forked from Qonstrukt/sonos_connect_sync.yaml
Last active February 5, 2024 00:26
Show Gist options
  • Save justinshuls/18a99af71e3c56f4cc7951e2378d4a41 to your computer and use it in GitHub Desktop.
Save justinshuls/18a99af71e3c56f4cc7951e2378d4a41 to your computer and use it in GitHub Desktop.
blueprint:
name: Sonos Connect Sync with Receiver Volume Limit offset
description: Sync volume and source control between Sonos Connect and the receiver it's connected to. If a volume limit is set on your receiver this will sync volume properly
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
receiver_volume_limit_percentage:
name: Volume limit set on receiver
description: If a volume limit is set on receiver match that here. This will keep the range in sync with sonos 0-100 even if volume limit lower on receiver
default: 100
selector:
number:
min: 30
max: 100
unit_of_measurement: percentage
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
receiver_volume_limit_percentage: !input receiver_volume_limit_percentage
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"] * (receiver_volume_limit_percentage / 100) }}
- 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: >
{{ 100 / (receiver_volume_limit_percentage / trigger.to_state.attributes["volume_level"]) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment