ESP8266 Relay (active low): how to prevent triggering on boot or reset (SOLVED)

I can’t seem to get this to work how I want it to.

When I reset the esp it still flashes the relays for a second before they turn off.

I tried this:

  - platform: gpio
    name: "GPIO4"
    restore_mode: ALWAYS_OFF
    pin:
      mcp23xxx: mcp23017_hub
      number: 3
      inverted: true
      mode:
        output: true

And this:

  - platform: gpio
    name: "GPIO4"
    restore_mode: RESTORE_INVERTED_DEFAULT_OFF 
    pin:
      mcp23xxx: mcp23017_hub
      number: 3
      inverted: true
      mode:
        output: true

But it still flashes the relays on reset/boot. What am I missing?

1 Like

thank you this worked for me. for anyone who wants to test quickly, bring the wemos and relay at your desk. whenever you make the change to code, turn off wemos and turn it back on, see if relay clicks. if it does not then your code is working. keep trying different scenarios until you figure it out. easiest way is to plug wemos into power bar which has on off switch.

here is my code if it helps, this is for the wemos d2 pin which is connected to in terminal on 5v relay

switch:

  • platform: gpio
    pin:
    number: D2
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF

    id: garage_door_relay

1 Like

I have the clicking problem only with the MCP23017 port expander during reboot of the ESP, not during a boot from power down. My board does not reset the MCP during boot, so the previous register values are retained in the MCP. During boot of ESPHome, the code assumes that all output registers have value 0x00 while in fact they are 0xff if all my relays are off (inverted mode).

The fix is to patch mcp23017.cpp and add two register reads to get the current output register state.

I will submit a PR with this fix.

1 Like

@wutr , thank you for adding this solution. It corrected the momentary relay turn-on issue experienced when initiating or rebooting the ESP8266 that I’m using to control an irrigation system.

1 Like

Hi,
I can’t make it work :frowning:
I have a relay connected to a Wemos D1
this is my config, at every boot/reboot the pin flips

  - platform: gpio
    pin: D6
    name: "Main Switch"
    id: main_switch
    #restore_mode: ALWAYS_OFF
    restore_mode: RESTORE_DEFAULT_OFF
    inverted: false
    #internal: true
    on_turn_on:
    #- delay: 500ms
    - delay: 300s

i tried also this

  - platform: gpio
    pin: 
      number: D6
      inverted: false
    name: "Main Switch"
    id: main_switch
    #restore_mode: ALWAYS_OFF
    restore_mode: RESTORE_DEFAULT_OFF 
    #internal: true
    on_turn_on:
    #- delay: 500ms
    - delay: 300s

D1, D5, D6, D7 all the time same issue
any ideas?

I’ve been banging my head with this problem for almost 2 weeks nothing I tried worked.
I have a passthrough battery pack to power d1 mini and a relay to charge it during the night, unfortunately because the battery turns off for a few milliseconds after the relay turns off and disconnects it from the outlet it would restart my d1 mini and with it would trigger all the relays including the one that is connected to the battery which leads to a loop where the mini keeps restarting until it fails.

The solution is super simple and right there in the documentation:
“* early_pin_init (Optional, boolean): Specifies whether pins should be initialised as early as possible to known values. Recommended value is false where switches are involved, as these will toggle when updating the firmware or when restarting the device. Defaults to true.”
Set it to false add the RESTORE_DEFAULT_OFF in the relay and everything works great without them auto switching on restart.

5 Likes

@Siar I have tried early_pin_init without luck. I am using Wemos D1 mini to control my Yeelight ceiling light. It has 3 GPIOs to control cold light, warm light and night light (dimmed light).
I am using pins D1, D2 and D5 (D5 is night light) and whenever I power it on, night light blinks for a split second. I know that it is that because when I disconnect the cable from D5, it is not happening.
This is my board definition:

esp8266:
  board: esp01_1m
  restore_from_flash : true
  early_pin_init: false

And this is how I define GPIOs and actual light:

output:
  - platform: esp8266_pwm
    pin: GPIO5 #D1
    id: output_warm
    frequency: 4882Hz
    min_power: 0.07
    max_power: 0.92
    zero_means_zero: true

  - platform: esp8266_pwm
    pin: GPIO4 #D2
    id: output_cold
    frequency: 4882Hz
    min_power: 0.07
    max_power: 0.92
    zero_means_zero: true

  - platform: esp8266_pwm
    pin: GPIO14 #D5
    id: output_nightlight
    frequency: 9765Hz
    max_power: 0.92

# Example usage in a light
light:
  - platform: monochromatic
    name: "${friendly_name} Nightlight"
    id: night_light
    output: output_nightlight
    gamma_correct: 0
    on_turn_on:
      - light.turn_off: ceiling_light
    restore_mode: ALWAYS_OFF
  - platform: cwww
    name: "${friendly_name}"
    id: ceiling_light
    cold_white: output_cold
    warm_white: output_warm
    cold_white_color_temperature: 6000 K
    warm_white_color_temperature: 2700 K
    gamma_correct: 0
    constant_brightness: true
    on_turn_on:
      - light.turn_off: night_light
    #restore_mode: RESTORE_AND_ON
    #restore_mode: ALWAYS_ON
    default_transition_length: 0s

preferences:
  flash_write_interval: 1min

Would you have an idea what else may I try?

I don’t have a lot of experience with PWN but unfortunately i think this is a hardware problem not a software one. From what i know D1 and D2 are the only 100% stable pins on boot for D1 mini, you can try a different pin to see how it reacts on boot or a different D1 mini if you have a spare(I’ve had cheap clones that react differently).

This site investigated pin outputs on boot and their behavior ESP8266 GPIO Behaviour at Boot

What i would try if you don’t mind complicating yourself is maybe adding a relay between your PWN pin and your light. That way after boot you activate the relay and complete the circuit and can drive your light as you would normally and during boot there might be a mismatch between the pin that operates the relay and your pwn pin which means the light won’t flicker.

I gave up quite a while ago with relays directly on esp pins becaise those short glitches. Now I always use port expander, usually mcp23017 (or 23008 for 8 outputs).

You should change the light value to switch, as the early_pin_init only affects the switch value.

@gitlaman how do I do that while still being able to control color/brightness for light?

Sorry i was flying trough this thread on my mobile and i didnt saw that you are using lights with frequency. Have you tried on diferent pin maybe, and i dont know if you know, but the wemos D1 controls the PWM through software and they are known to make flickering and so on, the ESP32 have hardware PWM and they are preffered for the LED lights (ESP8266 Software PWM Output — ESPHome)

I was curious about this, and i found out that you will always get this problem because the esp8266 gives little voltage on boot on that pin and only the D1 and D2 are low in 0V always on boot. You can see in this screenshot.

Here is the source (ESP8266 GPIO Behaviour at Boot)

So maybe with expander you will avoid that flicker on bootup, or choose another esp that have more outputs.

Thanks @gitlaman . Initially I tried with ESP32 (Wroom 32D DevkitV4) that has no flicker, but it takes little longer to boot, so there is visible delay when device is powered via AC (I do use switches to cut the AC), even noticeably longer than stock firmware.
Then I switched to ESP8266 (wemos D1 mini), that boots way faster, probably faster than stock yeelight, but has the flicker.
I did order few more ESPs to play with, I will see which one (if any) brings satisfactory results.

Thats info from a 6 year old web page, and yet its still an issue with these units!!

I have found this hardware solution to prevent the relay from triggering. Solder a 470uF 10v capacitor over the optocoupler pins. This works perfect!
Hardware solution to prevent relay from triggering
This would look like this:


There are boards with another layout, I don’t have one of those so can not give advice on them.

1 Like

Running into exactly this myself, with esp01+relay module. The information above is from Relay flickers on boot · Issue #1 · IOT-MCU/ESP-01S-Relay-v4.0 · GitHub where several possible solutions have been also discussed. Might be useful to someone.
D0 pin behavior cannot be controlled just by the firmware, it does a short low while booting.

Thank you!!! Thank you!!!Thank you!!!Thank you!!!
This did the trick! on NodeMCU

I tried a bunch of the options supplied, some worked but my problem wat that the garage and gate I wanted to open/close also was still using the original gate/garage remote. the solution I got to work was the below code:

switch:

  • platform: gpio
    pin: 14
    id: relay
    inverted: true
    name: “Garage White”
    icon: “mdi:garage”

this enable the existing remote controls to still activate the open/close and also stop the relay’s from activating when the nodemcu is restarted