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: []
If Iām reading this right, I need to set the Sonos target and Sonos source as well as motion sensor.
How would it work for this scenario?
Say I want to turn on music in bedroom when I walk into it. Sonos target = bedroom; sensor = bedroom sensor.
If I have music playing in the living room and walk into the bedroom then logic would dictate that I would set my automation source to living room. But say Iām playing music in the office and then walk to the bedroom, this wonāt work because source was living room instead of office.
Is there a way to make this with a more generic source?
I guess you would need to create a second automation based on this blueprint where youād use the office as a source and the bedroom as a target. Note that the blueprint only joins if the source is actually playing music. The reason the blueprint does not support multiple sources is so you have the flexibility to give priority to one of your sources. Letās assume that you use two sources (office and living room) and one target (bedroom) and both of them are playing different music, how would the blueprint know where to join the bedroom to? By using two automations based on this blueprint, you can easily determine this yourself by passing an additional ācustom_conditionā.
Thanks for this. Iām setting it up now. Like you said, Iāll set-up multiple automations to cover each piece.
I think the next progression is to make like a āfollow-youā based on presence. So if you go from living room to bedroom, it starts playing in bedroom (this is what you already have). If you leave bedroom, it stops playing in bedroom (you already have this). But if you stay in bedroom for a while without presence in living room, it stops playing in living room.
Edit: My bigger issue is that apparently my Sonos speakers donāt seem to update their status within Home Assistant most of the time.
this automation is great, but i have one issue i canāt figure out. when the kitchen (source) is already grouped with the living room, and i walk into the bathroom (target), the music in the kitchen stops. But when the living room is grouped with the kitchen, and i walk into the bathroom, everything stays on. any ideas?
Iāve tried importing this blueprint but get āwhile scanning for the next token found character ā' that cannot start any token in "<unicode string>", line 49, column 11: to: 'on' ^ā.
Has anybody had any luck grouping their target speakers with a Playbar playing TV audio as the source?
Iāve setup this blueprint to use Play speaker to Play speaker, but have no luck with TV Playbar to Play speaker. The sensor detects the motion but no action happens.
To make things more confusing, if I click the āRun Actionsā button it works perfectly, but motion wonāt make this one run.
In case you need my setup, Iāve listed it below.
alias: Group Sonos on Motion
description: ''
use_blueprint:
path: homeassistant/sonos_followme.yaml
input:
presence_entity: binary_sensor.kitchen_motionsensor
sonos_source: media_player.playbar
sonos_target: media_player.kitchen_sonos
custom_conditions: []
The aim is to have the kitchen speaker group to the Playbar (TV) and play the tv audio into the kitchen when weāre in there. At the moment the tv volume gets turned up too high so it can be heard in the kitchen.
You mentioned that the blueprint does not group when the tv is the source so I get the feeling this is intended. If I were to remove something like the like below do you think I could achieve the grouping after?
Yes this was intended, because I only wanted to use this blueprint to have music follow me around the house. Removing the line you supplied would get you the desired effect. I will make this configurable in a new version of the blueprint.
Terrific blueprint. This is a great idea. A couple quick suggestions: I also modified to remove the binary_sensor filter. Modified to remove the āTVā filter line. And I also learned that a zero wait_time did not work. So I made my wait_timeās 1 second. I point all of my grouping automations back to a single sonos as the source, then play everything on that source. Makes playing music in the house so simple .
Thanks for sharing this, @MarvinS! This sounds like an awesome idea, but I havenāt been able to get it to work yet. Iām both new to Home Assistant and also a coding rookie, so please forgive me if Iām missing something obvious, but Iām trying to import directly from the URL:
And Iām receiving this error:
while scanning a simple key in "<unicode string>", line 107, column 1: </code></pre> ^ could not find expected ':' in "<unicode string>", line 108, column 1: <p>EDIT: Fixed early ungrouping<br> ^
Iāve been able to get other blueprints to import via sometimes included import buttons or using a GitHub link, but this one wonāt work for me. Let me know if Iām doing something wrong or you have any other helpful tips. Appreciate it!