Looking for fail safe setting when ESP32 looses network

Hi,
I have build a small irrigation system with two ESP32’s controlling eight valves. This works great: flower garden gets watered and my wife is happy - I don’t have to do it and I’m happy.

But, if f there is a network dropout during the watering cycle the valves keep open, which is no good. Im thinking that this is a common problem but searching for fail-safe and similar did not help.
Can anyone point me in the direction of some code that be modified to close the valves when network connection is lost?

best wishes,
Kasper

What does your current code look like? Are you sending both the on and the off command from HA? If so, maybe change it so that HA sends the on command, and then a timer locally on the esp32 turns it back off?

Thanks for replying. Yes all control is done from HA.
That might be a solution, do you by any chance have any examples or a pointer?
Current code (very rudimentary):


esphome:
  name: valve-control-node-2
  platform: ESP32
  board: esp-wrover-kit
wifi:
  ssid: secret
  password: secret
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Valve Control Node 2"
    password: secret
captive_portal:
# Enable logging
logger:

# Enable Home Assistant API
api:
  password: secret
ota:
  password: secret

switch:
  - platform: gpio
    name: "Ventil_5"
    pin: GPIO2
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    name: "Ventil_6"
    pin: GPIO26
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    name: "Ventil_7"
    pin: GPIO27
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    name: "Ventil_8"
    pin: GPIO33
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF

Digging around i came across the api.connected which seem to work. Thanks for the idea!
I added the code below, which turns off the valve when the node looses api connection:

interval:
  - interval: 1s
    then:
      if:
        condition:
          api.connected:
        then:
          #nothing if connected
        else:
          - switch.turn_off: relay

And to be on the safe side I added the following to make sure the relay is off if it instructed to reboot and event more safe by setting on_boot to turn the relay off.

esphome:
  name: testrelay
  platform: ESP8266
  board: esp01_1m
  on_shutdown:
    then:
    - switch.turn_off: relay
  on_boot:
    priority: -10
    then:
    - switch.turn_off: relay
1 Like

I don’t use ESPHome, but maybe this can help you or someone else. On my irrigation system (consisting of Sonoff 4CH PRO R3) I use ESP Easy and they have the concept of a “pulse” instead of an “on”. So instead of sending an “on” command, I send for example a “30 minute pulse” command. I chose those 30 minutes to be the maximum safe irrigation time. If the network works fine, it will get the “off” command (which clears the pulse) after a few minutes, but if the network drops out it will run the full 30 minutes, which is a bit too long but will do no harm.

Thanks for sharing this! It’s exactly what I have been looking for to prevent my irrigation from running when it shouldn’t, the same problem you needed to fix. :slight_smile:

Thanks for the feedback.
For the sake of clarity, the complete code is below. The ESP32 controls a cheap amazon 4 way Solenoid Valve, https://www.amazon.de/-/en/Jadeshay-Solenoid-Electromagnetic-Normally-Closed/dp/B07TYCQ4JN/ref=sr_1_23?keywords=4+wege+magnetventil&qid=1637073621&sr=8-23.
“ventil” = danish for “valve”.
I run two of these 4 way valves to irrigate 8 zones.

esphome:
  name: valve-control-node-2
  platform: ESP32
  board: esp-wrover-kit
  on_shutdown:
    then:
    - switch.turn_off: Ventil_5
    - switch.turn_off: Ventil_6
    - switch.turn_off: Ventil_7
    - switch.turn_off: Ventil_8
  on_boot:
    priority: -10
    then:
    - switch.turn_off: Ventil_5
    - switch.turn_off: Ventil_6
    - switch.turn_off: Ventil_7
    - switch.turn_off: Ventil_8

wifi:
  ssid: "secret"
  password: "secret"
  fast_connect: on

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "secret"
    password: "secret"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "secret"

ota:
  password: "secret"

switch:
  - platform: gpio
    id: Ventil_5
    name: "Ventil_5"
    pin: GPIO2
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    id: Ventil_6  
    name: "Ventil_6"
    pin: GPIO26
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    id: Ventil_7
    name: "Ventil_7"
    pin: GPIO27
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    id: Ventil_8
    name: "Ventil_8"
    pin: GPIO33
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF



text_sensor:
  - platform: template
    name: Uptime Human Readable
    id: uptime_human
    icon: mdi:clock-start
sensor:
  - platform: wifi_signal
    name: "ESP WiFi Signal"
  - platform: uptime
    name: Uptime Sensor
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? String(days) + "d " : "") +
                (hours ? String(hours) + "h " : "") +
                (minutes ? String(minutes) + "m " : "") +
                (String(seconds) + "s")
              ).c_str();


interval:
  - interval: 1s
    then:
      if:
        condition:
          api.connected:
        then:
          #nothing if connected
        else:
          - switch.turn_off: Ventil_5
          - switch.turn_off: Ventil_6
          - switch.turn_off: Ventil_7
          - switch.turn_off: Ventil_8