Script help: wait_template and proceed after x minute

Timeout will not proceed with the script after 5 min… it just exit there

- wait_template: "{{ is_state('binary_sensor.door_main', 'on') }}"
  timeout: '00:05:00'

Weird, got it straight from here:

It says,
“It is possible to set a timeout after which the script will abort its execution if the condition is not satisfied.”

I wanted it to proceed after 5 mins… not abort.

Ah, ok, missed that. Then not sure how to do it.

Maybe start a timer before the wait and use the timer finishing as a second condition of the wait. Sorry, replying on phone so can’t really try it.

- service: timer.start
  entity_id: timer.mytimer
- wait_template: >
    {{ is_state('binary_sensor.door_main', 'on') or
        is_state('timer.mytimer', 'idle') }}

sorry to bump this, but it seems most appropriate to ask your assistance here:

using this script:

prompt_imac:
  alias: Prompt iMac
  sequence:
    - service: notify.notify
      data:
        message: "Switch off iMac"
    - wait_template: >
        {{ is_state('device_tracker.imac_lan' , 'not_home') and
           is_state('device_tracker.imac_wifi' , 'not_home') }}
      timeout: '00:05:00'
      continue_on_timeout: 'false'
    - service: script.switch_dorm_off  # <--- powerswitch which connects to the iMac...
    - condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    - service: notify.notify
      data:
        message: "Switched off iMac, continuing..."

the script does continue, even if the condition is not met… which is rather unpleasant, and causing iMac issues, as you can imagine.

did I understand the syntax incorrectly, in that it shouldn’t evaluate the template to be true?

I will add and ever further test to the template to make it:

 - wait_template: >
     {{ is_state('device_tracker.imac_lan' , 'not_home') and
        is_state('device_tracker.imac_wifi' , 'not_home') and
        states('sensor.dorm_actueel')|float < 1 }}
    timeout: '00:05:00'
    continue_on_timeout: 'false'

but if the logic is reversed, that wouldn’t make a difference I fear…
please have a look with me?

–UPDATE–

so I think I found it, and its rather serious, but simple to solve:
the docs at https://www.home-assistant.io/docs/scripts/#wait use quoted continue_on_timeout: 'false' while it should be simply continue_on_timeout: false

filed an issue/pr on this. Haven’t tested it on true but figure that would be the same…

Not sure about your conclusion. I just reviewed the code, and it should work, even if false is quoted. I haven’t personally tested it, but you might want to be sure.

well, ive read the code too, but even if it should, it didnt work.

Ive tested all possible variations with the above settings, and the only conclusion was the quotes made it not work.
If it something else yet, I wouldn’t know how to test, or how to find out what that might be.

In all cases my templates work not the issue, nor were the states of the entities. I checked and double checked on another instance

I just tested it. It works as expected. The following does not result in the notification being created:

script:
  test:
    sequence:
      - wait_template: false
        timeout: 5
        continue_on_timeout: 'false'
      - service: persistent_notification.create
        data:
          message: test

Same with continue_on_timeout: false. But I do see the notification if I change the value to true or 'true'.

just so I can explain myself best:

I used:

 - wait_template: >
     {{ is_state('device_tracker.imac_lan' , 'not_home') and
        is_state('device_tracker.imac_wifi' , 'not_home') and
        states('sensor.dorm_actueel')|float < 1}} 
    timeout: '00:05:00'
    continue_on_timeout: 'false'

the template is fine, and works perfectly with all 3 entities in their various states. Only evaluating the full template to True if both device_trackers are not_home And the sensor <1

Ive thought about the timeout notation, as you do, but ive checked, and it acts on the 5 minute count just fine.

what should happen is that the script stops after the timeout. It simply doesnt, and continues with the rest of the script, if I quote the ‘false’. Doesn’t matter what comes after it. In my setting, it runs another script switching of my iMac… which isn’t desirable…

taking out the quotes, makes it behave as desired… just tried is 2 more times, and 100% repeatable.

My PR was only aimed at the ‘false’, and I have explicitly mentioned that. Reading what you experience with the ‘true’ , that was a wise decision.

Now leaves us with the 'false' versus false.

Might it be a bug then, instead of a documentation error…

Please note that the wait_template itself works fine also: if the devices turn not_home within the 5 minutes timeframe the sensor floats below 1, turning the template into True, and immediately the script continues correctly

All I can tell you is if the timeout does expire, and you have continue_on_timeout: 'false', it will abort the script. If the script continues in this case, then the template must have evaluated to true before the timeout expired, even if you didn’t think it did.

You should check home-assistant.log. E.g., when I used 'false' or false, I saw this:

2019-08-26 09:36:33 INFO (MainThread) [homeassistant.helpers.script] Script test: Running script
2019-08-26 09:36:33 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step wait template
2019-08-26 09:36:39 INFO (MainThread) [homeassistant.helpers.script] Script test: Timeout reached, abort script.

When I used 'true' or true, I saw this:

2019-08-26 09:38:54 INFO (MainThread) [homeassistant.helpers.script] Script test: Running script
2019-08-26 09:38:54 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step wait template
2019-08-26 09:39:00 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step call service

FWIW, I did this test with 0.98.0.dev0.

That thought had crossed my mind also, one never knows… so Ive had a second machine on the dev-template page, and hit enter every second … no way it evaluated True.
That is practically impossible, and Ive also checked that, that even when the iMac is in sleep, it is still connected to the Network, and seen as Home.

Ill copy your script and see what happens here.

Sure one does. Just check the log. And if that’s not enough, then enable debug levels as needed, etc.

of course, will do.

just 1 thing: your template isn’t much of a template …

might I ask you to have a look at something more challenging? Just so we can rule out the template part causing havoc?

btw Ive just corrected a few (c&p) typos in my posts above in the template. Of no consequence as they are correct on my side in the HA instance, but wanted to be perfect on the community too. Not that you think I was cheating.

Sure. Testing this now:

script:
  test:
    sequence:
      - wait_template: "{{ states('sensor.date_time') == 'abc123' }}"
        timeout: '00:05:00'
        continue_on_timeout: 'true'
      - service: persistent_notification.create
        data:
          message: test

Once I verify I see the notification with that, I’ll change to continue_on_timeout: 'false' and run it again.

UPDATE 1: Ok, I saw the notification after 5 minutes:

2019-08-26 10:19:56 INFO (MainThread) [homeassistant.helpers.script] Script test: Running script
2019-08-26 10:19:56 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step wait template
2019-08-26 10:24:57 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step call service

Now testing with 'false'

UPDATE 2: Ok, I did not see the notification after 5 minutes, just as expected:

2019-08-26 10:26:41 INFO (MainThread) [homeassistant.helpers.script] Script test: Running script
2019-08-26 10:26:41 INFO (MainThread) [homeassistant.helpers.script] Script test: Executing step wait template
2019-08-26 10:31:42 INFO (MainThread) [homeassistant.helpers.script] Script test: Timeout reached, abort script.
1 Like

in the meanwhile, please let me think along a bit further on your suggestion the template might be True somehow. I could only imagine that if the sensor would switch somehow during fraction of a second.

I’d need to add a for: 00:01:00 or so to the template but thats not possible is it?

Why all this guessing? Why not just look in home-assistant.log? It will tell you if the timeout expired or not, just as I’ve shown you.

sure I do check the log and as I did it now, indeed both quoted and unquoted false lead to the timeout, and abortion of the script.

This is a first though, and I am completely puzzled how this can be.

because unexplained things happen… And as said the sensor seems not to be too stable (it is a Zwave switch with a small spread in the value. Ive added a small light to the switch, so I can test with that and not the iMac. But, in the event the light would be off, it switches between 0 and 1.1. That is a possibility for the template to go True, and continue the script.

Hence my thought preventing such a small change with a for:

In that case I would be 100% sure the template would be correct.

Logs below


2019-08-26 17:34:00 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Running script
2019-08-26 17:34:00 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Executing step call service

2019-08-26 17:34:07 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Executing step wait template

2019-08-26 17:39:08 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Timeout reached, abort script.

with quotes

2019-08-26 17:50:00 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Running script
2019-08-26 17:50:00 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Executing step call service

2019-08-26 17:50:10 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Executing step wait template
2019-08-26 17:55:11 INFO (MainThread) [homeassistant.helpers.script] Script Prompt iMac: Timeout reached, abort script.

So you’re saying the issue is this part of the template: states('sensor.dorm_actueel')|float < 1

Sometimes the sensor goes below 1 (while both device_tracker’s are not_home) and causes the template to become true? Is that right?

Whatever the reason, I think we’ve concluded that there’s nothing wrong with the continue_on_timeout feature, would you agree? Maybe you should close your PR, since there’s nothing wrong with the doc page.

Getting back to the main issue of the topic, it seems you need to do some filtering on the entities going into your wait_template. Maybe create a binary_sensor / input_boolean that only goes true when you want it to, and use that in the wait_template.

Since I can not be 100% positive about my claim, I’ll indeed close the PR, under reference to this discussion.

I’m not yet satisfied though, and leave the log on for now.

The binary_sensor has been made already, with a delay_on.

thanks for your support and precision.