Skip to main content
Smart Sleep: my automation to turn off Music Assistant at just the right time!
  1. Articles/

Smart Sleep: my automation to turn off Music Assistant at just the right time!

·2 mins
Article Music Home Assistant Music Assistant Sonos
Table of Contents

A recent Sonos app update led me to a fantastic discovery: Music Assistant! And it’s been a real blessing, especially for a problem we’ve had for a while.

Remember Sonos’s “auto sleep” function? 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 the auto sleep function, I rolled up my sleeves and created a custom automation. And I tried to make it even “smarter” than Sonos’s option, ensuring the music only turns off at the end of the current song. This way, there’s no risk of abrupt shutdowns in the middle of a track!

Automation Principle
#

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

Related

My Open Source journey in the world of music
·4 mins
Article Music Home Assistant Music Assistant Gonic Sonos
Integrating Daikin Madoka into Home Assistant: the definitive ESPHome solution!
·3 mins
Article Guide Iot Home Assistant Esp32 Esphome
Improve your office (or home) air with a DIY sensor and Home Assistant
·3 mins
Article Guide Iot Home Assistant Esp32 Sensor