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.
The Automation Process:
In this example we will use a motion sensor as the trigger but you can use one or multiple Binary Sensors as the trigger/s.
Triggers on a motion sensor and adds the Sonos speaker (target) in the room to a group (source).
When no motion is detected, a time delay is activated and removes the Sonos player from the group Automatically.
The Sonos speaker will remain GROUPED if the motion sensor detects motion before the time delay removes the Sonos speaker from the group. It will then reset the time delay after last motion is detected.
You have the option to add “Custom conditions”. This allows you to extend this blueprint with more advanced YAML syntax.
You have the option to set the “Allow TV” option. When selected, the target Sonos Speaker will join the group even if the group is playing audio from the TV.
You have the option to adjust the “Time Delay”. This will increase or decrease the amount of time it will take before the target Sonos Speaker will leave the group again.
Im extending this blueprint with an option to set the volume. which worked, but i want to make it pretty and dont change the volume if none has been set.
and that last part i cant get to work. As this is my first work on a script or blueprint in home assistant ever can i get a bit of help?
Is it possible to specify several Sonos speakers here as a source for grouping?
I have 3 rooms and would like to group them one by one as you go into the rooms.
The whole thing should be dynamic, since the room where the music was played changes here and there.
I tried changing multiple to true in the blueprint for source, but it didn’t work.
If you split the blueprint at the moment, the last speaker will simply be grouped out again.
It would be better if the speakers were gradually grouped together and then gradually removed from the group again.
Hi, did you manage to get the “last part” working? If I understand you currently you want the “long press on play” that you can do on each device, right? It will join the last active session. Would be a great feature to have
This just finds the last updated media players entity name. This should be similar to what a long press on the playbutton on a sonos device actually does.
What I fail to understand is how to then use the entity (that I now have as a string) in the blueprint above. Can I make a sensor return an entity? Greateful for all help here. Thanks
I am not using the Motion Detection, but I took some inspiration from the thread and want to give back what I discovered.
This script basically mimics the “long press” feature of the sonos speaker. It is static and needs to be created for each speaker, but it does work so far in my testing and that is more than I had in the morning
The trick is, that the “group-master” is always the first entry in the attribute “group_members”:
alias: Add speaker "bathroom" to active group
description: Add the defined speaker to the most current sonos group
mode: restart
sequence:
- variables:
current_master: |-
{% set players = expand(states.media_player)
| selectattr('state', 'eq', 'playing')
| sort(attribute='last_updated', reverse=true)
| map(attribute='entity_id')
| list %}
{% if players | count > 0 %}
{% set entity_id = players[0] %}
{% set master = state_attr(entity_id, 'group_members') | default([], true) %}
{{ master[0] if master | count > 0 else entity_id }}
{% else %}
{{ none }}
{% endif %}
- if:
- condition: template
value_template: "{{ current_master is not none }}"
then:
- action: media_player.join
target:
entity_id: "{{ current_master }}"
data:
group_members:
- media_player.sonos_roam #this is where you set your speaker
else:
- action: tts.speak
metadata: {}
data:
cache: true
message: >-
Grouping isn't working right now. I couldn't find any active speakers.
media_player_entity_id: media_player.sonos_roam
target:
entity_id: tts.home_assistant_cloud
The else-path is helpful in my case, because I want to control the group via voice assistant.