Configurable delay using templating

Hello, I want to create a script with a configurable delay but I can’t figure out how to correctly define it.

I tried the following using templating:

 countdown_run:
    alias: 'Countdown running'
    sequence:
      - delay:
          minutes: '{{states.input_select.timeout_delai.state|int}}'
      ...

But I end up with this error :

ERROR:homeassistant.bootstrap:Invalid config for [script]: expected int for dictionary value @ data[‘script’][‘countdown_run’][‘sequence’][0][‘delay’][‘minutes’]

Any idea ?

Thks!

We currently do not support templates in delays

I’d like this too, especially for parsing brightness values from an input_slider…

Just for the sake of it I am going to assume the typo is not in your actual config…right?

Well I use to mix french and english in my code…

I like the idea so I’ve added to pivotal. No timeline when it will be done…

2 Likes

I can’t believe this doesn’t have more votes. Having the ability to set values of delay with a template will make programming some events really easy. Imagine turning on a light with a motion detector and then turning it off after a programmable delay? You could have it turn off after 10 minutes in the morning and 3 minutes in the afternoon/evening.

For me personally, this would solve the issue of the lights turning off while I am in the shower and the motion detector can’t see me.

3 Likes

Something like delay: '00:{{ (range(1, 55)|random|int) }}:00' works. You can modify the same to your liking.

That may work, but I need it to be compared to time of day. I can’t get an “If” statement to be accepted in the delay: section.

I’m using this:

- delay: '00:{{ states.input_slider.light_timeout.state | int }}:00'

And if you need to compare it to a certain time frame just use conditions. If you want to go the ‘complicated’ way take a look at this: https://community.home-assistant.io/t/custom-component-to-declare-set-variables/25218?u=sjee

1 Like

Thanks everyone. Ultimately this is what I came up with:

In my included sensor.yaml:

    - platform: template
      sensors: 
        bathroom_delay:
          value_template: > 
              {% if (states.sensor.local_time.state > "05:00") and (states.sensor.local_time.state < "10:00") %}
                15
              {% else %}
                5
              {% endif %}

Then in my included script.yaml:

bathroom_motion_helper:
  sequence:
    - delay: "00:{{ states.sensor.bathroom_delay.state }}:00"
    - service: light.turn_off
      data:
        entity_id: light.over_sink_light_53

bathroom_motion_timer:
  sequence:
    - service: script.turn_off
      data:
        entity_id: script.bathroom_motion_helper
    - service: light.turn_on
      entity_id: light.over_sink_light_53
    - service: script.turn_on
      data:
        entity_id: script.bathroom_motion_helper

Finally in my automations.yaml:

- id: jwe001
  alias: Bathroom Motion Detected
  trigger:
  - entity_id: binary_sensor.eye_motion_71
    platform: state
    from: 'off'
    to: 'on'
  action:
  - alias: Turn on bathroom lights
    service: script.bathroom_motion_timer
  hide_entity: true

All the sensor created needs to be hidden from the interface and would not be necessary if we had a delay_template option.

It would appear that all is quiet on delay templating and I’m not sure that wait is going to achieve my end goal.

Having several similar irrigation sensors, I’m trying to reduce the amount of near duplicate code with a universal script.

In my use case, I’d like to concatenate the variable to an entity state, e.g.

    - delay:
        data_template:
          minutes: "states.input_number.duration_{{entity}}.state | int"

I don’t think HA people pay attention to our requests at all :frowning:
Way too busy implementing their own

By the way… the new “time of day binary” component should solve this problem!

How would the solution be like?

Is there a way to use passed variables in the delay. This for example does not work.

Automation

- alias: test_automation
  trigger:
  - platform: state
    entity_id: input_boolean.test
  action:
  - service: script.test_script
    data:
      duration: 10

Script

test_script:

  sequence:

    - service: notify.alexa_media
      data_template:
          target: media_player.echo_computer
          data:
            type: announce
          message: 'Test Script 1'

    - delay: "00:00:{{ duration }}"

    - service: notify.alexa_media
      data_template:
          target: media_player.echo_computer
          data:
            type: announce
          message: 'Test Script 2'

can you try this

- alias: test_automation
  trigger:
  - platform: state
    entity_id: input_boolean.test
  action:
  - service: script.test_script
    data:
      duration: 2
test_script:
  sequence:
    - service: persistent_notification.create
      data_template:
          message: 'Test Script 1'

    - delay: "00:00:{{ duration }}"

    - service: persistent_notification.create
      data_template:
          message: 'Test Script 2'

It does work as expected for me (and the docs confirm that we can have templates in delay).
I believe this FR was completed long time ago.