How can sunrise and sunset be added to NTP-based events in OpenBK7231N, and what is the right way to set it up?
Yes — sunrise and sunset can be added to NTP events, and the feature was eventually merged after testing [#20869378][#20906485] Use a single `ntp_setLatlong` command instead of separate latitude/longitude commands, and keep the code behind a `#define` in `obk_config.h`; if you want to avoid breaking config downgrades, do not add new config fields and use globals/statics set from `autoexec.bat` instead [#20819240][#20843876][#20846866] In `autoexec.bat`, start NTP, set the timezone and coordinates, then `waitFor NTPState 1` before creating `addClockEvent sunrise ...` and `addClockEvent sunset ...`, otherwise the events may be created before the timezone change has taken effect [#20823027][#20828244] The implementation was reported to be accurate to about ±1–2 minutes versus published sunrise/sunset times, which was considered acceptable [#20823018][#20869623] If you want a sunset delay like Tasmota’s civil sunset, the simplest workaround suggested was a one-shot `addRepeatingEvent` delay of 30 minutes after the sunset event, but it will not survive a reboot during that delay [#20879370]
I've been testing it for a while now and it seems to work fine. There are a couple of cases where it doesn't work exactly as it should for the first day you set it up, depending on when you issue the event compared to when sunrise/sunset happens. But after the first day it should work fine. I put the code here: https://github.com/rickbronson/OpenBK7231T_App
I'm trying to figure out how to do a "pull request" but haven't quite got there yet...
According to my self test of the hour/minute timestamp of sunrise/sunset itself, the discrepancy from the time from official websites is like 1 minute, which is fully acceptable.
There is an example autoexec here: https://github.com/openshwprojects/OpenBK7231...p/blob/main/docs/autoexecs/sunrise_sunset.bat The GPS example in the autoexec isn't "mine", just a random one in my timezone. My tests of the following places showed no more than +-2 minutes error. I used a google search to find accurate sunrise/sunset time. I have no idea how accurate their algorithm is :)
#toyko: backlog removeClockEvent 13; NTP_SetLatlong 35.939078 138.735747; ntp_timeZoneOfs 9; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 35.939078 138.735747; ntp_timeZoneOfs 9; addClockEvent sunrise 0x7f 12 POWER OFF #hawaii: backlog removeClockEvent 13; NTP_SetLatlong 21.434091 -157.910329; ntp_timeZoneOfs -10; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 21.434091 -157.910329; ntp_timeZoneOfs -10; addClockEvent sunrise 0x7f 12 POWER OFF # Sao Paulo backlog removeClockEvent 13; NTP_SetLatlong -23.533773 -46.625290; ntp_timeZoneOfs -3; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong -23.533773 -46.625290; ntp_timeZoneOfs -3; addClockEvent sunrise 0x7f 12 POWER OFF # Mexico City backlog removeClockEvent 13; NTP_SetLatlong 19.432608 -99.133209; ntp_timeZoneOfs -6; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 19.432608 -99.133209; ntp_timeZoneOfs -6; addClockEvent sunrise 0x7f 12 POWER OFF # Cairo backlog removeClockEvent 13; NTP_SetLatlong 30.071893 31.267905; ntp_timeZoneOfs 2; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 30.071893 31.267905; ntp_timeZoneOfs 2; addClockEvent sunrise 0x7f 12 POWER OFF # New York backlog removeClockEvent 13; NTP_SetLatlong 40.730610 -73.935242; ntp_timeZoneOfs -5; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 40.730610 -73.935242; ntp_timeZoneOfs -5; addClockEvent sunrise 0x7f 12 POWER OFF # Buenos Aires backlog removeClockEvent 13; NTP_SetLatlong -34.603722 -58.381592; ntp_timeZoneOfs -3; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong -34.603722 -58.381592; ntp_timeZoneOfs -3; addClockEvent sunrise 0x7f 12 POWER OFF # cape town backlog removeClockEvent 13; NTP_SetLatlong -33.918861 18.423300; ntp_timeZoneOfs 2; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong -33.918861 18.423300; ntp_timeZoneOfs 2; addClockEvent sunrise 0x7f 12 POWER OFF # Moscow backlog removeClockEvent 13; NTP_SetLatlong 55.751244 37.618423; ntp_timeZoneOfs 3; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong 55.751244 37.618423; ntp_timeZoneOfs 3; addClockEvent sunrise 0x7f 12 POWER OFF # sydney backlog removeClockEvent 13; NTP_SetLatlong -33.865143 151.209900; ntp_timeZoneOfs 11; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong -33.865143 151.209900; ntp_timeZoneOfs 11; addClockEvent sunrise 0x7f 12 POWER OFF #solomon is backlog removeClockEvent 13; NTP_SetLatlong -9.601967 159.948609; ntp_timeZoneOfs 11; addClockEvent sunset 0x7f 13 POWER ON backlog removeClockEvent 12; NTP_SetLatlong -9.601967 159.948609; ntp_timeZoneOfs 11; addClockEvent sunrise 0x7f 12 POWER OFF #adelaide: backlog removeClockEvent 12; NTP_SetLatlong -34.911498 138.809488; ntp_timeZoneOfs 10:30; addClockEvent sunrise 0x02 12 POWER OFF
Many thanks @rickbronson and @p.kaczmarek2 for this feature.
I updated the firmware for my lights and appears to be working for me also. I updated the setup script for my timers to include the sunset event, but was looking for a way to get the expected next sunrise/sunset values to allow setting the initial state of the light. Sample script:
The simplest way would be to call addRepeatingEvent with repeats 1 and delay 30 minutes (30*60 seconds) in the sunrise event, but it would not take the device reboot into account (if it were to reboot during that 30 minutes delay)
Constants file is automatically exported from the source code. You only need to manually update autoexec samples JSON. The MD files will be generated when you run docs update, but I can do it after I merge the PR.
Guys, thank you for this! I finally am replacing our 24+ years X10 entrance module with this unit. Excited to try the new sunset/sunrise feature to automate the balcony light attached to it.
Can you Pleeeease give me a line by line sample/instruction on how to implement turning on the lights at sunset and off at sunrise please?
unless, you happen to live there:
You need to insert the correct lat/long coordinate of your home location so our mathematical formula can calculate correctly the time on sunrise and the sunset.
Futhermore, are you sure that you have correct time zone set?
Yes, of course I will update those, didn't want to publish my location... I will try it out now. Thanks
Added after 15 [minutes]:
"addEventHandler OnHold 8 SafeMode" I don't have anything at Pin8 to trigger, can we just have it always active? This relay should automatically turn off/on at sunset/rise, everyday.
// I'm the entrance/Hallway switch with 2 relays, Hallway(1) and Balcony(2)
// short press means turn all on
// 2 second press means turn all off
// First turn off/on Balcony by Sunrise/set
PowerSave 1
startDriver ntp
// must be corrected
ntp_timeZoneOfs -8
// must be corrected
ntp_setLatlong 44.002130 -123.091473
removeClockEvent 12
removeClockEvent 13
waitFor NTPState 1
addClockEvent sunrise 0x7f 12 POWER2 OFF
addClockEvent sunset 0x7f 13 POWER2 ON
// adjust button timing for hold, from 4 seconds to 1 second
SetButtonTimes 10 3 3
// program all on and all off actions
addEventHandler OnClick 24 startScript lights_on.bat
addEventHandler OnHold 24 startScript lights_off.bat
Is that OK? So excited if this works.
Tried it, got locked, turned around the sequence and it allows "all on/off" commands.
Now lets' see if it turns on the balcony lights...
Another edit, tried to pass the sunset time several times (changed ntp_timeZoneOfs), doesn't work.
NTP does work, Local Time: is correct, but no switching of relay 2.
Here is the current autoexec.bat
// I'm the entrance/Hallway switch with 2 relays, Hallway(1) and Balcony(2)
// short press means turn all on
// 2 second press means turn all off
//
// adjust button timing for hold, from 4 seconds to 2 seconds
SetButtonTimes 20 3 3
// program all on and all off actions
addEventHandler OnClick 24 startScript lights_on.bat
addEventHandler OnHold 24 startScript lights_off.bat
//
// turn off/on Balcony by Sunrise/set
PowerSave 1
startDriver ntp
// must be corrected
ntp_timeZoneOfs -8
// must be corrected
ntp_setLatlong 44.002130 -123.091473
removeClockEvent 12
removeClockEvent 13
waitFor NTPState 1
addClockEvent sunrise 0x7f 12 POWER2 OFF
addClockEvent sunset 0x7f 13 POWER2 ON
It seems to work during transitions actually. Thanks again!
The device has a strange MAC 00:00:00, that was causing issues.
Question: Is it possible to use this sunrise/set function in a way so that the device sets the right condition after each reboot, not just during transitions? For instance, after a long power outage (missing the transition from day to night), it should turn on if "night" condition.
Unless a variable, something like "set manually already" was set, so it would not constantly override it if one had prior set it manually. For instance, it is "day" but really dark outside and you manually turn it on, then, after a power outage or reboot it should not turn it off but respect your prior manual override.
Question: Is it possible to use this sunrise/set function in a way so that the device sets the right condition after each reboot, not just during transitions? For instance, after a long power outage (missing the transition from day to night), it should turn on if "night" condition.
@omniron Please have a look at my sample script in https://www.elektroda.com/rtvforum/topic4033229.html although the setup is with a smart light connected to a dumb switch I think the startup cases will be similar. The Time Constants section revolves around setting the initial light states.
omniron wrote:
Unless a variable, something like "set manually already" was set, so it would not constantly override it if one had prior set it manually. For instance, it is "day" but really dark outside and you manually turn it on, then, after a power outage or reboot it should not turn it off but respect your prior manual override. And vice versa.
To remember states after a device restart it looks like you need to use the LFS commands to write/read the manual states. In my scenario, I was able to avoid remembering the state by configuring the device so that when its switched off then on, it would trigger a timer mode by where the light is on for 30mins.
✨ A feature to add sunrise and sunset events to NTP-based scheduling on BK7231N devices was proposed and developed, with code inspired by Tasmota and integrated into the OpenBK7231T_App firmware. The implementation includes commands to set latitude and longitude in a single command to save flash memory, and sunrise/sunset events can be added as clock events triggering device actions such as powering relays on or off. Testing showed the sunrise/sunset calculations are accurate within ±2 minutes globally. Challenges addressed include handling NTP synchronization timing, timezone offsets, and ensuring correct event triggering after device reboot or power outages. The code footprint increased by about 11 KB, mainly due to trigonometric calculations. The feature was merged into the main repository with ongoing improvements such as self-tests and documentation updates. Users shared example autoexec.bat scripts demonstrating how to configure the feature, including setting timezone, coordinates, and clock events for controlling lights. Discussions also covered handling manual overrides and state persistence across reboots using LFS commands. The feature is fully functional and available in the latest OpenBK7231T_App firmware, with additional pull requests extending functionality to expose next sunrise/sunset times as constants.
TL;DR: OpenBK7231 sunrise/sunset NTP events are now fully functional, add about 1 minute of typical timing error, and were described as "fully acceptable" in self-tests. This FAQ helps BK7231N/BK7231T users automate relays at dawn and dusk, avoid autoexec timing mistakes, and document or submit the feature correctly. [#20869623]
Why it matters: Sunrise/sunset scheduling solves a real automation problem: lights and relays follow local daylight instead of fixed clock times, with workable accuracy and known startup limits.
Approach
Best use
Main requirement
Known limitation
addClockEvent sunrise/sunset
Daily daylight-based switching
Valid timezone, latitude/longitude, NTP sync
First-day edge cases can occur after initial setup
Fixed-time addClockEvent HH:MM
Stable manual schedules
Only local clock time
Does not track seasonal daylight changes
addRepeatingEvent after sunset
Simple delay like +30 minutes
Sunset event must run first
Reboot during delay loses the pending action
Key insight: Put timezone and location in autoexec.bat, then wait for NTP before creating sunrise or sunset events. That ordering prevents wrong-time calculations and makes the feature reliable after daily rescheduling. [#20823027]
Quick Facts
The sunrise/sunset code was first estimated at <900 bytes, but later measured at roughly 11 kB, likely due to trig functions such as atanf and sinf. [#20828276]
Test results across cities such as Tokyo, Hawaii, São Paulo, New York, Cairo, Moscow, and Sydney showed about ±2 minutes error in manual checks. [#20870355]
Built-in self-tests later reported sunrise/sunset timestamp discrepancy of about 1 minute versus official websites, which was accepted for automation use. [#20869623]
A failed NTP receive can appear in bursts of 10 messages because UDP is not reliable and recv may fail without indicating a firmware fault. [#20824897]
OpenBK7231 documentation examples are generated from JSON sources, including autoexecExamples.json, not by editing Markdown directly. [#20854291]
How do I set up sunrise and sunset NTP events in OpenBK7231T_App using autoexec.bat?
Use autoexec.bat to start NTP, set timezone and coordinates, wait for sync, then add the events. 1. startDriver ntp and ntp_timeZoneOfs .... 2. Set ntp_setLatlong <lat> <long> and run waitFor NTPState 1. 3. Add addClockEvent sunrise ... and addClockEvent sunset ..., for example event IDs 12 and 13. A working sample turns POWER OFF at sunrise and POWER ON at sunset after the wait step. [#20828244]
Why does addClockEvent sunrise or sunset fail if I set ntp_timeZoneOfs in autoexec.bat before NTP has fully synced?
It fails because the new timezone offset is not applied immediately to the current NTP time. The firmware explicitly reports, NTP offset set, wait for next ntp packet to apply changes, so a sunrise or sunset event created right away can use the wrong timezone. That produces the wrong local event time until the next NTP packet arrives. [#20823018]
What is waitFor NTPState 1 in OpenBK7231T_App, and when should I use it in an autoexec script?
waitFor NTPState 1 is a script gate that pauses execution until NTP is live. Use it before any command that depends on correct local time, especially addClockEvent sunrise and addClockEvent sunset. In this thread, it was recommended specifically to fix startup ordering in autoexec.bat. [#20823027]
What is SafeMode in OpenBK7231T_App, and how do I enter it without adding an OnHold event handler?
SafeMode is a recovery mode that lets you regain control after a bad script or configuration. You do not need an OnHold event for it. Enter it by doing 5 quick power off/on cycles, which makes the extra addEventHandler OnHold 8 SafeMode line unnecessary for normal sunrise/sunset automation. [#20950287]
Why does OpenBK7231T_App show "Info:NTP:NTP_CheckForReceive: Error while receiving server's msg" in groups, and is that normal for UDP NTP polling?
Yes, that can be normal because NTP here uses UDP, and UDP does not guarantee delivery. The maintainer checked the code and said the message appears when recv fails. One user noticed the messages arriving in groups of 10, but that alone did not indicate a firmware bug. [#20824897]
How accurate are the OpenBK7231 sunrise and sunset calculations compared with official sunrise/sunset times?
They are accurate enough for home automation, usually around 1 minute in self-tests and up to about ±2 minutes in broader city checks. One maintainer wrote that a 1 minute discrepancy versus official websites was "fully acceptable." That makes the feature suitable for lights, relays, and similar daily triggers. [#20869623]
What's the best way to turn a relay on at sunset and off at sunrise on a BK7231N or BK7231T device?
Use two sunrise/sunset clock events after NTP sync, because that tracks seasonal daylight automatically. For relay 2, a tested pattern is addClockEvent sunrise 0x7f 12 POWER2 OFF and addClockEvent sunset 0x7f 13 POWER2 ON, preceded by timezone, coordinates, and waitFor NTPState 1. That is better than hard-coded times when daylight changes through the year. [#20950188]
How can I make a device restore the correct day or night state after a reboot or power outage if it missed the sunrise or sunset transition?
Use time constants after NTP sync to set the initial state at boot, then let sunrise/sunset events handle later transitions. A posted script compared $hour and $minute with $sunset_hour and $sunset_minute to choose the correct startup mode. "Time constants" are runtime variables that expose computed clock values, including next solar event times, so scripts can restore state after reboot instead of waiting for the next transition.[#20878116]
What is the proper way to add sunrise/sunset documentation and example autoexec files to the OpenBK7231T_App docs system on GitHub?
Update the JSON doc sources, not the generated Markdown file. Put the example script in docs/autoexecs, add its metadata to docs/json/autoexecExamples.json, and then run the docs generation step. The maintainer said commands.md is autogenerated from JSON comments, so direct Markdown edits are not the preferred path. [#20854291]
How do I create a GitHub pull request for OpenBK7231T_App from my own fork if pushing to the main repository gives a 403 permission error?
Push your branch to your own fork first, then open a pull request from that fork to the parent repository. The 403 happened because direct push access to openshwprojects/OpenBK7231T_App.git was denied. After pushing to the personal fork, the contributor successfully created PR #1001 from branch sunrise_sunset_NTP. [#20865847]
Why should sunrise/sunset latitude and longitude settings use globals or autoexec.bat instead of adding fields to the OpenBK7231 config structure?
Use globals or autoexec.bat to avoid changing the persistent config structure size across firmware versions. The maintainer warned that if config size changes, downgrade compatibility breaks. He recommended globals or statics and simply setting latitude and longitude on every startup through autoexec.bat. [#20843876]
OpenBK7231 sunrise/sunset vs Tasmota timers: which approach is better for offset events like civil sunset or delayed light switching?
Use native OpenBK7231 sunrise/sunset events for exact dawn and dusk switching, but Tasmota-style offset behavior is more convenient for civil sunset. In this thread, offset support was not implemented directly. The suggested OpenBK7231 workaround was to trigger a delayed action from the sunset event, which works but is less robust than a built-in offset model. [#20879370]
How can I delay a sunset-triggered action by 30 minutes in OpenBK7231T_App, and what happens if the device reboots during that delay?
Trigger a one-shot repeating event from the sunset event with a 30 minute delay, or 30×60 seconds. That is the simplest workaround for civil-sunset-like behavior. The downside is clear: if the device reboots during that delay window, the pending delayed action is lost because the temporary timer is not preserved across reboot. [#20879370]
Where can I find a real BK7231N datasheet, and what official hardware information is actually available?
You likely cannot get a full public BK7231N datasheet from this project, because the maintainer said the forum information is all they know. In other words, the practical hardware reference here is the project’s forum and command documentation, not an official vendor datasheet package. That was the direct answer given in the thread. [#20843876]
Why is BL602 apparently ignored by the sunrise/sunset implementation in OpenBK7231T_App, and what needs to change to make sunset calculation work there?
The thread raises the BL602 issue but does not answer it, so the exact reason is unresolved here. The only concrete clue is a 2025-01-12 report pointing to a commit in PR #1030 and asking why BL602 was excluded from sunset calculation. From this thread alone, the next step is to inspect that commit and add the missing BL602-specific path or guard changes there. [#21389444]