Using dynamic delay for switch

Hello folks!

My irrigation project for my home shall have 6 irrigation lines with a configurable irrigation time in the morning and in the evening for each line. Configuration shall be done via the web interface of Home Assistant. The automation part will be located in the ESP 32 Module.

The switch_on of the irrigation lines will be handled by a YAML Script. It is very basic:

  • switch on
  • delay (-> wait for a configurable time)
  • switch off

What I can not figure out is how to give the parameter for the delay function.

The definition of my script with parameters is:

script:
  - id: script_turns_relais_on
    mode: parallel
    parameters: 
      channel_index: int
      channelname: string
      currentdelaysec: float

The delay time is currentdelaysec. and defined as float. When setting the time to the delay something goes wrong:

 - delay:
          seconds: lambda |- 
              'return {currentdelaysec)}; '

ESPHome tells me that a float is expected and does not compile. In some post it is advised to assign the time directly to the delay. This throws the error that a time periode with unit is expected. I tried according to several posts global variables, sensor values (normaly float!) and much more - no valid code, no success.

The documentation gives the following advice (see Automations and Templates — ESPHome) and does not compile:

script:
  - id: blink_light
    parameters:
      delay_ms: int
    then:
      - light.turn_on: status_light
      # The param delay_ms is accessible using a lambda
      - delay: !lambda return delay_ms;
      - light.turn_off: status_light

Has anyone solved this issue? Or do I have to program timers, that bypass ESPHome?

int = short for “interger” aka a value
now its defined as NULL what equals to nothing hense the error so put in 1 or something else :wink:

if you want to set it from Home assistant of the website of the esp itself

you need to define:

  • default value (esp loses power for example)

then you can read the value from a “service” or input in many ways

an example from fully working thing that might explain bits and bobs

Thanks Victor,

I did not manage to set the delay directly. In the end I use a while: loop withe delay of 1 second and check the passed time by code:

Loop

   - while:
          condition: 
            lambda: return (id(channelcounters)[channel_index]  < currentdelaysec);
          then:
            - delay: 1s
            - lambda: |-
                  id(channelcounters)[channel_index]  += 1;
                  id(logtext) = id(channelcounters)[channel_index]; 
            - logger.log: 
                format: "Relais %d increment: %d up to %d" 
                args: [channel_index, id(logtext), currentdelaysec]

this works very nice even for my six channels, when the script mode is set to parallel.