ESP8266 | ESP-01S relay did not save states after power failure

Device Type: 5V ESP8266 ESP-01 2 Channel WiFi Relay Module 2 Channel Relay Module.

Board: ESP8266 + ESP-01S.

Problem: Relay did not save it states after reboot.

ESP-01S flashed and controlled by ESPHome Addon from IP address or HA entities.

Tried different configs here, but as a lack of practice, I can’t figure it out by myself. Relay cant retrieve it last state and switch to it after power loss. Last config I tried saves last state for HA entity, but not for relay. Tried configs with restore_from_flash and power_on_state, so far I’m stuck. Can someone please point me in the right direction?

My config from ESPHome device yaml for simple relay control:

esphome:
  name: water-input
  friendly_name: Water Controller

esp8266:
  board: esp01_1m

logger:
  baud_rate: 0 #need this to free up UART pins

api:
  encryption:
    key: "..."

ota:
  password: "..."

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Water-Controller Hotspot"
    password: "..."

captive_portal:

web_server:
  port: 80
  
time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: version
    name: Relay ESPHome Version
  - platform: wifi_info
    ip_address:
      name: Relay IP
    bssid:
      name: Relay BSSID

sensor:
  - platform: uptime
    name: Relay Uptime
  - platform: wifi_signal
    name: Relay WiFi Signal
    update_interval: 60s

uart:
  baud_rate: 115200
  tx_pin: GPIO1
  rx_pin: GPIO3

switch:
  - platform: template 
    id: relay1
    name: "Relay 1"
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
    optimistic: true

  - platform: template 
    id: relay2
    name: "Relay 2"
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
    optimistic: true

What do I need to add here?

Thank you!

lookup restore_mode, at least that’s what I’m using for my athom plugs

restore_mode: RESTORE_DEFAULT_OFF
1 Like

Thank you. Small progress here:

After adding

restore_from_flash: true

preferences: flash_write_interval: 1sec

and

restore_mode: RESTORE_DEFAULT_OFF

to every relay I got this strange behavior:

relay1 did not retrieve (or save) it state after turn off, but relay2 works as intended.

My code now is:

esphome:
  name: water-input
  friendly_name: Water Controller

esp8266:
  board: esp01_1m
  restore_from_flash: true

preferences:
  flash_write_interval: 1sec

logger:
  baud_rate: 0

api:
  encryption:
    key: "..."

ota:
  password: "..."

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Water-Controller Hotspot"
    password: "..."

captive_portal:

web_server:
  port: 80
  
time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: version
    name: Relay ESPHome Version
  - platform: wifi_info
    ip_address:
      name: Relay IP
    bssid:
      name: Relay BSSID

sensor:
  - platform: uptime
    name: Relay Uptime
  - platform: wifi_signal
    name: Relay WiFi Signal
    update_interval: 60s

uart:
  baud_rate: 115200
  tx_pin: GPIO1
  rx_pin: GPIO3

switch:
  - platform: template 
    id: relay1
    name: "Relay 1"
    restore_mode: RESTORE_DEFAULT_OFF
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
    optimistic: true

  - platform: template 
    id: relay2
    name: "Relay 2"
    restore_mode: RESTORE_DEFAULT_OFF
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
    optimistic: true

Why relay1 behave so? Did I mess something?

I don’t see any issues: I use same entries:

substitutions:
  name: "athom-plug-01"
  relay_restore_mode: RESTORE_DEFAULT_OFF

esp8266:
  board: esp8285
  restore_from_flash: true

preferences:
  flash_write_interval: 1min

switch:
  - platform: gpio
    name: "${friendly_name}"
    pin: GPIO12
    id: relay
    restore_mode: ${relay_restore_mode}

I would try to restart the relay, or put it without power for x minutes to get the flash cleared. Or re-upload code after keeping it off power for a few minutes

1 Like

Just found another esp-01s module and tested it on UART adapter, and it works! Flipped new one with the old - still relay1 issues. The problem with relay itself.

So the question now - is there something I can do with it to make it work properly? It has 2 jumpers and s1/s2 buttons on it.

This is the one:
Screenshot 2023-10-21 120955

Relay was with its own logic aboard. Was needed to add some delay to esp8266 after relay was fully booted.

Adding this code did the trick:

esphome:
  on_boot:
    priority: 1000
    then:
      - lambda: |-        
          delay(7000);