Automation to pause Media Player as it starts for 10 seconds

I have an automation that spots when the KODI Media Player is started. This will power on the Amp and swap to AV1. It works well.

Trouble is my AV amp takes a while to warm up, so I want to insert a 10 second pause to make the music wait for ten seconds.

This does not always work. Sometimes it seems to get stuck in the pause and I have to jump a track forward\back to release it. I assume this “play” command after the delay 10 secs is re-triggering this automation and ending up in a loop?

Any suggestions to make this work? Is there a “Resume” command?

Or maybe I can set some kind of variable\flag to let me know I am re-running the automation and I can skip the pause. Does YAML have global variables?

alias: KODI playing, switch Yamaha, pause music if amp off
description: >-
  When PLAY is hit on KODI, swap Yamaha AV channel to AV1

  If Yamaha was OFF then pause KODI for 10 secs while amp warms up, and select
  AV1 channel.

  Else just select AV1 channel
triggers:
  - trigger: device
    device_id: b792eee65153d6e70755a7f0ff9e8e40
    domain: media_player
    entity_id: media_player.kodi_echoes
    type: playing
actions:
  - if:
      - condition: device
        device_id: b3b15e268a956572829d97397791c237
        domain: media_player
        entity_id: e8ca972ce044578d40b2b6814746887e
        type: is_off
    then:
      - action: media_player.media_pause
        metadata: {}
        data: {}
        target:
          device_id: b792eee65153d6e70755a7f0ff9e8e40
      - action: media_player.turn_on
        data: {}
        target:
          device_id: b3b15e268a956572829d97397791c237
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
      - action: media_player.media_play_pause
        metadata: {}
        data: {}
        target:
          device_id: b792eee65153d6e70755a7f0ff9e8e40
    else:
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
    alias: If Amp was off, turn on and pause KODI for 10 secs
mode: single

Why are you pausing twice?
Shouldn’t the second be play?

Originally it was “media_player.media_play”, but that didn’t work. I guessed it was just triggering the automation to come back in and pause again for 10second delay. Ad-infinitum.

So I tried “media_player.media_play_pause” hoping it was an “unpause”, but that also didn’t work.

What you see is my “work in progress” which I think just ends up in a mad loop of constantly retriggering the same automation.

(And how do you make the script coloured in a post?)

It could be that it triggers itself yes.
Try and add a 5 or 10 second delay after everything else in the automation, that should hinder it from triggering again.

You need to pay the color fee to get text in color.

Sorry bad joke. I don’t know, sometimes it is sometimes not.

But what to do after the delay? It is the resume that retriggers the automation, so a delay after that point is not going to change the retrigger. How to restart play without triggering the loop?

I tried a few things…

  • Delay to 30 seconds - still stuck on pause.
  • Amp on, on Radio - works fine. And skips the delay.
  • Hit Forward and Back - pause is released

The test for “is it on” should mean it skips the pause. And some of the above shows that happens as it recognises it is on when playing the radio. I don’t understand why the second time through the automation it would return to this pause as AV Amp should be seen as on.

Is it a formatting error? A bad indent? Delay done wrong? These are next areas I’ll test. Is AV Amp just be slow to register that it is “on” in HA?

And I put my 50p donation in the box for the colour. Also tried both ~~~ and ```

Right… Ok. Sure…
I was so sure that it would with a mode single automation.

Just for the hell of it, I threw another delay at the very end of the script. Still same “never start” happening.

It feels to me like the state is not set to say the AV Amp is on until the first automation is fully completed? How else can it pop back into the first part of the if statement?

Powering up the amp with:

      - action: media_player.turn_on
        data: {}
        target:
          device_id: b3b15e268a956572829d97397791c237

Should set this test to FALSE:

  - if:
      - condition: device
        device_id: b3b15e268a956572829d97397791c237
        domain: media_player
        entity_id: e8ca972ce044578d40b2b6814746887e
        type: is_off

And therefore skip the THEN clause and jump straight to the ELSE.

This is just weird…

I may cut this up a bit into sub-automations to see if that helps.

Ooo - look - my extra 50p donation has been accepted and I am in colour now.

The saga continues. Something is weird about how the AV Amp is tested for the Off State. It just does not seem to update.

A new variation:

alias: KODI playing, switch Yamaha, pause music if amp off
description: >-
  When PLAY is hit on KODI, swap Yamaha AV channel to AV1

  If Yamaha was OFF then pause KODI for 10 secs while amp warms up, and select AV1 channel.

  Else just select AV1 channel
triggers:
  - trigger: device
    device_id: b792eee65153d6e70755a7f0ff9e8e40
    domain: media_player
    entity_id: media_player.kodi_echoes
    type: playing
conditions:
  - condition: device
    device_id: b3b15e268a956572829d97397791c237
    domain: media_player
    entity_id: e8ca972ce044578d40b2b6814746887e
    type: is_off
actions:
  - alias: If AV Amp was off, turn on and pause KODI for 10 secs
    if:
      - condition: device
        device_id: b3b15e268a956572829d97397791c237
        domain: media_player
        entity_id: e8ca972ce044578d40b2b6814746887e
        type: is_off
    then:
      - action: media_player.media_pause
        metadata: {}
        data: {}
        target:
          device_id: b792eee65153d6e70755a7f0ff9e8e40
      - action: media_player.turn_on
        data: {}
        target:
          device_id: b3b15e268a956572829d97397791c237
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
      - delay:
          hours: 0
          minutes: 0
          seconds: 25
      - action: media_player.media_play
        metadata: {}
        data: {}
        target:
          device_id: b792eee65153d6e70755a7f0ff9e8e40
    else:
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
mode: single

There is one difference. Now the “Is the AMP Off” test is moved to one of the Automations Conditions. So this Automation should not even run if the amp is ON.

Makes no difference.

I know this new variation of the automation as written works as when I swap to RADIO and then start KODI it now will not swap the amp over as the amp is not off. That also shows I am using the correct test for “is the amp off?”

Is there a status context issue? Can an automation acknowledge that something has changed start after the automation has started?

I going to need to go learn how to do debug logging.

Are you sure the state changes on the amp that quickly?

I have no reason not to think the update is pretty instant. I have a test page in HA covered with various little things I do with this amp, other people’s GUIs and Players, and hitting the power I get a pretty instant reaction to all of them showing the amp is on.

It is only in this automation do I seem to fail to see it is on.

If it is lost in a loop… then I’d expect if I left it for five minutes then at some point HA would twig the Amp is actually on? And finally break the loop. That never happens.

Sometimes while puzzling this I have left it in limbo state for ages while looking for some other way round it.

This is why I am kinda expecting it to be a dumb typo of mine.

As seen by my last post, I know the AMP state test is functional as I see it tested correctly by flicking from the radio.

Duh - finally read and located “Traces”. Which only goes to confuse things further as an Automation refuses to run twice and aborts the second time round… so it isn’t looping.

Now I am reading the traces, the confusion shows in a different way… :stuck_out_tongue_winking_eye:

I swapped the code to instead of having a second PLAY, to have a FORWARD. Just for testing as it skips the first track. And it means the Automation now successfully Pauses and Plays something.

image
Not sure why the trace shows that… it has paused.

For now I have inserted a hack that works. Instead of “play” I perform the manual hack I had been doing {“mute”, “forward one track”, “delay quarter second”, “previous track”, “unmute”}

Trouble is this doesn’t ever allow me to continue playing from where I left off the day before… so about to get real complex of

  • Find current track time.
  • Mute
  • Wait ten seconds
  • Jump back to that time
  • Unmute

Therefore never actually hitting “pause”… why it no like pause/play? :man_shrugging:

The current slightly sideways working solution for not being able to make Pause work…

alias: KODI playing, switch Yamaha, pause music if amp off
description: >-
  When PLAY is hit on KODI, swap Yamaha AV channel to AV1

  If Yamaha was OFF then pause KODI for 10 secs while amp warms up, and select AV1 channel.

  Else just select AV1 channel
triggers:
  - trigger: device
    device_id: b792eee65153d6e70755a7f0ff9e8e40
    domain: media_player
    entity_id: media_player.kodi_echoes
    type: playing
conditions: []
actions:
  - alias: If AV Amp was off, turn on and pause KODI for 10 secs
    if:
      - condition: device
        device_id: b3b15e268a956572829d97397791c237
        domain: media_player
        entity_id: e8ca972ce044578d40b2b6814746887e
        type: is_off
    then:
      - action: media_player.media_pause
        metadata: {}
        data: {}
        target:
          entity_id: media_player.kodi_echoes
      - action: media_player.turn_on
        data: {}
        target:
          device_id: b3b15e268a956572829d97397791c237
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
      - action: media_player.volume_mute
        metadata: {}
        data:
          is_volume_muted: true
        target:
          entity_id: media_player.kodi_echoes
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
      - action: media_player.media_next_track
        metadata: {}
        data: {}
        target:
          entity_id: media_player.kodi_echoes
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 250
      - action: media_player.media_previous_track
        metadata: {}
        data: {}
        target:
          entity_id: media_player.kodi_echoes
      - action: media_player.volume_mute
        metadata: {}
        data:
          is_volume_muted: false
        target:
          entity_id: media_player.kodi_echoes
    else:
      - action: media_player.select_source
        data:
          source: av1
        target:
          entity_id: media_player.av_amp
mode: single