A recent Sonos app update led me to a fantastic discovery: Music Assistant! And it has been a true blessing, especially for a long-standing problem we had.
Do you remember Sonos’s “auto sleep” feature? The one that, once activated, would turn off the music after a certain period? It was super convenient, especially for us parents who used it to help the kids fall asleep, particularly on those evenings when a little extra calm was needed. The problem? My better half sometimes forgot to set the timer, and the music would play all night!
To increase the WAF (or HAF, as you prefer), I decided to take matters into my own hands. Since Music Assistant doesn’t have an auto-sleep function, I rolled up my sleeves and created a custom automation. And I tried to make it even “smarter” than the Sonos option, ensuring that the music only turns off at the end of the currently playing song. This way, there’s no risk of abrupt cut-offs in the middle of a track!
Here’s how it works:
- Trigger: The media player has been playing for 1 hour and 15 minutes.
- Condition: The current time is between 7:30 PM and 7:00 AM.
- Actions:
- Wait a maximum of 15 minutes for the song to finish (title change).
- Turn off the music!
Automation Code#
And for the more technical folks, here’s the YAML code for the automation:
description: ""
mode: parallel
triggers:
- alias: nessie_play
trigger: state
entity_id:
- media_player.nessie
for:
hours: 1
minutes: 15
seconds: 0
id: nessie_play
enabled: true
to: playing
conditions:
- condition: time
after: "19:30:00"
before: "07:00:00"
enabled: true
actions:
- wait_for_trigger:
- trigger: state
entity_id:
- media_player.nessie
attribute: media_title
timeout:
hours: 0
minutes: 15
seconds: 0
milliseconds: 0
- action: media_player.media_stop
metadata: {}
data: {}
target:
device_id: 9bcc2148607323743c51c688b54a7e8d
max: 10
Resilience in case of reboot: the solution with an “automation helper”#
The solution I’ve shown you is a good starting point, but there’s an important detail to consider: what happens if Home Assistant restarts? Unfortunately, the trigger timer based on the media player’s state would be lost, nullifying our automation. To make our solution truly robust and reboot-proof, we can use an “automation helper” (or helper).
Home Assistant helpers, particularly timers, allow us to manage timings more independently and, crucially, to maintain their state even after a reboot. The important thing is to remember to enable the “Restore state on reboot” option in the settings of the timer helper we’re going to create.
This means that our automation will now have two main “events” (triggers) to monitor. The first will be the start of music playback on the player, which will initiate our timer helper. The second trigger will be the expiration of this timer, which will signal the moment to turn off the music. To keep everything clean and manageable within a single automation, we’ll use the id
s of the triggers, allowing us to differentiate actions based on which event triggered the automation.
Here’s how the YAML code for our improved automation looks:
alias: Auto Sleep Timer Sonos
description: ""
triggers:
- alias: nessie_play
trigger: state
entity_id:
- media_player.nessie
id: nessie_play
enabled: true
to: playing
- trigger: event
event_type: timer.finished
event_data:
entity_id: timer.timer_sleep_nessie
id: Timer-nessie
conditions:
- condition: time
after: "19:30:00"
before: "07:00:00"
enabled: true
actions:
- choose:
- conditions:
- condition: trigger
id:
- nessie_play
sequence:
- if:
- condition: state
entity_id: timer.timer_sleep_nessie
state: idle
then:
- action: timer.start
metadata: {}
data: {}
target:
entity_id:
- timer.timer_sleep_nessie
- conditions:
- condition: trigger
id:
- Timer-nessie
sequence:
- wait_for_trigger:
- trigger: state
entity_id:
- media_player.nessie
attribute: media_title
timeout:
hours: 0
minutes: 15
seconds: 0
milliseconds: 0
- action: media_player.media_stop
metadata: {}
data: {}
target:
device_id:
- ff2bbd4dc403c6f1cc829328f8dc5022
mode: parallel
max: 10