Enable relay from HomeAssistant with variable time, passed to ESPHome

Hi,
Soon i’m planning to build a garden irrigation system with ESPHome, and therefor will trigger a few relays which in turn will control solenoid valves to enable/disable my garden sprinklers.

For this, i’d like the relay to be ‘on’ for a certain amount of time and then be switched off.

Offcourse, this would be very easy to do in HomeAssistant by first sending the ‘on’ signal, then wait for the desired amount of time, and the send the ‘off’ signal.
But, the risk I see is with any possible hickup in the ESP’s wifi-signal/connection, or HomeAssistant crashing, etc.
Therefor, what I’d rather do is sending the 'on’signal together with a parameter/variable (the desired on-time), and then have the ESP switch off the relay after this amount of time itself, fully locally on the ESP, so independent off any connection or second command to turn it off again.

How would i set this up in HA/ESPHome?

Thanks in advance!

That would be interesting but not sure this is possible. Maybe someone can help you on exactly what you are trying to do.

But if they don’t then;

A work around could be, to put in a condition… “if HA or Wi-Fi not connected then turn off” you probably would like your Wi-Fi to be stable or have a good/strong connection to use this.

so it would be something like this.

    on_turn_on:
      then:
        if:
          condition:
            and:
              - wifi.connected:
              - api.connected:
          then:
            - switch.turn_on: 
                id: zone_1
          else:
            - switch.turn_off:
                id: zone_1

You could put a delay in to turn it off like 5 or 10 min… this way you will know that watering is done for that zone.

Also just a suggestion if you didn’t know https://www.rainmachine.com/

This should be fairly easy.

  • Define your output switch in ESPHome
  • Define a number in ESPHome to select minutes

These will both appear in HA.

On your switch in ESPHome define an on_turn_on: automation that:

  • Does a delay: based on the value of your number. Remember that to do a delay in lambda you will need to convert minutes to milliseconds
  • After the delay, does a switch.turn_off: action

Note that you could override the turn off by toggling the switch from HA.

Hmm,
Not to familiar with that, are you able to give an example config of it?

Small up, anyone able to generate a example config regarding this?