This blueprint automatically groups a Sonos to a main device when presence (e.g. motion) is detected.
---
blueprint:
name: Group Sonos on Motion
description: >
Group a Sonos device to a main device when presence (e.g. motion) is detected and
the main device is playing music. Can be extended with custom conditions, for
example, only group the Sonos when you're not asleep
domain: automation
input:
presence_entity:
name: A sensor to detect presence (can be a motion sensor)
selector:
entity:
sonos_source:
name: Sonos source
description: Sonos entity to group the target Sonos to
selector:
entity:
integration: sonos
sonos_target:
name: Sonos target
description: Sonos target to group to the source Sonos
selector:
entity:
integration: sonos
no_presence_wait:
name: Wait time
description: Time to leave the sonos grouped after last presence is detected.
default: 120
selector:
number:
min: 1
max: 3600
unit_of_measurement: seconds
custom_conditions:
name: Custom conditions
default: []
description: >
A list of custom condititions that also have to be met before grouping the
sonos. Only available via YAML mode
group_on_tv:
name: Group on TV
description: >
Whether to also group the Sonos when the source is playing TV audio
default: false
selector:
boolean:
mode: restart
max_exceeded: silent
variables:
sonos_source: !input sonos_source
sonos_target: !input sonos_target
group_on_tv: !input group_on_tv
trigger:
- platform: state
entity_id: !input presence_entity
to: "on"
- platform: state
entity_id: !input presence_entity
to: "off"
for:
seconds: !input no_presence_wait
- platform: state
entity_id: !input sonos_source
to: "playing"
- platform: homeassistant
event: start
- platform: event
event_type: automation_reloaded
action:
- choose:
- conditions:
- condition: state
entity_id: !input presence_entity
state: "on"
- condition: state
entity_id: !input sonos_source
state: "playing"
- condition: or
conditions:
- condition: state
entity_id: !input sonos_target
state: "paused"
- condition: state
entity_id: !input sonos_target
state: "idle"
- '{{ group_on_tv or state_attr(sonos_source, "source") != "TV" }}'
- condition: and
conditions: !input custom_conditions
# Only join when we have not already joined,
# since this would cause a 1 sec silence
- '{{ sonos_target not in state_attr(sonos_source, "group_members") }}'
sequence:
- service: media_player.join
data:
group_members:
- !input sonos_target
target:
entity_id: !input sonos_source
- conditions:
- condition: state
entity_id:
- !input presence_entity
state: "off"
for:
seconds: !input no_presence_wait
sequence:
- service: media_player.unjoin
data:
entity_id: !input sonos_target
default: []