Delay variable not being recognised in a script

I am trying to pass the entity_id and the duration of the delay as variables from an automation to a script:
The automation…

  trigger:
    platform: state
    entity_id: light.level_9
    from: 'off'
    to: 'on'
  action:
    - service: script.light_timer
      data:
        entity: 'light.level_9'
        delay_mins: '15'

The script…

  alias: "Light timer"
  sequence:
  - delay:
    data_template:
      minutes: "{{ delay_mins }}"
  - service: light.turn_off
    data_template:
      entity_id: "{{ entity }}"

YAMLlint tells me the syntax is ok.

However Check Config includes:
2018-03-01 22:03:30 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: [delay] is an invalid option for [script]. Check: script->script->light_timer->sequence->0->delay. (See /home/homeassistant/.homeassistant/configuration.yaml, line 75). Please check the docs at https://home-assistant.io/components/script/

Check Config says OK if I comment out the three lines in the script:

     alias: "Light timer"
     sequence:
   #  - delay:
   #    data_template:
   #      minutes: "{{ delay_mins }}"
     - service: light.turn_off
       data_template:
         entity_id: "{{ entity }}"

Any help much appreciated.

Have a look at delay_template not sure if it will help or not.

Try the following instead:

 alias: "Light timer"
 sequence: 
   - delay: '00:{{ delay_mins | int }}:00'
   - service: light.turn_off
     data_template:
       entity_id: "{{ entity }}"

I have a working script for delay in seconds stated similarly, so you might as well try anyway.

Let us know how it goes.

Thanks keithh666, searching on delay_template led me to Configurable delay using templating where there are great examples of even more sophisticated ways of inserting delays.

Thanks pplucky, your suggestion worked perfectly. Wonderful!

It’s a steep learning curve for me, but home automation is very rewarding!

1 Like

Further update on the concept of having an automation for each light serving a single script. I’m guessing that as one of the lights I have programmed has stayed on for three hours that each time another light with an automation is turned on, the single script is refreshed so that the “turn_off” signal goes to the latest light and the older one is forgotten.
In which case I may as well have an automation script for each light that does the sensing and the turning on then off of the switch. And no point trying to be smart with a single script.