[solved] Possible to detect if mediaplayer has been playing in the last 5 minutes?

Is it possible to have an automation which checks at certain times if a mediaplayer has been playing for example in the last 5 minutes?

Thanks for any help.

I mean something like this will trigger when your media player was playing and now has not been playing for at least 5 minutes. Is that what you’re looking for?

automation:
  trigger:
    - platform: state
      entity_id:
        - media_player.my_media_player
      from: "playing"
      for:
        minutes: 5
  action:
    # Whatever you want to do when you're media player hasn't been
    # playing for 5 minutes
2 Likes

Hi @CentralCommand, thank you for stepping in!

What I would like to do is to detect if a mediaplayer was playing before but switched to stop/pause in for example the last 5 minutes.

I think you have put me in the right direction…

  trigger:
  - platform: time
    at: 01:13:00
  - platform: state
    entity_id: media_player.living
    to: paused
    from: playing
    for: 00:05:00

In my example I only specified a from because then it will trigger any time the media player’s state was playing and became anything else, that’s how that type of trigger works. If you also specify a to then it will only trigger if the media player specifically goes from playing to paused for 5 minutes.

If that’s what you want then that works but originally you asked for something which triggers when it had been playing for 5 minutes. A media player that was playing can become paused but can also become idle, off, home, etc., there’s a variety of other possible states depending on the type of media player. If your media player goes from playing to one of those it won’t trigger your automation.

Also not understanding this part at at all:

  trigger:
  - platform: time
    at: 01:13:00

What’s the point of having it trigger specifically at 1:13AM?

Sorry for not explaining it well Mike. :innocent:

I would like that, at certain times, when the mediaplayer has played and went to paused within a certain timeframe, it starts playing again.
That trigger ad 1:13AM was a test but apparently this starts it EVERY time when it has been paused for 5 minutes.

I think you have to use the media player as condition, not as trigger

1 Like

Hi @tenn0, you are right that makes more sense.
I’m searching for a way to detect that there is a state change in that timeframe before the time trigger.

I think you need 2 automations for that. One starts a timer when playback started, the other checks rhe state of the timer at certain times

Hm ok. Like this maybe?

automation:
  trigger:
    - platform: state
      entity_id:
        - media_player.my_media_player
      from: "playing"
      to: "paused"
      for:
        minutes: 5
  condition:
    condition: time
    after: "01:00:00"
    before: "02:00:00"
  action:
    - service: media_player.media_play_pause
      data:
        entity_id: "{{ trigger.entity_id }}"

So this will trigger when the media player has been paused for 5 minutes after playing but only continues to the actions to unpause it if its within a specific time window.

Unless you want it to unpause the media player even if its been paused for a lot longer then 5 minutes when it gets inside the time range. In that case you could switch the trigger and condition to something like this:

automation:
  trigger:
    - platform: time_pattern
      minutes: "/5"
  condition:
    - condition: time
      after: "01:00:00"
      before: "02:00:00"
    - condition: state
      entity_id: media_player.my_media_player
      state: paused
      for:
        minutes: 5
  action:
    - service: media_player.media_play_pause
      data:
        entity_id: media_player.my_media_player

This one will trigger every 5 minutes but will only actually proceed to the action if the media player has been paused for at least 5 minutes. If your range is actually only an hour you can add hours to the time_pattern trigger to be a little more efficient and only trigger every 5 minutes during that specific hour, I wasn’t sure what the range was though.

Note that this will proceed if the media player was paused for 5 minutes regardless of what state it was in before. There’s no way to factor in its previous state unless you use a state trigger so you’d have to use something like the first one to specifically trigger when it goes from playing to paused. But then again, is there anyway for a media player to become paused if it wasn’t playing first? I don’t know what type of media player you have but I’m pretty sure mine can’t do that.

1 Like

Guys, it seems I have solved it: I’m using an Input Boolean - Home Assistant which stores the status of the mediaplayer in an automation at certain times.
In another automation I use this status at the desired times.
Maybe not the best/cleanest option but it does the job.

I was also trying to use templates and timedelta but could not get to a working solution.

Thanks a lot for helping!

1 Like

working on the same issue, can you elaborate on the solution?

Hi Kevin, the solution is in this topic together with the automation.
With what can I help you?

@Nick4 Thank You - I am not following where the solution is.

I have mine working now by one automation following the media player, and it triggers another automation that does what I need. No idea why this is needed.

the solution is marked;

Guys, it seems I have solved it: I’m using an Input Boolean - Home Assistant which stores the status of the mediaplayer in an automation at certain times. In another automation I use this status at the desired times. Maybe not the best/cleanest option but it does the job. I was also trying to use…

Read the section on Input Booleans

How did that map back to the media player? was there a way to follow the Play/Pause?

The code in the threads all seem

Like this maybe?
I think you …

So not sure which worked.

Thank You in advance for the help

So in one automation I check at specific times if a mediaplayer is playing a stream.
If yes, it switches on an input boolean.

In a second automation, the state of this input boolean is used as a condition.