Skip to content

Instantly share code, notes, and snippets.

@legovaer
Last active December 19, 2023 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save legovaer/b0211044a36411d8089220687ffda1d8 to your computer and use it in GitHub Desktop.
Save legovaer/b0211044a36411d8089220687ffda1d8 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint that joins and leaves Sonos speakers on motion
blueprint:
name: Group Sonos on Motion
description: >
# Group Sonos on Motion
**Version: 1.0**
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.
Let us know what you think of this blueprint and for community support including updates: [Click Here](https://community.home-assistant.io/t/sonos-grouping-and-ungrouping-using-motion-detection/575452)
**The Automation Process:**
- In this example we will use a motion sensor as the trigger but you can use one or multiple [Binary Sensors](https://www.home-assistant.io/integrations/binary_sensor/) 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.
domain: automation
input:
motion_trigger:
name: Trigger Sensor - Binary Sensors *
description: The Sensor/s that will make a Sonos speaker join a group. The trigger can be any
[Binary Sensors](https://www.home-assistant.io/integrations/binary_sensor/) you like.
selector:
entity:
domain: binary_sensor
multiple: true
sonos_source:
name: Sonos source
description: The sonos player which this player will join. Probably located in a different room.
selector:
entity:
integration: sonos
domain: media_player
sonos_target:
name: Sonos target
description: The Sonos player that will be joining the existing group. Probably the player which is located
in the room where the motion has been triggered.
selector:
entity:
integration: sonos
domain: media_player
time_delay:
name: Time Delay
description: The delay time to leave the sonos player in the group after the last trigger is detected.
default: 5
selector:
number:
min: 0
max: 30
step: 0.5
unit_of_measurement: minutes
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.
allow_tv:
name: Allow TV grouping
description: Whether to also group the Sonos when the source is playing TV audio.
default: false
selector:
boolean:
mode: restart
max_exceeded: silent
variables:
motion_trigger: !input motion_trigger
sonos_source: !input sonos_source
sonos_target: !input sonos_target
allow_tv: !input allow_tv
time_delay: !input time_delay
trigger:
- platform: state
entity_id: !input motion_trigger
from: "off"
to: "on"
id: "t1"
- platform: state
entity_id: !input motion_trigger
from: "on"
to: "off"
id: "t2"
- platform: state
entity_id: !input sonos_source
to: "playing"
id: "t3"
- platform: homeassistant
event: start
id: "t4"
- platform: event
event_type: automation_reloaded
id: "t5"
condition:
- condition: and
conditions:
# Check if the player is actually playing
- condition: state
entity_id: !input sonos_source
state: "playing"
# Do not trigger the 'action' if our target is already playing some music
- condition: or
conditions:
- condition: state
entity_id: !input sonos_target
state: "paused"
- condition: state
entity_id: !input sonos_target
state: "idle"
# However, re-trigger the 'action' if we are already grouped to prevent early ungrouping
- '{{ sonos_target in state_attr(sonos_source, "group_members") }}'
# When a group is playing TV audio, only join if we allow this
- '{{ allow_tv or state_attr(sonos_source, "source") != "TV" }}'
# Add the custom defined conditions
- condition: and
conditions: !input custom_conditions
action:
- choose:
- alias: "Motion has been detected - group speakers"
conditions:
- condition: trigger
id: 't1'
sequence:
- alias: "Add the speaker to the group"
service: media_player.join
data:
group_members:
- !input sonos_target
target:
entity_id: !input sonos_source
- alias: "Motion has no longer been detected - unjoin after delay"
conditions:
- condition: trigger
id: 't2'
sequence:
- alias: "Wait the number of minutes set in the time delay"
delay:
minutes: !input time_delay
- alias: "Unjoin the speaker from the group"
service: media_player.unjoin
target:
entity_id: !input sonos_target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment