User data_template for delay in a script. To allow pasing delay as a variable from an automation

I would like to be able to pass a delay value from my automation to my script. This would allow me to make my script more reusable. I asked on gitter but apparently its not currently possible.

See below as an example of what I would like to do:

timed_lamp:
  alias: "Turn on light for x time"
  sequence:
    - service: homeassistant.turn_on
      data_template:
        entity_id: '{{ lighttoturnon }}'
    - delay:
      data_template:
        minutes: '{{timetodelay}}'
    - service: homeassistant.turn_off
      data_template:
        entity_id: '{{ lighttoturnon }}'

This would be a nice addition. I am building a config page and would like to select a delay from an input_select to run certain scripts.

It’s possible now from within a script:

temporizador_calefactor:
  alias: 'Se ha encendido el calefactor'
  sequence:
    - service: script.turn_off
      entity_id: script.calefact_off
    - service: script.turn_on
      entity_id: script.calefact_off
calefact_off:
  alias: 'Activando el temporizador del calefactor'
  sequence:
    - delay: '00:{{ states.input_select.tiempo_calefactor.state | int }}:00'
    - service: homeassistant.turn_off
      entity_id: switch.sonoff_1

awesome! I was trying this…

sequence:
  - delay:
      seconds: '{{ input_select.delay_time | int }}'

…but couldn’t get it to work. I guess I needed to not use the seconds and in the format hh:mm:ss Thanks for this! You saved me more hours of frustration!

I’m facing a similar issue when it comes to using a calculated value for “delay” in an automation script, so sorry to hijack that thread.

The following automation works perfectly fine

- alias: TEST Automation Delay
  trigger:
    platform: sun
    event: sunset
    offset: '+00:05:00'
  action:
    - service: switch.turn_on
      entity_id: switch.MySwitch
    - delay:
        **minutes: 120**
    - service: switch.turn_off
      entity_id: switch.MySwitch

Now I just wanted to replace the part marked in bold with a calculated value.

I came up with

{{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }}

which works perfectly fine in http://MyHomeAssistantBox:8123/dev-template and returns 124.

But how to use it in the delay context?

I’ve tried the following without success:

minutes: "{{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }}" 

and

minutes: '{{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }}'

both result in

ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected int for dictionary value @ data[‘action’][1][‘delay’][‘minutes’]. Got None. when Checking Config.

minutes: {{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }} 

throws no errors when checking config, but reloading automation throws:

ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: invalid key: “OrderedDict([(’((((as_timestamp(states.sun.sun.attributes.next_dawn)) - (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int’, None)])”

Is this possible at all?

Thanks,
Tom