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.
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.
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.
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.
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
Sounds like a bad idea as this will significantly increase the gap between pushing the light switch and the light finally goes on
Indeed, when saving the state to flash is (yet?) not possible with libretiny this is the way to go
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
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
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.
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
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.