openshwprojects / OpenBK7231T_App

Open source firmware (Tasmota/Esphome replacement) for BK7231T, BK7231N, BL2028N, T34, XR809, W800/W801, W600/W601 and BL602
https://openbekeniot.github.io/webapp/devicesList.html
1.39k stars 239 forks source link

Identify when light is turned off/on #794

Open ostat opened 1 year ago

ostat commented 1 year ago

Feature Request Is there a way to know if the device was power off and on again quickly? When resetting a device we need to turn it on and off 3-5 times. This puts the chip in to a reset mode with a fast or slow flash. I assume there is a counter somewhere tracking the number of on and off switches. Would it be possible to access that counter?

Firmware:

Additional context We have lights controlled with a mains switch. On boot they are set to a brightness level that is appropriate for the location. I would like to create a script that sets the light to the max brightness if it was turned off and on again quickly.

openshwprojects commented 1 year ago

I can add $failedBoots variable. The reboot is marked as failed if device is up for time shorter than 5 seconds (configurable in general menu)

ostat commented 1 year ago

I was not able to get the $failedBoots to work. I did see that the value is one before the boottime then goes to zero. I tried changing boot time to 10seconds and played with turning on and off the power but I was not able to get failedBoot to go to 2 before the boot time or 1 after the boot time.

startDriver ntp
ntp_timeZoneOfs 10:00

echo boot failed boots $failedBoots, uptime $uptime, time $day $hour $minute $second

addRepeatingEvent 1 -1 echo boot failedBoots $failedBoots; uptime $uptime; time $day $hour $minute $second

//Override dynamic temp on fast reboot (mains off and on)
if $failedBoots>=2&&$uptime<5 then goto override 
if $failedBoots>=1&&$uptime>=5 then goto override 
goto continue

override:
echo overriding dynamic light, setting to max brightness, let there be light. failedBoots $failedBoots uptime $uptime
backlog led_temperature 154; led_dimmer 100; led_enableAll 1;
goto end:

continue:
echo setting up dynamic lighting. failedBoots $failedBoots uptime $uptime
backlog led_temperature 500; led_dimmer 20; led_enableAll 1;

end:
openshwprojects commented 1 year ago

It seems you are correct. This value will get cleared after boot is complete.

I can fix it and make it keep the value at startup: https://github.com/openshwprojects/OpenBK7231T_App/commit/c6d16f999d2e471789161fba116ec708afa035c7 Done, I am not sure if it's the final solution, but let's try it that way for now.

ostat commented 1 year ago

Thanks for the update. I see you are saving after boot is complete, where the boot count should be reduced by one. Do you also need to save it at the start, where I assume it is incremented by one?

Aside does the FW have a feature to put it in AP mode after n Off and on switches like the Tuya firmware does?

ostat commented 1 year ago

Thanks for the update. This is working for me. Of note failedboots is now one higher than you might expect. its number of failed + current boot.

This is my example boot script (I will come back and edit if I tweak it).

The script will set the light brightness and color dynamically based on the time of day. The intention is to have bright lights during the day and dull soft lighting of a night. With the ability use the mains light switch to override to max light if needed. It uses waitFor NTPState 1 to ensure that the NTP has set up before setting the initial value at boot time. Turning off/on the light during the boot success time (5s) will override the dynamic lighting and set to max brightness.

startDriver ntp
ntp_timeZoneOfs 10:00

//Override dynamic control on fast reboot (mains off and on)
//To trigger turn on for less than configured failed boot time(default 5s), off for 2s, on again.
if $failedBoots>=2 then goto override 

echo setting up dynamic lighting. failedBoots $failedBoots uptime $uptime
alias day_lights backlog led_temperature 200; led_dimmer 100; echo lights_day_lights
alias evening_lights backlog led_temperature 500; led_dimmer 50; echo lights_evening_lights 
alias night_lights backlog led_temperature 500; led_dimmer 25; echo lights_night_lights

//Timed events for auto switching when leaving the light on
addClockEvent 06:00 0xff 1 day_lights 
addClockEvent 19:00 0xff 2 backlog evening_lights 
addClockEvent 22:00 0xff 3 backlog night_lights

//set initial value while we wait
backlog led_temperature 500; led_dimmer 20; led_enableAll 1;

//Wait for NTP to set up and retrieve current time value
waitFor NTPState 1
if $hour>=06&&$hour<19 then day_lights
if $hour>=19&&$hour<22 then evening_lights
if $hour>=22||$hour<06 then night_lights
goto end

override:
echo overriding dynamic light, setting to max brightness, let there be light. failedBoots $failedBoots uptime $uptime
backlog led_temperature 154; led_dimmer 100; led_enableAll 1;

end:
echo autoexec done. failedBoots $failedBoots, uptime $uptime, NTPOn $NTPOn, time $day $hour $minute $second

@openshwprojects, thanks for your support

openshwprojects commented 1 year ago

Thanks, tell me when I can include it on examples list

ostat commented 1 year ago

The above config (link) has been working ok for me.

This is an extension to the automatic_day_and_night_lights.bat example. The main difference being, this version supports using the mains switch override the dynamic control. Not sure if you want to extend that one, or create a new example.

Otherwise we can close this one, thanks again.