Stop media_player after X minutes

Hi everyone,

I’m trying to find a way to stop the music playing on a chromcast audio after 1h30.
But, only if we’re between specific hours when the music should be stopped.

Basically, I want to go to bed, launch the music, then it should stop after some time.
But I don’t want the music to stop if I’m listening it during the day or going to sleep early, etc…

Example :

xx:xx - STOP at 01:00
19:00 - STOP at 01:00
22:00 - STOP at 01:00
23:00 - STOP at 01:00
23:30 - STOP at 01:00 (+ 1:30)
23:45 - STOP at 01:15 (+ 1:30)
00:00 - STOP at 01:30 (+ 1:30)
01:30 - STOP at 03:00 (+ 1:30)
02:00 - STOP at 03:30 (+ 1:30)
02:30 - STOP at 04:00 (+ 1:30)
03:30 - STOP at 05:00 (+ 1:30)
xx:xx - STOP at 05:00
05:00 - STOP at 01:00
xx:xx - STOP at 01:00
22:00 - STOP at 01:00

I can’t make the conditions work :frowning:

Thx for you help !
amans

So this will require some work but you’ll basically have to create a timer and an automation. Then you need to decide how to handle transition times. For example, what happens if you start playing music at 23:29, do you want that to cut off in an hour and a half?

Also, this is going to get complicated unless someone else has better ideas.

If you have entity_id’s I can help you code this. But the flow should be:

automation 1:

trigger off turning on music.
check to see if time+1.5 hours is inside time window of 23:30 to 5:00.
If the time falls in window, start timer for 1.5 hours.

automation 2:

when timer stops
turn off music

The hardest part about this will be the condition for the first automation. Checking time can be painful when you need to ADD minutes to the current time.

The way I would approach this is to create an automation that is triggered when you start playing music. In the action it would set an input_number to the timestamp of when you want the music to stop. Then have a second automation that is triggered at that time that stops the music. It might look something like this:

input_number:
  stop_music:
automation:
  - alias: Set time to stop music
    trigger:
      # Put trigger here for music starting...
    action:
      service: input_number.set_value
      entity_id: input_number.stop_music
      data_template:
        value: >
          {% set n = now() %}
          {% set h = n.hour + n.minute/60 + n.second/60/60
                     + n.microsecond/60/60/1000000 %}
          {% if 5 <= h <= 23.5 %}
            {{ n.replace(hour=1, minute=0, second=0, microsecond=0)
                .timestamp() + 24*60*60 }}
          {% elif 3.5 <= h < 5 %}
            {{ n.replace(hour=5, minute=0, second=0, microsecond=0)
                .timestamp() }}
          {% else %}
            {{ n.timestamp() + 1.5*60*60 }}
          {% endif %}
sensor:
  - platform: time_date
    display_options:
      - 'date_time'
automation:
  - alias: Stop music
    trigger:
      platform: template
      value_template: >
        {{ states('sensor.date__time') == 
           states('input_number.stop_music')
             |float|timestamp_custom('%Y-%m-%d, %H:%M') }}
    action:
      # Put service call here that stops music

One potential flaw is if is not running when the stop time comes. When HA starts again the trigger will have been missed. You could add an automation that is triggered when HA starts and has a condition that (somehow) determines if the time was missed, but it’s probably not worth it. :slight_smile:

I think me mistakingly said he wanted to turn it off in an hour between 5 and 23.5 (in his example). I think he’s only aiming to have the ‘turn off’ sequence occur between 23.5 and 5. All other times he doesn’t want it to auto turn off. But I could have read that wrong.

I don’t think he meant an hour. The way I read it was 01:00 meant one o’clock in the morning, not “in one hour.” I think the list he showed meant if turned on between 5 in the morning and 11:30 at night then it should turn off at 1:00 the following morning, and if turned on between 3:30 and 5:00 then it should turn off at 5:00 in the morning, and only between 11:30 at night and 3:30 in the morning should it turn off an hour and a half later.

ah yes, I read that wrong

Hi guys,

Thx for your help.
To try to clarify :slight_smile:
If I launch the music between 5h00 in the morning and 23h30, it should stop at 1h00 in the morning.
Otherwise it should stop 1h30 after I launched it.
It was maybe easier to say it like this ^^

I’ve never used input_number.
I didn’t think to use it :slight_smile:

Yah I miss interpreted your response. My method will not work unless you do some fancy footwork in the automation that stops the music.

The solution from @pnbruckner seems to work.
But I have another problem :frowning:
The status of the chromecast keeps toggling between playing and iddle

Chromcast audio issue ?
Home Assistant issue ? Pi3 not enough powerful ?
Network / Wi-Fi issue ?

Thx

No idea. I happen to have a Chromecast on the network, which discovery found, but I don’t really use from HA, and I’m always seeing errors from it. There were these this morning:
image
Sometimes I don’t see any for a while, sometimes I see more. Sometimes I seem them when there are other obvious network issues, and sometimes by themselves (like this morning.) FWIW, I’m running HA on a Pi 3B via WiFi.

If the Idle states while Playing are short, you could use some sort of filtering mechanism, although I’m not sure what would work best. Probably others have had this problem, too, so I’d search around the forum for solutions. Or you might want to start a new topic to see if someone can suggest something.