How to restore a ESPhome light to it's previous state on power

I’ve flashed a Tuya RGBWW can light with ESPhome using the libretiny_pwm platform. My code is as follows:

esphome:
  name: tuya-light-1
  friendly_name: tuya light 1

bk72xx:
  board: generic-bk7231t-qfn32-tuya
  framework:
    version: dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"



wifi:
  ssid: "XXXXXXXXXXX"
  password: "xxxxxxxxxxx"
  #Static IP
  manual_ip:
    static_ip: 192.168.1.229
    gateway: 192.168.1.1
    subnet: 255.255.255.0



text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version

output:
  - platform: libretiny_pwm
    id: output_red
    pin: P26
  - platform: libretiny_pwm
    id: output_green
    pin: P24
  - platform: libretiny_pwm
    id: output_blue
    pin: P6
  - platform: libretiny_pwm
    id: output_cold
    pin: P8
  - platform: libretiny_pwm
    id: output_warm
    pin: P7


light:
  - platform: rgbww
    id: light_rgbww
    name: Light
    color_interlock: true
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold
    warm_white: output_warm
    restore_mode: RESTORE_AND_ON

I want to make sure when the light switch is turned and the light receives power that it turns on using the same state it was in when it was turned off (red, white, warm white, etc). If that’s not possible, I’d settle for being able to restore the light to a pre-set state, like warm-white (3700K). I’ve tested using the configuration above and it does not work. Is there a way to do this?

Since I don’t see a switch input variable, does this mean that this processor is only running when the light is on?
(Depending on if the processor is always on or only on when the light is on will determine the best way to save the state of the light).

Correct, the processor is only running, and there is only power to the light, when the light switch is turned on. I want the light to restore it’s previous state, or set the default color temperature of the light, when it receives power from the light switch being turned on.

on_boot :point_down:

That works for esp82xx and esp32 like described in the docs but I didn’t found a option for libretiny in the 10 seconds I flow over the docs :man_shrugging:

Thanks for the help!

I’ve pieced this together so far, I’m also referencing the light component page on ESPhome

esphome:
  name: tuya-light-1
  friendly_name: tuya light 1
#Restore light to 3700K when turned on
  on_boot:
    priority: 600
    then:
      - light.turn_on:
          id: light_rgbww
          brightness: 100%
          color_mode: COLOR_TEMPERATURE


text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version

output:
  - platform: libretiny_pwm
    id: output_red
    pin: P26
  - platform: libretiny_pwm
    id: output_green
    pin: P24
  - platform: libretiny_pwm
    id: output_blue
    pin: P6
  - platform: libretiny_pwm
    id: output_cold
    pin: P8
  - platform: libretiny_pwm
    id: output_warm
    pin: P7


light:
  - platform: rgbww
    id: light_rgbww
    name: Light
    color_interlock: true
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold
    warm_white: output_warm
    restore_mode: RESTORE_AND_ON

I can’t figure out what to add, and how to add it, to this line to set the color temp to 3700K
color_mode: COLOR_TEMPERATURE

How would I write that line to make the lights set to 3700K temp on boot?

Basically there are 3 options:

  1. Store the state of the light in the flash memory of the processor. Unfortunately you are using a rather obscure processor so I can’t find specs. I am assuming that, like an esp8266, there is only flash storage with a limited number of writes. (Look At The Adjusting Flash Writes Section). Depending on how often you change the state of the light you could store the light state in the flash memory but if you change it a lot this will impact the longevity of your device.

  2. Write the state of the light to a sensor in home assistant and when the light boots, get the value of the sensor from home assistant. This reduces the wear on the flash memory, but depending on your boot time you will have (potentially significant) lag between turning on the light and it changing to the state it was in when it was turned of.

  3. Have the light go to to a default state on startup.

Let me know what option suits you and I can walk you through setting it up. (Too lazy to type out all the steps for each solution haha). btw. you could potentially combine 2 & 3.

Thanks for the help. I think option 3 is the best option for me. I’d like the light to set the default color temp immediately on boot.

That’s only a theoretical problem on esp82xx but even for this theoretical issue (no case yet found were a esp82xx has grid locked flash) esphome has us covered and allows adjusting flash writes :page_facing_up:

Sounds like a bad idea as this will significantly increase the gap between pushing the light switch and the light finally goes on :bulb:

Indeed, when saving the state to flash is (yet?) not possible with libretiny this is the way to go :raised_hands:

To have the light go to a default value on booting the following documentation is relevant
on boot and ESPHome light.

Setting up esphome: like this should do the trick (I think). Don’t forget to update the % parameters as desired. Let us know if you run into any issues.

esphome:
  name: tuya-light-1
  friendly_name: tuya light 1 
  on_boot:
    priority: -100
    then:
      - light.turn_on:
          id: light_rgbww
          brightness: 100% # Change value as desired
          red: 100% # Change value as desired
          green: 100% # Change value as desired
          blue: 100% # Change value as desired
          cold_white: 100% # Change value as desired
          warm_white: 100% # Change value as desired
1 Like

I didn’t look into it in too much detail, but didn’t want to risk being responsible for causing any sort of damage to someone else’s device. If you have experience or other sources/links I’d definitely be interested :slightly_smiling_face:

I agree it was not likely to be a good choice but you never know if it’s some niche case (eg. studio light in room with other devices which take 10minutes to startup anyway). But I totally agree that for 99% of the cases the delay will mean this is not a good solution. Nevertheless, it is an option so just wanted to put it out there.

Thanks for the help! I’ll give this a shot tonight when I get a chance to play with it.

I played around a little more and settled on this solution. This way I can just enter a kelvin temp

esphome:
  name: tuya-light-1
  friendly_name: tuya light 1
#Restore light to 3700K when turned on
  on_boot:
    priority: 600
    then:
      - light.turn_on:
          id: light_rgbww
          brightness: 100%
          color_temperature: 3700K

text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version

output:
  - platform: libretiny_pwm
    id: output_red
    pin: P26
  - platform: libretiny_pwm
    id: output_green
    pin: P24
  - platform: libretiny_pwm
    id: output_blue
    pin: P6
  - platform: libretiny_pwm
    id: output_cold
    pin: P8
  - platform: libretiny_pwm
    id: output_warm
    pin: P7


light:
  - platform: rgbww
    id: light_rgbww
    name: Light
    color_interlock: true
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold
    warm_white: output_warm
    restore_mode: RESTORE_AND_ON
2 Likes

This thread was incredibly helpful - I too now have a flashed (libretiny) Tuya downlight working, and restoring to a default state when power is restored. I am using these in a bedroom, and really want them to restore the the previous state based on the flash value. This way when suffering a brief power outage in the middle of the night, the lights come back to the state they were in before the power was cut (perhaps “off”).

I am using a bk7231t board, that is in these lights: https://amzn.to/3RGz4XF (you can’t beat the price)

I have this value set (every minute is plenty)

preferences:
  flash_write_interval: 1min

Using the ESPHome dashboard on the device, I see this working:

[lt.preferences:104]	Saving 2 preferences to flash...
[lt.preferences:132]	Saving 2 preferences to flash: 0 cached, 2 written, 0 failed

I’m struggling to understand how to write the necessary YAML to restore this value when the light is powered back on. I’ll keep pushing, but any assistance would be awesome :beers:

light:
  - platform: ...
    restore_mode: RESTORE_DEFAULT_ON # Attempt to restore state and default to ON.

restore_mode (Optional): Control how the light attempts to restore state on bootup. …

  • RESTORE_DEFAULT_OFF - Attempt to restore state and default to OFF if not possible to restore.
  • RESTORE_DEFAULT_ON - Attempt to restore state and default to ON.
  • RESTORE_INVERTED_DEFAULT_OFF - Attempt to restore state inverted from the previous state and default to OFF.
  • RESTORE_INVERTED_DEFAULT_ON - Attempt to restore state inverted from the previous state and default to ON.
  • RESTORE_AND_OFF - Attempt to restore state but initialize the light as OFF.
  • RESTORE_AND_ON - Attempt to restore state but initialize the light as ON.
  • ALWAYS_OFF (Default) - Always initialize the light as OFF on bootup.
  • ALWAYS_ON - Always initialize the light as ON on bootup.

And esphome is even smart enough to actually only write changed states to flash - so it typical only get’s written once if you change the light settings:bulb:

I’m still working through this. Because this is a Tuya device converted using cloudcutter (libretiny), i think that RESTORE_DEFAULT_ON will only set the power to the previous state. I cannot use esp8266_restore_from_flash because this is not an esp8266 chip.

Still digging…

Here is my full ESPhome code for a Lumary RGD wafer light. It works perfectly. The lights default to 3200 Kelvin temp when turned on by the switch.

esphome:
  name: basement-rgb-wafer-light-1
  friendly_name: Basement RGB Wafer Light 1
#Restore light to 3200K when turned on
  on_boot:
    priority: 600
    then:
      - light.turn_on:
          id: light_rgbww
          brightness: 100%
          color_temperature: 3200K


bk72xx:
  board: generic-bk7231t-qfn32-tuya
  framework:
    version: dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "rzoxpLgJTOT9q71U6J9OfDR0puV4Ekof/VPT6M9avPw="

ota:
  password: "044fbfe89ea96625092b74e6ce8ff58a"

wifi:
  ssid: "Phillips_IoT"
  password: "xxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "RGB Light WiFi Fallback"
    password: "xxxxxxx"



text_sensor:
  - platform: libretiny
    version:
      name: LibreTiny Version

output:
  - platform: libretiny_pwm
    id: output_red
    pin: P26
  - platform: libretiny_pwm
    id: output_green
    pin: P24
  - platform: libretiny_pwm
    id: output_blue
    pin: P6
  - platform: libretiny_pwm
    id: output_cold
    pin: P8
  - platform: libretiny_pwm
    id: output_warm
    pin: P7


light:
  - platform: rgbww
    id: light_rgbww
    name: Light
    color_interlock: true
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold
    warm_white: output_warm
    restore_mode: RESTORE_AND_ON
1 Like