Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mediacutlet/6db996a57988366ca8269ffb98943971 to your computer and use it in GitHub Desktop.
Save mediacutlet/6db996a57988366ca8269ffb98943971 to your computer and use it in GitHub Desktop.
Home Assistant Automation: When motion is detected in the kitchen around dinner time, offer to play music — but only under very specific conditions.
alias: "Lorelai: Spotify Playlist Dinner Time"
description: "Plays music when motion is detected at dinner time under specific conditions."
trigger:
- platform: device
type: motion
device_id: device_kitchen_motion
entity_id: binary_sensor.kitchen_motion
domain: binary_sensor
for:
seconds: 5
condition:
# Only run between 5:45 PM and 7:45 PM on any day
- condition: time
after: "17:45:00"
before: "19:45:00"
# No music should currently be playing on any media device
- condition: not
conditions:
- condition: or
conditions:
- condition: state
entity_id: media_player.kitchen_echo
state: playing
- condition: state
entity_id: media_player.kitchen_speaker
state: playing
- condition: state
entity_id: media_player.house_group
state: playing
- condition: state
entity_id: media_player.spotify_user
state: playing
- condition: state
entity_id: media_player.living_room
state: playing
# Cooldown: only allow this automation to run if it's never triggered or at least 4 hours have passed
- condition: template
value_template: >-
{{ state_attr('automation.spotify_playlist_dinner_time', 'last_triggered') is none or
as_timestamp(now()) - as_timestamp(state_attr('automation.spotify_playlist_dinner_time', 'last_triggered')) > 14400 }}
# Ensure dining room TV is off (measured by plug power draw)
- condition: numeric_state
entity_id: sensor.dining_room_tv_power
below: 60
# Living room media plug must not be in use
- condition: state
entity_id: binary_sensor.living_room_media_plug
state: "off"
# Double-check Spotify isn't already playing
- condition: state
entity_id: media_player.spotify_user
state: "paused"
action:
# Send Alexa voice prompt asking to play music
- service: script.send_alexa_actionable_notification
data:
text: >-
<speak><voice name='Joanna'>Good evening, would you care to listen to
some music?</voice></speak>
event_id: actionable_notification_dinner_music
alexa_device: media_player.kitchen_echo
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment