Help with Scripts

Hi Guys,

I have the following script to play music:

alias: Breakfast Music
  - service: media_player.play_media
      entity_id: media_player.kitchen_living
      media_content_id: FV:2/1
      media_content_type: favorite_item_id
      title: 181.FM Classic Hits 181
      media_class: genre
      children_media_class: null
        - {}
        - media_content_type: favorites
          media_content_id: ""
        - media_content_type: favorites_folder
          media_content_id: object.item.audioItem.audioBroadcast
mode: restart
icon: mdi:music`

How can I update it to then stop the music after 1hr?

I’ve read about the wait_delay options but couldn’t figure them out.

The easiest way is to add two more actions to your script:

  • A 1-hour delay.
  • A service call to stop playing.
  - delay: '01:00:00'
  - service: media_player.media_stop
      entity_id: media_player.kitchen_living

However, as easy as this may be it comes with a drawback. While the delay is counting down the hour, if you restart Home Assistant or execute Reload Automations during the countdown it will cancel the delay and terminate the script. In other words, the script never gets to turn off the media_player.

There’s a way to mitigate this behavior, ensuring the media_player is turned off even after a restart/reload, but it introduces additional complexity (requires a Timer helper and an automation).

This should be fine - thank you for the help.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.