Wait for 20 minutes syntax for wait-template in visual mode?

I need to create a script to open some switches for 20 minutes… but I can’t find the right way to do it with scripts visual editor, just in YAML mode. So I added a 20 minutes timeout, that works fine…
Is there any way to write the minutes to delay in wait_template field of the scripts visual editor?

Thanks

Why a wait template and not just a simple 20 min delay and then close the switches again?

2 Likes

You are right. Thanks

I would have 2 different automation, one to turn them on and another one to turn them off, if they stay in turn on state for 20 mins

If you’re interested, here’s a fairly robust way to turn something on at a specific time and have it automatically turn off some time later.

In this example, the “some time later” is hard-coded to be 20 minutes but could be based on the value of an input_number.

  • It uses a time-only input_datetime to know when to turn on the switch (or whatever it is you want to do).
  • It automatically computes the turn off time based on the start time plus 20 minutes.
  • It is immune to restarts unlike methods that employ delay, wait_template, timer, etc. When Home Assistant restarts, the automation will always set the switch to the correct state.
- alias: example 555
  id: example_555
  trigger_variables:
    seconds: '{{ 20 * 60 }}'
  trigger:
  - platform: time
    at: input_datetime.start_time
  - platform: template
    value_template: >
      {% set t = (now().time()|string)[:5] %}
      {% set end = (state_attr('input_datetime.start_time', 'timestamp') + seconds) | timestamp_custom('%H:%M', false) %}
      {{ t == end }}
  - platform: homeassistant
    event: start
  action:
  - variables:
      ts: "{{ state_attr('input_datetime.start_time', 'timestamp') }}"
      t_start: "{{ ts | timestamp_custom('(%-H,%-M)', false) }}"
      t_end: "{{ (ts + seconds) | timestamp_custom('(%-H,%-M)', false) }}"
      t_now: '({{ now().hour }},{{ now().minute }})'
  - choose:
    - conditions: "{{ t_start <= t_now < t_end }}"
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.whatever
    default:
    - service: switch.turn_off
      target:
        entity_id: switch.whatever

NOTE
If you change the value of the input_datetime (i.e. set a new start time) the automation will re-compute the new end time.

3 Likes