SOLVED - ESPHOME opening my garage door when the ESP32 restarts (including after an ESPHOME update to the ESP32)

I am using Home Assistant, Node Red, and ESPHOME to control an ESP32 with a relay attached that is used to open and close my garage door.

To function, Node Red triggers the ESP32 to toggle the relay to close and open a circuit within about half a second (which effectively pushes the button on a garage door remote to open/close the garage door).

This works fine most of the time, except when the ESP32 is restarted - most notably if there is an update to the ESP32 driven by a ESPHOME updated, or when the power goes out and comes back on in our neighbourhood. When the ESP32 is restarted, it somehow causes the ESP32’s relay to toggle and the garage door opens.

Here is the ESPHOME card in Home Assistant:

esphome:
  name: esphome-garage-door

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "redacted"

ota:
  password: "redacted"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Garage-Door"
    password: "redacted"

captive_portal:

globals:
 # Set the threshold distance
 # if the reading is greater than the threshold distance the garage door is considered closed otherwise it will be considered open
 - id: threshold_distance
   type: float
   initial_value: '0.25'
    
sensor:
  # This is the physical sensor, not published to Home Assistant
  - platform: ultrasonic
    trigger_pin: 2
    echo_pin: 5
    name: "Ultrasonic Sensor"
    update_interval: 3s
    filters:
     - filter_out: nan
    id: ultrasonic_sensor
    internal: true

binary_sensor:
  - platform: template
    name: "Garage Door Sensor"
    id: garage_door_sensor
    lambda: |-
      return (id(ultrasonic_sensor).state > id(threshold_distance));
  
switch:
  - platform: gpio
    pin: 21
    id: garage_door_relay
    name: "Garage Door Relay"
  - platform: template
    name: "Garage Door Button"
    id: garage_door_button
    turn_on_action:
      - switch.turn_on: garage_door_relay
      - delay: 500ms
      - switch.turn_off: garage_door_relay
    turn_off_action:
      - switch.turn_on: garage_door_relay
      - delay: 500ms
      - switch.turn_off: garage_door_relay

The reference to an “Ultrasonic Sensor” in the above is just something that the Node Red code uses to ensure it doesn’t open the garage door when it is already open, or close the garage door when it is already closed.

Is there any safeguard that can be coded in above to ensure the relay doesn’t get toggle within say the first 30 seconds of the ESP32 being restarted?

Some pins are pulled high on boot, so you need to make sure that you are not using those pins on your board.
Look for a pinout for your board.

I’m wondering if it could be something else, the esphome card (above) is using pin 21 - which should be good - at least according to this: ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

I have found the solution. I needed to add

restore_mode: DISABLED

in the ESPHOME card, under where it says switch under where it says id: garage_door_button

Change as follows:

switch:
  - platform: gpio
    pin: 21
    id: garage_door_relay
    name: "Garage Door Relay"
  - platform: template
    name: "Garage Door Button"
    id: garage_door_button
    restore_mode: DISABLED
    turn_on_action:
      - switch.turn_on: garage_door_relay
      - delay: 500ms
      - switch.turn_off: garage_door_relay
    turn_off_action:
      - switch.turn_on: garage_door_relay
      - delay: 500ms
      - switch.turn_off: garage_door_relay
2 Likes