Lambda value on delay:

Hi Homeassistanters and esphomers,
anyone knows if is possibile to set a variable value for a delay within a script?

I have a script like the one below, but I would like to have the delay duration coming from a global.

I have tried with
dalay: !lambda "id(duration) + 'min'; "
but it is making my device to randomly reboot…

script
  - id: execBoostMode
    mode: restart
    then:
      - switch.turn_on: heaterRelay
      - delay: 30 min
      - switch.turn_off: heaterRelay
   

Thanks a lot.
Marco.

1 Like

Looking for a similar solution, but just i want the MQTT to pass the delay payload to the delay property.
I also haven`t found anything suitable on the docs , i will try with your ‘solution’ and check if i benefit from it :1st_place_medal:

Did you find a solution? I set the delay without unit but it seems not to interpret it.

The delay is templatable, but it expects a timeout in milliseconds.

- delay: !lambda "return id(duration) * 60 * 1000;"

If someone could PR the documentation to add this, that’d be appreciated :slight_smile:

This is from the documentation on esphome about delay actions. (last two lines).

on_...:
  then:
    - switch.turn_on: relay_1
    - delay: 2s
    - switch.turn_off: relay_1
    # Templated, waits for 1s (1000ms) only if a reed switch is active
    - delay: !lambda "if (id(reed_switch).state) return 1000; else return 0;"

Maybe not explicitly said. But still it there.

/Mattias

Created a PR.

PR accepted, and documentation updated. The ESPhome team move fast! :slight_smile:

1 Like