Skip to content

Instantly share code, notes, and snippets.

@amitkeret
Created November 3, 2021 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitkeret/3d5febc3fb1bcf9d0e0034e5904eef9d to your computer and use it in GitHub Desktop.
Save amitkeret/3d5febc3fb1bcf9d0e0034e5904eef9d to your computer and use it in GitHub Desktop.
Home Assistant - Motion-controlled multiroom speakers with manual override
# Inspired by PIR_light.yaml by @vanceb
# @see https://gist.github.com/vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26
blueprint:
name: Music on motion (OwnTone)
description: Turn on (= join the group) a OwnTone-controlled media player on motion detection
domain: automation
input:
owntone_source:
name: OwnTone server
description: OwnTone server entity
selector:
entity:
integration: forked_daapd
owntone_target:
name: OwnTone target
description: OwnTone-controlled media player to turn on
selector:
entity:
integration: forked_daapd
presence_entity:
name: Motion sensor entity
description: A presence sensor to trigger the automation
selector:
entity:
domain: binary_sensor
device_class: motion
no_presence_delay:
name: Delay time
description: Time delay to stop media after last presence is detected
default: 5
selector:
number:
min: 0
max: 300
step: 5
unit_of_measurement: minutes
mode: restart
max_exceeded: silent
variables:
presence_entity: !input presence_entity
owntone_target: !input owntone_target
no_presence_delay: !input no_presence_delay
trigger:
- platform: state
id: motion on
entity_id: !input presence_entity
to: 'on'
- platform: state
id: music start
entity_id: !input owntone_source
to: playing
condition:
# Two scenarios for which we'd need to turn on the media player
- condition: or
conditions:
# 1) Motion detected while music is playing
- condition: and
conditions:
- condition: trigger
id: motion on
- condition: state
entity_id: !input owntone_source
state: playing
# 2) Playback started while presence detected
- condition: and
conditions:
- condition: trigger
id: music start
- condition: state
entity_id: !input presence_entity
state: 'on'
action:
- service: media_player.turn_on
target:
entity_id: !input owntone_target
# Turn player off when presence no longer detected
- wait_for_trigger:
platform: state
entity_id: !input presence_entity
to: 'off'
# Wait until presence no longer detected for the user-defined time
- delay:
minutes: !input no_presence_delay
- service: media_player.turn_off
target:
entity_id: !input owntone_target
# The manual-override automation (MOA) needs to know if a media player was turned on/off manually or from here
# By adding this delay, will allow MOA to detect this automation's "current" attribute
- delay: 1
# Inspired by PIR_light.yaml by @vanceb
# @see https://gist.github.com/vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26
blueprint:
name: Music on motion (OwnTone) manual override
description: Override automated music playback when player is manually turned on/off
domain: automation
input:
owntone_source:
name: OwnTone server
description: OwnTone server entity
selector:
entity:
integration: forked_daapd
owntone_target:
name: OwnTone target
description: OwnTone-controlled media player to watch
selector:
entity:
integration: forked_daapd
auto_script:
name: Music on motion script
description: Select the automation affecting the media player above
selector:
entity:
domain: automation
reenable_after:
name: Re-enable automation delay
description: Time delay to re-enable automated behaviour after manual turn on/off
default: 120
selector:
number:
min: 15
max: 1440
step: 5
unit_of_measurement: minutes
mode: restart
max_exceeded: silent
variables:
owntone_target: !input owntone_target
auto_script: !input auto_script
reenable_after: !input reenable_after
trigger:
# Any change to the state of the media player (on/off)
# Made more specific to stop trigger by attribute changes
- platform: state
id: player on
entity_id: !input owntone_target
to: 'on'
- platform: state
id: player off
entity_id: !input owntone_target
to: 'off'
condition:
- condition: state
entity_id: !input owntone_source
state: playing
action:
# In case the player was turned off, a little hack:
# Delay the script by 1 second longer than the auto-behaviour automation, and check the "current" value:
# 0 means the "player_off" request came from it (and then it concluded)
# 1 means it is still running, probably during "delay" step, therefor "player_off" request was manual
- choose:
- conditions:
- condition: trigger
id: player off
sequence:
- delay: 2
- condition: or
conditions:
- condition: and
conditions:
- condition: trigger
id: player on
- condition: template
value_template: "{{ is_state_attr(auto_script, 'current', 0) }}"
- condition: and
conditions:
- condition: trigger
id: player off
- condition: template
value_template: "{{ is_state_attr(auto_script, 'current', 1) }}"
# Disable auto-behaviour and resume when "reenable_after" expires
- service: automation.turn_off
entity_id: !input auto_script
- delay:
minutes: !input reenable_after
- service: automation.turn_on
entity_id: !input auto_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment