How to end an automation stuck on a wait_template?

I know this subject isn’t new and I also now that moves are afoot to generally improve the control of processing between scripts (and automations?). I also know about the timeout parameter of wait_template. But…

How can I cancel an automation that has stopped at a wait_template?

Turning the automation off and then on again doesn’t seem to do it.

As I said, I know this whole subject isn’t new but it also isn’t, as far as I can tell documented anywhere (which is odd given how fundamental this is) but if it is please point me in the right direction.

Turning an automation off simply tells the system not to trigger the automation it does nothing to an active automation. HA restart?

Yeah, I was hoping to be able to do programmatically.

Is there a way to re-write the automation / use scripts / combination there-of to accomplish what you want w/out using a wait_template that’s causing your issue(s)? Maybe post more about what you’re after and we could try to help fix the underlying cause vs. the symptom?

1 Like

Yes I thought about scripts too but that won’t work because…

The automation is at the end of a fairly long chain of ‘events’ (not HA events) and it is triggered by an MQTT payload. The reason it is done this way is because the payload can be longer than 256 chars so I can’t pass it as a variable to a script.

As you asked, here is some background:
This is for my notification engine. I need to know when the Sonos announcement is finished so I have to wait for the media_player to change from idle to playing and back to idle. The underlying cause is that unfortunately the Sonos integration doesn’t always update the state of the media_player so sometimes it just waits forever. I’ve been in contact with the actual (ex)dev of this and he suspects a hidden bug somewhere because it should and usually does, work. So basically I test if the notification is not doing anything for too long and then if it is stop all the scripts so that at least the next one will work. I just can’t stop the automation that is waiting.

And just to clarify, timeout: doesn’t suit because my announcements are usually dynamically constructed so I don’t know how long they are. I have though resorted to a longish timeout to cover most cases but that is hardly ideal.

There is a script.turn_off service. Maybe?

I want to stop/cancel an automation.

Wondering if there’s a way for the automation to trigger a script that uses delays (or maybe repeats itself and uses a service_template to trigger itself or a new script once media_player goes from playing -> idle). Certainly (as you’ve found out) not a real clean solution given the variability…

Another option:
Maybe just set a fixed delay / wait after the notification is played so it always waits for 20 seconds (or the longest notification length you’d send?)

Another option:
Setup a TIMER and use a series of automations/scripts to handle instead of a single automation w/ a wait.

IE.

  • Setup a 20 second TIMER

Then your current notification automation triggers the notification and resets / starts the timer

Then (instead of wait) have another automation with two triggers:

  1. Timer hits 0
  2. media_player goes from playing->idle

The first action is to reset the timer and do the rest of your original notification automation (the part AFTER the wait_template)

Again - none of this is super clean, and mostly I’m just thinking out loud…

As I think has already been suggested, move the steps from the automation’s action into a new script. Then have the automation turn that script off, then start it…

action:
- service: script.turn_off
  entity_id: script.my_new_script
- service: script.my_new_script

Anything that can be done in the action part of an automation can be done in a script. They are both run the by the same code. And a script does allow you to stop it (by turning it off.)

Like I said, I can’t move it into a script because it acts upon an MQTT payload which can be greater than 256 characters.

I don’t know what you’re talking about. Anything that can go into the action part of an automation can go in a script.

EDIT: Or are you saying you need the trigger variable in your action sequence? That’s the only thing that I can think of that could possibly matter.

EDIT 2: Maybe it would help if you shared the relevant part(s) of your automation.

Yes.
The automation is triggered by an MQTT payload.
That payload can be longer than 256 chars.
Therefore I cannot pass it to a script.

For example (a heavily simplified version)

  - alias: My Automation
    trigger: 
      - platform: mqtt
        topic: some/topic

      - service_template: >
          tts.google_translate_say
        data_template:
          entity_id: >    
            media_player.kitchen
          message: >
            {{ trigger.payload }}

trigger.payload is a dynamically built announcement that could be several hundred characters long (which is why I use MQTT in the first place).

And why do you say that?

Clearly a template can return a string that is longer than 256 characters, otherwise this wouldn’t work:

      - service_template: >
          tts.google_translate_say
        data_template:
          entity_id: >    
            media_player.kitchen
          message: >
            {{ trigger.payload }}

Why do you think that a variable for a script cannot accept a string longer than 256 characters?

I’d like to blame lockdown but I think I have had this idea for a lot longer than that. I thought that a script could not pass or be passed a variable greater than 256 characters long.

Clearly from what you are saying I am wrong and have to unlearn that.

Just as a sense check then, you say this will work with a long payload?

automation:
  - alias: My Automation
    trigger: 
      - platform: mqtt
        topic: some/topic

      - service: script.some_script
        date_template:
          message: >
            {{ trigger.payload }}

script:
  some_script:
    sequence
      - service_template: >
          tts.google_translate_say
        data_template:
          entity_id: >    
            media_player.kitchen
          message: >
            {{ message }}

I don’t see why not.

BTW, what you’re probably thinking of is the length limit for the state field of State Objects. They are limited to 255 characters.

1 Like