Sonoff last state after cut electricity

Hi, all!

Someone familiar with the sonoff devices, already know that there is an option inside the ewelink device settings, which can set the device to the last state if power down. So… no matter what the last state was, it will know to turn it on or off after power restoration.

I made this code in order to improve my S26 R2 device capability and with this I can set the time to auto turn off/on, if the state was changed, but I would really love to make this restore it’s last state after power down. Anyone knows how to do it? I’m almost noob with this :smiley:
In this moment, I just made the device turn on by itself, after power down…

esp8266:
  board: esp01_1m
  board_flash_mode: dout
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Dressing Router Switch"
    on_press:
      - switch.toggle: relay
  - platform: status
    name: "Sonoff Dressing Router Status"

sensor:
  - platform: wifi_signal
    name: "Sonoff Dressing Router WiFi Signal"
    update_interval: 60s
    
number:
  - platform: template
    id: on_power
    name: "Sonoff Dressing Router Auto Power On"
    optimistic: true
    restore_value: true
    initial_value: 0
    min_value: 0
    max_value: 100
    step: 1
    unit_of_measurement: seconds
    entity_category: config
    mode: slider
  - platform: template
    id: off_power
    name: "Sonoff Dressing Router Auto Power Off"
    optimistic: true
    restore_value: true
    initial_value: 0
    min_value: 0
    max_value: 100
    step: 1
    unit_of_measurement: seconds
    entity_category: config
    mode: slider

switch:
  - platform: gpio
    name: "Sonoff Dressing Router Relay"
    pin: GPIO12
    id: "relay"
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      then:
        - switch.turn_on: relay
        - if:
            condition:
              lambda: 'return id(off_power).state > 0;'
            then:
              - delay: !lambda 'return id(off_power).state * 1000;'
              - switch.turn_off: relay
    on_turn_off:
      then:
        - switch.turn_off: relay
        - if:
            condition:
              lambda: 'return id(on_power).state > 0;'
            then:
              - delay: !lambda 'return id(on_power).state * 1000;'
              - switch.turn_on: relay
  - platform: restart
    name: "Sonoff Dressing Router Smart Restart"

Thanks!

I just found out about the existence of “restore_from_flash”, I will test it a bit :slight_smile:

Edit: I added this “restore_from_flash” line and removed “restore_mode: RESTORE_DEFAULT_ON” but still nothing

esp8266:
  board: esp01_1m
  board_flash_mode: dout
  restore_from_flash: true

I have solved the issue, if someone search for the answer:

esp8266:
  board: esp01_1m
  board_flash_mode: dout
  restore_from_flash: true

preferences:
  flash_write_interval: 0s
switch:
  - platform: gpio
    name: "Sonoff Living Router Relay"
    pin: GPIO12
    id: "relay"
    restore_mode: RESTORE_DEFAULT_OFF

where did you typed that code?

In ESPHome file of the device