Script help: wait_template and proceed after x minute

I have this template to wait for door open or 5 minute then proceed but it does not trigger after 5 mins

sc_leave_scene:
sequence:
- wait_template: “{% if (is_state(‘binary_sensor.door_main’, ‘on’) or (now() - states.script.sc_leave_scene.attributes.last_triggered).seconds / 60 | int > 5) %}True{% else %}False{% endif %}”

How do I make the script to wait for door open proceed or proceed regardless door open or not.

The code isn’t going to work the way you think it does.

Thats the wrong syntax. You are subtracting 2 datetime objects, that doesn’t work. Also, you don’t need the if then crap.

sc_leave_scene:
  sequence:
    - wait_template: "{{ is_state('binary_sensor.door_main','on') or (now().timestamp() - as_timestamp(states.script.sc_leave_scene.attributes.last_triggered)) / 60 > 5 }}"

EDIT: To clarify, you need to use as_timestamp() on datetime objects to turn them into seconds, then subtract

Thanks for your script… unfortunately it does not work as well…

I come to believe it is problem with wait template, it does not keep checking the time now…
i think i need to work around.

Actually, you can, and the result is a timedelta object, which has a total_seconds() method that can be used. E.g.:

(now() - states.script.sc_leave_scene.attributes.last_triggered).total_seconds()

would evaluate to the number of seconds since that script was last triggered. @tyjtyj, be careful, though, if the script has not been triggered, then that attribute would evaluate to None, which cannot be subtracted from a datetime.

Also, I’m still not sure this will work the way it was being used, because a wait_template probably suffers from the same issue of not updating based on the value of now(), but that’s just a guess.

I think a better way to do this is to have the automation wait for the door to be open for 5 minutes, then run the script (without that wait.) E.g.:

automation:
  - alias: Wait for door open for 5 minutes, then ...
    trigger:
      platform: state
      entity_id: binary_sensor.door_main
      state: 'on'
      for:
        minutes: 5
    action:
      service: script.sc_leave_scene

So now I’m not sure exactly what you’re trying to do. If you just want the script to wait 5 minutes before proceeding, then just add a delay:

sc_leave_scene:
  sequence:
    - delay: '00:05:00'
    - ...

Or do you mean you want it to wait for either a) the door to open, or b) 5 minutes to elapse, whichever comes first? If so, then add a timeout:

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

your script is correct and should work unfortunately wait template does not seems to updating now().

I resolve it by spilting the scipt .

Thanks for everyone effort… simple things need to be so complex code…

sc_leave_scene:
      sequence:
    # Track leave scene running
        - service: input_boolean.turn_on
          entity_id: input_boolean.vr_leave_scene
    # Start wait for 5m script
        - service: script.turn_on
          entity_id: script.sc_leave_scene_wait_5m
        - wait_template: "{{ is_state('binary_sensor.door_main', 'on') }}"
          timeout: '00:05:00'
    #Run scene  2 if door open
        - service: script.turn_on
          entity_id: script.sc_leave_scene_2

sc_leave_scene_wait_5m:
      sequence:
        - delay: '00:05:00'
        - service: script.turn_on
          entity_id: script.sc_leave_scene_2


 sc_leave_scene_2:
      sequence:
    #check if leave scene aleady run or not 
        - condition: state
          entity_id: input_boolean.vr_leave_scene
          state: 'on'
        - service: input_boolean.turn_off
          entity_id: input_boolean.vr_leave_scene
        - service: switch.turn_off
          entity_id: switch.all_light

ps: how to use block code?

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.