Restore previous state after power failure - light

Hello,

I’m still new to both HA and ESPHome and I’m looking for a bit of help. I recently used tuya-cloudcutter to flash ESP on to some Merkury Innovation lights (MI-BW210-999WW) and have built a sunset/sunrise automation.

The lights are connected to a physical switch and sometimes they have been accidently switched off when the lights are on. When the switch is flipped back on, the lights reconnect but don’t turn back on. I have done some research seen “esp8266_restore_from_flash”, “on-boot” and “restore_mode” but none of them either make sense to me or completely answer my question.

Below is my yaml file for one of the lights. Any help is greatly appericated.

esphome:

  name: "driveway-light-right"

libretiny:

  board: generic-bk7231t-qfn32-tuya

  framework:

    version: dev

logger:

web_server:

captive_portal:

mdns:

api:

  password: !secret api_password

  encryption:  

    key: !secret api_encrypt_driveway-light-right

ota:

  password: !secret ota_password

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  ap:

  manual_ip:

    # Set this to the IP of the ESP

    static_ip: !secret ip_driveway-light-right

    # Set this to the IP address of the router. Often ends with .1

    gateway: !secret ip_gateway

    # The subnet of the network. 255.255.255.0 works for most home networks.

    subnet: !secret ip_subnet

button:

- platform: restart

  name: Restart

debug:

  update_interval: 30s

text_sensor:

- platform: debug

  reset_reason:

    name: Reset Reason

- platform: libretiny

  version:

    name: LibreTiny Version

sensor:

- platform: uptime

  name: Uptime

bp5758d:

  clock_pin: P8

  data_pin: P7

output:

- platform: bp5758d

  id: output_red

  channel: 3

  current: 29

- platform: bp5758d

  id: output_green

  channel: 2

  current: 29

- platform: bp5758d

  id: output_blue

  channel: 1

  current: 29

- platform: bp5758d

  id: output_cold

  channel: 5

  current: 65

light:

- platform: rgbw

  id: light_rgbw

  name: Light

  red: output_red

  green: output_green

  blue: output_blue

  white: output_cold

To start with a disclaimer: I am not familiar with libretiny or the lamp that you’re using. I’m therefore not sure if it has flash memory to store values which ‘survives’ a restart.

For a ‘regular’ ESP8266 or ESP32 you can make use of globals to store and restore values as follows:

esphome:
  #...
  on_boot:
    then:
      - light.control:
          id: my_light
          state: !lambda
            return id(last_light_state);

esp8266:
  board: ...
  restore_from_flash: true  # only needed for esp8266

globals:
  - id: last_light_state
    type: boolean
    restore_value: yes
    initial_value: 'false'

light:
  - platform: ...
    id: my_light
    on_turn_on:
      - globals.set:
          id: last_light_state
          value: 'true'
    on_turn_off:
      - globals.set:
          id: last_light_state
          value: 'false'

The restore_value tries to read value from the memory upon (re)start. The on_boot part sets the light state based on the last known state.

Finally, the light. Each time it’s switched on or off, would save the new state in the globals.

Didn’t seem like those worked. I’ll keep searching the documentation. Thanks for attempt!

It does work, but, the state of the light will only be stored in the flash memory after 1 minute because of this breaking change that will preserve your flash memory:
ESPHome 2023.4.0 - 19th April 2023

You can edit the write interval with this line:

preferences:
    flash_write_interval: example #(time in seconds e.g. 30s)