How to prevent ESPHome controlled relay from turning on at startup

I took apart my garage door remote, soldered wires to the button contacts, and hooked them to a relay controlled by ESPhome (Wemos D1 Mini) in order to open/close my doors. The problem is that when I supply the power to the Wemos D1 Mini it turns on the relays for a split second, this will cause the doors to open. Normally, not a big deal. But if the Wemos loses power then regains power it would open all my doors. Is there a way to prevent this millisecond of turning the relays on? I’m also providing 3.3v power to the garage remote using the Wemos 3.3v pin. I’ve considered finding a way to cut the power at startup. Is there code that I could add to ESPhome that would toggle a switch command 1 second after startup? Then I could wait 1 second before supplying power to the remote control.

Here is my code

switch:
  - platform: gpio
    pin: D5
    id: relay1
    name: "Side Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay1
  - platform: gpio
    pin: D6
    id: relay2
    name: "Big Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay2
  - platform: gpio
    pin: D7
    id: relay3
    name: "Basement Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay3

Have you wired your relays to be normally energised?

So that when you power up the relays they change state because the coil is powered?

If so just remove the inverted: true config and use the opposite contact on the relay. e.g if you were using C and NC use C then and NO instead.

The relays are wired to be normally energized, but I don’t know how I can wire them differently to be normally de-energized. Instead, I used the inverted: true in my esphome code. But it takes maybe 200 milliseconds for the relay to invert after powering on the Wemos.

The wire are currently wired to the C → NC. If I use the opposite contact (C → NO) on the relay then the doors will all open once the wemos and relay lose power as the relays will de-energize and the C and NO will be connected.

You need to wire them to be normally de-energised. That way loss of power (or power on) won’t activate them.

Which relay (module?) are you using?

If you use C & NO (normally open) and wire the relays to only come on and close the contact when you activate them then you will not get glitching or activation on power loss.

I’m using this relay - https://www.amazon.com/dp/B00KTEN3TM?psc=1&ref=ppx_yo2ov_dt_b_product_details

Here is my wiring diagram

Is there a way to wire this so that the relay is de-energized by default?

Remove this from your switches:
inverted: true

And use the C & NO contacts.

That won’t work. Without inverted: true in my ESPhome code the relays energize by default once the Wemos D1 Mini receives power and will then open the garage doors.

I even tried adding restore_mode: ALWAYS_OFF but it still turns on for a split second when it receives power.

Here is a video of what it is doing

Does this happen even without the esp board connected to the relay board, i.e. when you power up just the relay board?

No, it does not happen when I power up just the relay board.

Well that’s some good news.

With the board still disconnected from the ESP, does a relay activate when you connect its input to ground or is it when you connect an input to 3.3V?

Actually try this with a jumper wire.

The relay activates when connected to ground

You are already using the best of the limited available GPIOs, D5,6 and 7. (ESP8266 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials)

You have the correct config:

  - platform: gpio
    pin: D6
    id: relay2
    name: "Big Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay2

And you are correctly using the C & NO contacts (so the contacts close when you activate the switch).

The only other thing I can think to suggest is to add physical 3.3V pull-up resistors to ensure the inputs are held high before the GPIOs are configured as outputs. Any value from 1K to 10K Ohm should work.

4a90e9f783bb12f5433b073905e18b3d97cc6b31_2_690x291_2

However if these outputs are actually pulled low for some reason at boot this is unlikely to help.

1 Like

I found a work around. I’m powering the remote control using the 3.3v from the ESP board. I routed the 3.3v power from the ESP to the 4th relay on the board. But I used the C>NC instead of the C>NO I’m using for the controls. Then I removed the invert:true from that relay command. Finally, I used an ESPhome automation to turn that 4th relay “on” 2 seconds after boot. This way the power is cut from the remote for the first 2 seconds of boot up, the same time the other relays turn on briefly.

esphome:
  name: garage-door-opener
  on_boot:
    then:
      - delay: 2s
      - switch.turn_on: relay4

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

...........WiFi info.........

captive_portal:

switch:
  - platform: gpio
    pin: D5
    id: relay1
    name: "Side Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    restore_mode: ALWAYS_OFF
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay1
  - platform: gpio
    pin: D6
    id: relay2
    name: "Big Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    restore_mode: ALWAYS_OFF
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay2
  - platform: gpio
    pin: D7
    id: relay3
    name: "Basement Garage Door"
    icon: "mdi:garage-variant"
    inverted: true
    restore_mode: ALWAYS_OFF
    on_turn_on:
    - delay: 1000ms
    - switch.turn_off: relay3
  - platform: gpio
    pin: D2
    id: relay4
    name: "Basement Garage Door Remote Power"
    icon: "mdi:power"
    restore_mode: ALWAYS_OFF

My new wiring

I just wanted to leave a comment to this unresolved post since this is the first hit in google.
Adding the following line helps, as well check the pinout diagram of the wemos d1 mini, some pins are high at boot. D1, D2, D3, D5, D6,D7, D8 works.

esp8266:
  board: esp01_1m
  early_pin_init: false
4 Likes

Hi @dansity your solution is brilliant. It works for me.
Is that happening for Wemos D1 only or this is for every ESP32 / ESP8266 board?
Thanks
Vasilis