Script help: wait_template and proceed after x minute

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.

HI @pnbruckner,

please let me bother you again, sorry.

Ive managed to rewrite the flow of events, so i get useful feedback in all phases but one: If the wait_template times out, and the script is aborted, it does so silently.
Id really need a notification upon that event. Would you know of a way to realize that?

consider this:

prompt_imac:
  alias: Prompt iMac
  sequence:
    - service: notify.notify
      data:
        message: "Script paused: need to switch off iMac!"
    - wait_template: >
        {{ is_state('binary_sensor.imac_off','on') }}
      timeout: '00:05:00'
      continue_on_timeout: 'false'
    - service: script.switch_dorm_off
    - condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    - service: notify.notify
      data:
        message: "Switched off iMac, continuing..."

is an event created which can trigger an automation? Or can I insert a message in this script somehow?

Ideally I want the logger to stop loggin the scripts, because it uses a lot of system resources, so best not rely on anything created by that.

As I’m sure you’re aware, HA’s scripting component is very basic and is really not up to the task here. The best I could suggest is to not abort (so continue_on_timeout: true), and then add something that checks the state of the binary_sensor. Unfortunately not very easy to to if-then-else in HA scripts.

wait, your right! I could add a service_template of course for both the switch and the message after that… yes, thanks a bunch!

- wait_template: >
        {{ is_state('binary_sensor.imac_off','on') }}
      timeout: '00:05:00'
      continue_on_timeout: 'true'
    - service_template: >
        script.{{'switch_dorm_off' if is_state('binary_sensor.imac_off','on') else 'dummy' }}
    - condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    - service: notify.notify
      data_template:
        message: >
         {{"Switched off iMac, continuing..." if is_state('binary_sensor.imac_off','on') else "You need to manually turn the iMac of later"}}

would do it… ;-0

2 Likes