Run script only once

I’ve created a 3 scripts to turn on my AMP via IR when a specific Alexa is being used. One to turn it on when that Alaexa is “Playing”, one to turn it off after 1 minute is it’s in “Pause” state, and one to turn it off if it’s in “stanby” state/

But now what happens is it triggers after it turns off since the trigger is still valid. So is there a way to run the OFF triggers once and then reset them once the ON is triggered?

  - alias: Gym audio ON
    trigger:
      platform: state
      entity_id: media_player.gym
      to: 'playing'
    action:
       service: script.turn_on
       entity_id: 
        - script.gym_audio_power

  - alias: Gym audio Paused
    trigger:
      platform: state
      entity_id: media_player.gym
      to: 'paused'
    action:
       - delay: 00:01:00
       - service: script.turn_on
         entity_id: 
           - script.gym_audio_power
           
  - alias: Gym audio Standby
    trigger:
      platform: template
      value_template: '{{states.media_player.gym.attributes.media_content_type == ''standby''}}'
    action:
       - service: script.turn_on
         entity_id: 
           - script.gym_audio_power

I don’t think that statement is true.

Notice in the trigger definition that the trigger is only true when the state changes “to: ‘playing’”. It doesn’t stay true at any time other than that very instant when the state of the media_player changes to that state. IOW, the trigger doesn’t ever “stay true” it is only true at one instant in time when the state changes.

If the automation is firing again there is something else going on causing the trigger to become true again. That can only happen when the state changes from any other state to “playing”, “paused” or “standby”.

I would say that it might be in the interaction between all of the automations since they all run the same script to turn the amp on.

without knowing the exact contents of the script or the states of your media players when turning it on/off it’s hard to know what is causing the things you’re experiencing.

You could try adding some input_booeans for more control. For instance…when the turn on automation triggers, also turn on a yes_i_am_on boolean, and have a condition that makes sure it was off first. The turn off automation can have a condition that the input_boolean must be on. and also turn it off if the 1 minute has passed and this script gets triggered…verifying the on automation already ran. this may help avoid a temp status change trigger between songs.

1 Like

I coded the following:

 - alias: Gym audio ON
trigger:
  platform: template
  value_template: '{{states.media_player.gym.attributes.media_content_type != ''standby''}}'
action:
   service: script.turn_on
   entity_id: 
    - script.gym_audio_power

  - alias: Gym audio OFF
trigger:
  platform: template
  value_template: '{{(states.media_player.spotify.attributes.source == ''Gym'') and (states.media_player.spotify == ''paused'')}}'
action:
   service: script.turn_on
   entity_id: 
    - script.gym_audio_power

But my value_template: ‘{{(states.media_player.spotify.attributes.source == ‘‘Gym’’) and (states.media_player.spotify == ‘‘paused’’)}}’ doesn’t seem to trigger…

you need to make sure that your quotation marks inside the template are different from those outside:

value_template: '{{ (states.media_player.spotify.attributes.source == "Gym") and (states.media_player.spotify == "paused") }}'

Also make sure that you capitalization is correct. It has to match the state exactly. “Gym” is different than “gym”.

Figured the AND out, had to remove the ( ) that I used to split the 2 conditions with.

Now trying to find the service call that would pause a media_player by changing its state to pause.

I really don’t think it was () that was causing any issues. As far as I know the syntax was correct minus the incorrect quotation marks.

Did you ever figure this out? I can’t seem to pause Spotify with a script or scene no matter what