logo elektroda
logo elektroda
X
logo elektroda

Automating Daylight Savings Adjustments for NTP with Scripting or driver update

randomalias324 2487 58
ADVERTISEMENT
  • #31 21359557
    randomalias324
    Level 8  
    So I wrote an autoexec script inspired by miegapele's script. It is possibly overly complicated, but it is more general. It can handle triggering on any day index of any week index of any month, and the daylight saving period can span the middle of the year or cross over the new year (like Australia). Parameters are store in private channels and are typed in only once per parameter.

    To be honest, I don't know how to explain how the indexing formulas work. I found a procedure that worked and then I simplified the logic equations. The resulting simplified equations kinda obfuscate whats going in. But, they do calculate the days to go or days past the nth weekday of the month without using modulo operators.

    I developed the script on open beken version 1.17.740, but noticed it fails on 1.17.689, which most of my sensors had installed. I am not sure why. When I OTA updated them they worked. I did try checking formulas though, and putting comments on their own line with no white space.

    Even if this script is overcooked, maybe it can serve as just another example in the diverse collection of scripts that can run on these open beken devices.

    // ************************************************
    // ** GENERIC TEMP HUMIDITY SENSOR WITH LCD + IR **
    // ************************************************
    //                   AUTOEXEC.BAT
    
    
    
    // **** Setup Base Drivers and TuyaMCU Channels ****
    startDriver TuyaMCU
    startDriver NTP
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 101 val 1
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 102 val 2
    tuyaMcu_defWiFiState 4      // This setting is required to prevent the TuyaMCU resetting when it receives 'not connected' message when MQTT is offline
    
    
    
    // **** Set custom NTP Server address ****
    //ntp_setServer 192.168.4.45
    
    
    
    // **** Define Daylight Savings Parameters ****
    SetChannelType 10 ReadOnly
    SetChannelPrivate 10 1
    SetChannelLabel 10 "10 Base timezone offset"
    SetChannel 10 10
    SetChannelType 11 ReadOnly
    SetChannelPrivate 11 1
    SetChannelLabel 11 "11 Daylight Savings offset" // The difference between DST inactive and DST active
    SetChannel 11 1
    
    SetChannelType 20 ReadOnly
    SetChannelPrivate 20 1
    SetChannelLabel 20 "20 Daylight Savings Start Month"
    SetChannel 20 10
    SetChannelType 21 ReadOnly
    SetChannelPrivate 21 1
    SetChannelLabel 21 "21 Daylight Savings Start Week index" // 1st occurance = 1
    SetChannel 21 1
    SetChannelType 22 ReadOnly
    SetChannelPrivate 22 1
    SetChannelLabel 22 "22 Daylight Savings Start Day index" // Sunday = 0, Saturday = 6
    SetChannel 22 0
    SetChannelType 23 ReadOnly
    SetChannelPrivate 23 1
    SetChannelLabel 23 "23 Daylight Savings Start Hour" // 24hr time
    SetChannel 23 2
    
    SetChannelType 30 ReadOnly
    SetChannelPrivate 30 1
    SetChannelLabel 30 "30 Daylight Savings Stop Month"
    SetChannel 30 4
    SetChannelType 31 ReadOnly
    SetChannelPrivate 31 1
    SetChannelLabel 31 "31 Daylight Savings Stop Week index"
    SetChannel 31 1
    SetChannelType 32 ReadOnly
    SetChannelPrivate 32 1
    SetChannelLabel 32 "32 Daylight Savings Stop Day index"
    SetChannel 32 0
    SetChannelType 33 ReadOnly
    SetChannelPrivate 33 1
    SetChannelLabel 33 "33 Daylight Savings Stop Hour"
    SetChannel 33 3
    
    
    
    // **** Define Daylight Savings Calculations ****
       // Calculations to see where we are reletive to the nth occurance of index day for start
    SetChannelPrivate 25 1
    SetChannelLabel 25 "25 Days to trigger (DTT) - Start"
    SetChannelPrivate 26 1
    SetChannelLabel 26 "26 First Ineq Result - Start"
    SetChannelPrivate 27 1
    SetChannelLabel 27 "27 Second Ineq Result - Start"
    
    alias Run_Strt_inq_1 SetChannel 26 ($CH21*7-7+$day-$mday)<0
    alias Run_Strt_inq_2 SetChannel 27 (($CH26+$CH21)*7-6+$day-$mday-$CH22)>0
    alias Run_Strt_DTT SetChannel 25 ($CH27-$CH26)*7+$CH22-$day
    alias Run_Strt_Day_Logic backlog Run_Strt_inq_1; Run_Strt_inq_2; Run_Strt_DTT
    
       // Calculations to see where we are reletive to the nth occurance of index day for start
    SetChannelPrivate 35 1
    SetChannelLabel 35 "35 Days to trigger (DTT) - Stop"
    SetChannelPrivate 36 1
    SetChannelLabel 36 "36 First Ineq Result - Stop"
    SetChannelPrivate 37 1
    SetChannelLabel 37 "37 Second Ineq Result - Stop"
    
    alias Run_Stop_inq_1 SetChannel 36 ($CH31*7-7+$day-$mday)<0
    alias Run_Stop_inq_2 SetChannel 37 (($CH36+$CH31)*7-6+$day-$mday-$CH32)>0
    alias Run_Stop_DTT SetChannel 35 ($CH37-$CH36)*7+$CH32-$day
    alias Run_Stop_Day_Logic backlog Run_Stop_inq_1; Run_Stop_inq_2; Run_Stop_DTT
    
       // Test if we are before or after the trigger days
    SetChannelPrivate 28 1
    SetChannelLabel 28 "28 Past Start Trigger"
    alias Eval_Strt_Trigger SetChannel 28 ($month>$CH20)||(($month==$CH20)&&(($CH25<0)||(($CH25==0)&&($hour>=$CH23))))
    
    SetChannelPrivate 38 1
    SetChannelLabel 38 "38 Before Stop Trigger"
    alias Eval_Stop_Trigger SetChannel 38 ($month<$CH30)||(($month==$CH30)&&(($CH35>0)||(($CH35==0)&&($hour<($CH33-$CH11)))))
    
       // Test if we are in daylight savings based on the trigger days
       // Note that if the start time is later in the year than end, ie DST rolls over newyears, different logic applies as shown in Set_Daylight_Savings_Bool_2
    SetChannelType 15 ReadOnly
    SetChannelPrivate 15 1
    SetChannelLabel 15 "15 Daylight Savings Active"
    alias Set_Daylight_Savings_Bool_1 SetChannel 15 $CH28&&$CH38
    alias Set_Daylight_Savings_Bool_2 if (2016*$CH20+168*$CH21+24*$CH22+$CH23)>(2016*$CH30+168*$CH31+24*$CH32+$CH33) then SetChannel 15 ($CH28+$CH38)==1
    
       // Set up alias's to run the required commands
    alias Run_Daylight_Savings backlog Run_Strt_Day_Logic; Run_Stop_Day_Logic; Eval_Strt_Trigger; Eval_Stop_Trigger; Set_Daylight_Savings_Bool_1; Set_Daylight_Savings_Bool_2
    
    alias Set_Time backlog ntp_timeZoneOfs $CH10; Run_Daylight_Savings; ntp_timeZoneOfs $CH10+$CH11*$CH15
    
    alias ReSet_Time backlog clearClockEvents; Set_Time; addClockEvent ($CH33*$CH15+$CH23*(!$CH15))*3600 0xff 1 ReSet_Time
    
    // **** Run Daylight savings logic ****
    waitFor NTPState 1
    ReSet_Time


    Attached is a derivation of the indexing formulae
  • ADVERTISEMENT
  • #32 21360771
    p.kaczmarek2
    Moderator Smart Home
    Nice work. By the way, if you know programming, you can also just do a custom driver in C How to create a custom driver for OpenBeken with online builds (no toolchain required)
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #34 21361200
    p.kaczmarek2
    Moderator Smart Home
    Ah, this PR. I've reviewed it again and decided that I can merge it, but can you, maybe, disable it by default?Screenshot of source code editing in a configuration file with the ENABLE_NTP_DST option highlighted.
    How many users needs this feature and what is the flash usage increase?
    Helpful post? Buy me a coffee.
  • #35 21361233
    divadiow
    Level 34  
    p.kaczmarek2 wrote:
    maybe also from @divadiow, is it ready to merge?

    sorry. I seemed to have missed this.
  • #36 21361412
    max4elektroda
    Level 20  
    No problem, there is so much going on here.
    @divadiow one question was, if it's possible to run on BL602 or if the image is to big.
    I will disable it as default.
    Maybe @randomalias324 can tell if this single command is enough for his needs or if there is some good command missing, don't know, something like $nextDSTswitch or ... ?

    And, of course, Merry Christmas 🎄🎁!
  • #37 21364166
    max4elektroda
    Level 20  
    Made some extensions and changes to the code:
    It will now automatically adjust the time following DTS rules, so it should serve a larger audience than the simple command.
    I also changed logging to avoid using not already used time.h functions.





    Now additional size is more moderate:
    
    Beken:    bin:   +1.4k      rbl:   +900b
    LN882H:   bin:   +1.6k      OTA:   +1k
    BL602:    bin:   +1.5k      ota:   +1k
    W600:     fls:   +1.3k      OTA:   +1k

    Last commit will disable feature again (like it was before) for all platforms but Windows (can't do a self test if a feature is disabled ;-))
  • ADVERTISEMENT
  • #38 21366516
    p.kaczmarek2
    Moderator Smart Home
    Merged, thank you. Let me know do you have any other PRs pending?
    Helpful post? Buy me a coffee.
  • #39 21366892
    max4elektroda
    Level 20  
    Thanks @p.kaczmarek2 for merging.
    Actually I have two other "pending" PRs:
    1464 to remove the unnecessary HTTP calls to state on pages != Index
    1471 for W800 OTA and "internal" OTA w/o the App

    If you can imagine to accept it, I would "port" the clock configuration (time zone, DST information) to a union saved in cfg, so no need to configure it on every start. It's already present e.g. in the outdated PR 1425 in drv_deviceclock and new_cfg.

    Then maybe later finishing the idea to separate clock functions from NTP functions...

    Added after 11 [hours] 24 [minutes]:

    max4elektroda wrote:
    Then maybe later finishing the idea to separate clock functions from NTP functions...

    Made a new PR to split NTP code:
    There are several functions related to clock functions inside NTP code.
    In order to possibly use other clock sources in the future, they should be separated.

    https://github.com/openshwprojects/OpenBK7231T_App/pull/1483

    I would highly appreciate intense testing of the changed code:
    Is NTP working as expected?
    Are clockEvents working - also with sunset/sunrise?
    Thanks!
  • Helpful post
    #40 21374202
    divadiow
    Level 34  
    OpenESP32S3 interface with system information. Screenshot of the OpenW600_CD0A369D device interface with system details. Device interface for OpenBL602 with status and configuration information. User interface of OpenLN882H_C25E1088 device showing system status information. Screenshot of the OpenW800 user interface showing connection and configuration details. Screenshot of the OpenBK7231N Fridge Tuya Plug user interface showing online status. Web interface screen of the OpenESP32C2 module. OpenTR6260 device control panel with configuration buttons and diagnostic information. User interface of OpenESP32 device showing system information and configuration options. XR809 device user interface with system information and configuration options.Screenshot of the OpenBK_BK7231T_Desk_Calex_Plug interface showing the OFF state and system details.

    so i guess it's a start. ntp starts on all but XR809 and TR6260 for which the driver doesn't seem to be enabled. clock not a thing for XR809

    I did notice in my refreshing though that some of them (I wasn't watching/noting methodically at the time) would jump up from xx minutes online to xx minutes online, quite the difference - quite the noticeable jump in some cases. I should restart each one and then see which are losing track.

    Added after 27 [minutes]:

    having said that, a subsequent refresh after a few minutes online has made them all jump up several minutes, but roughly the same amount each. I can't recall - is that a browser count thing and the refresh only gets the device current online time upon refresh?

    Added after 1 [hours] 1 [minutes]:

    oh. I think it's fine. I guess the up-time timer is only active when the page is foreground. The page refresh gets the up-time the device is reporting, which is what matters?
  • #41 21374332
    max4elektroda
    Level 20  
    Thanks for the tests, great!
    The uptime shown on the web page is counted by the browser and only updated to the devices uptime only on refresh of the page.
    I would expect this on the release versions, too.
    Good to hear it's working
  • #42 21374462
    divadiow
    Level 34  
    max4elektroda wrote:
    I would expect this on the release versions, too.

    yep yep.

    all those that remained up in the night (which excludes ESP32C2 - WDT reboot) they all have the expected uptime this morning of ~9 hours 53mins. None are way out of whack that might signal an issue.

    Do you have a preferred clocky thing to be tried next?
  • #43 21374542
    max4elektroda
    Level 20  
    There are two possible causes for this, maybe both are coming together here:
    1: The browsers timing is not too precise, so the numbers diverge over the time
    2: There is the fact, that the uptime is only a rough estimation. Maybe you recall my first tries with an internal clock which lead to the observation, that "g_secondsElapsed" was running out of sync about 2 minutes a day.
    https://www.elektroda.com/rtvforum/topic4046620.html
    So one of my goals is still to implement the idea from there, hence also improving the uptime measurement (the implementation used there came down to 1 - 2 seconds per day).

    Back to your question: If you really can afford some time, you could try some "timed events", especially "tricky" ones like do something every day at sunset

    I updated obk_config.h to allow these on all platforms (except TR, I maybe will try in the next commit, don't know about the memory etc there. And, most important, it doesn't use NTP at all)
    It's strange: PR doesn't rebuild the firmwares after this change?!?
    So please use them from here: https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/12595088390/job/35103694426
  • #44 21374549
    divadiow
    Level 34  
    max4elektroda wrote:
    Maybe you recall my first tries with an internal clock which lead to the observation, that "g_secondsElapsed" was running out of sync about 2 minutes a day.


    yes, I am desperately trying to remember with certainty what I/we had already ascertained.

    max4elektroda wrote:
    Back to your question: If you really can afford some time, you could try some "timed events", especially "tricky" ones like do something every day at sunset


    yes. I will familiarise myself with some sunset stuff. I've not actually touched clock event things so just need to swat up on the documentation.
  • #45 21374574
    max4elektroda
    Level 20  
    divadiow wrote:
    I am desperately trying to remember

    ... I know this to all to well ;-)

    TR build was done, so maybe that would be some test, if NTP runs at all here
  • #46 21394201
    divadiow
    Level 34  
    @max4elektroda how are you testing split NTP/Clock? what clock events? how are you externally recording the event actually occurs at the expected time?

    anything to jump-start me into testing..
  • #47 21394408
    max4elektroda
    Level 20  
    To be honest, exactly that's my problem: I'm the special user who doesn't really use the devices but only programs them.
    So I made some event to change a channel and check them later, if they are changed.
    If you don't usually use events, no problem, then I will try here.
    But maybe you can do a basic test on the platforms I don't have: does the clock work as expected?
    If, eg, there is a relay or led, you might check if you can change the state with an event in the (near) future. If it works in 15 seconds there's no reason why it should not work in 15 hours...
    Actually I have Beken N, LN882H, W800 and BL602.

    Added after 4 [minutes]:

    Sunset and sunrise can usually be "echoed" iirc, you might need to define "ENABLE_EXPAND_CONSTANT"
  • #48 21394745
    divadiow
    Level 34  
    max4elektroda wrote:
    I'm the special user

    I don't use them much either irl.

    I think I'm overthinking it. Of course HA can just track events!

    Screenshot of history and event log for device LNCLOCK 1, showing on and off cycles.

    Added after 43 [minutes]:

    OK. I think I'm going for something like this:

    Code: Text
    Log in, to see the code


    clock events for main on/off then an arbitrary delayed toggle

    Added after 49 [seconds]:

    bum. W600 doesn't have LFS

    Added after 20 [minutes]:

    Code: Text
    Log in, to see the code

    no backlog delay_s? will remove. keep simple

    Added after 11 [minutes]:

    I think I'm hitting ENABLE_EXPAND_CONSTANT missing on ESP. Basic autoexec is:

    Code: Text
    Log in, to see the code


    but no clockevents are actioned at specified time.

    no ESP platform in here https://github.com/MaxineMuster/OpenBK7231T_A...3c8381801835411fa3cde9ee/src/obk_config.h#L20
  • #49 21395066
    max4elektroda
    Level 20  
    Sorry for that, I think I used an old obk_config.h there.
    Code was updated in the meantime, should be present now in the latest code:
    https://github.com/openshwprojects/OpenBK7231...a9ba824b60270bbb1192b3fdd5ae/src/obk_config.h

    So if you could move to the latest firmwares, they are now built again also in the PR:
    https://github.com/openshwprojects/OpenBK7231T_App/actions/runs/12791520421

    Thanks for your support and sorry for the hassle
  • #50 21395076
    divadiow
    Level 34  
    no, my bad, I was looking at the wrong branch.

    I am testing with Built on Jan 15 2025 15:27:55 version 1483_merge_3e38a3384635

    I am glued to the Actions tab to keep an eye on what's going on 🤓

    in that case, I'll expand from this one ESP32 S3 onto other ESPs to see how they behave. The same autoexec copied verbatim to LN does behave as expected, so maybe that's an indicator of something with ESP...

    Added after 5 [minutes]:

    hmm. ESP is weird. That's the second time after a reboot the MQTT settings and wifi config is reset to default and it goes into AP mode (not safe mode), always after I save autoexec (which I'm doing loads of times and it's usually fine). Not got a pattern/log for why.
  • #51 21395107
    max4elektroda
    Level 20  
    Ah, ESP, I forgot about them in the list of platforms I can access. I can look at my ESP32, but might be not before the weekend.
    Somtimes I get distracted from here by other tasks ;-)
  • ADVERTISEMENT
  • #53 21396101
    max4elektroda
    Level 20  
    max4elektroda wrote:
    I can look at my ESP32, but might be not before the weekend.

    Just did it, but at leas with my simple test changing the autoexec file and restarting ESP does work as expected ?!?
    Or is this only happening seldom on changes?
  • #54 21396144
    divadiow
    Level 34  
    >>21396101

    Seldom. No doubt I'll be saving autoexec a ton more on ESP and may get a pattern, if it even continues. The actual non-operation of that script, the one that worked on LN, was odd tho.

    Have you success with clock event on ESP?
  • #55 21396173
    max4elektroda
    Level 20  
    Yes, but I'm only doing it "by hand" now:

    entered command:

    backlog echo $ch1;  echo $hour:$minute:$second; addClockEvent 19:04:30 0xff 123 setChannel 1 123


    log:
    
    Info:MAIN:Time 2550, idle 0/s, free 193996, MQTT 1(1), bWifi 1, secondsWithNoPing 2484, socks 0/0 
    Info:MAIN:Time 2551, idle 0/s, free 184728, MQTT 1(1), bWifi 1, secondsWithNoPing 2485, socks 0/0 
    Info:CMD:0
    Info:CMD:19:4:24
    Info:CMD:[WebApp Cmd 'backlog echo $ch1;  echo $hour:$minute:$second; addClockEvent 19:04:30 0xff 123 setChannel 1 123' Result] OK
    Info:MAIN:Time 2554, idle 0/s, free 194160, MQTT 1(1), bWifi 1, secondsWithNoPing 2488, socks 0/0 
    Info:MAIN:Time 2556, idle 0/s, free 194504, MQTT 1(1), bWifi 1, secondsWithNoPing 2490, socks 0/0 
    Info:MAIN:Time 2557, idle 0/s, free 194060, MQTT 1(1), bWifi 1, secondsWithNoPing 2491, socks 0/0 
    Info:MAIN:Time 2558, idle 0/s, free 193988, MQTT 1(1), bWifi 1, secondsWithNoPing 2492, socks 0/0 
    Info:GEN:CHANNEL_Set channel 1 has changed to 123 (flags 0)
    
    Info:MAIN:Time 2559, idle 0/s, free 193080, MQTT 1(1), bWifi 1, secondsWithNoPing 2493, socks 0/0
  • #56 21396245
    divadiow
    Level 34  
    OK. that works for me too. 30s
    Code: Text
    Log in, to see the code


    and I set one for 20 mins in future and it echoed at time expected. ESP32S3 ✅
  • #57 21398354
    divadiow
    Level 34  
    TR6260 ✅ - NTP driver now present

    scheduled echo event for +20s and +7 minutes into future happened as expected

    Screenshot of TR6260 device control panel showing NTP details and system statistics.

    Added after 26 [minutes]:

    RTL87X0C ✅

    same. fixed time in near future then one in 10 or 15 mins
    Device control panel displaying NTP connection and configuration information.

    Added after 15 [minutes]:

    XR809 ❌
    no Clock or NTP

    Screenshot of the OpenXR809_42D7AD9F user interface with control buttons.

    Added after 23 [minutes]:

    W600 ✅

    User interface for the OpenW600 device, displaying network time configuration details and other system parameters.

    Added after 1 [minutes]:

    will do more ESPs shortly

    Added after 2 [hours] 53 [minutes]:

    ESP32 ✅
    Control panel of the OpenESP32 device with connection and status information.

    ESP32C2 ✅
    Screenshot of device control panel OpenESP32C2_7A0F3D24 with info on local time, chip temperature, and Wi-Fi status.

    ESP32C6 ✅
    Screenshot of the user interface for the OpenESP32C6_BD0EFA94 device displaying system status information and configuration options.

    ESP32S2 ✅
    User interface of the OpenESP32S2 platform displaying system status information.

    ESP32C3 ✅
    OpenESP32C3 user interface displaying system information.
  • #58 21399140
    max4elektroda
    Level 20  
    Thank you so much, that's great!
  • #59 21564898
    4N6nerd
    Level 2  
    >>21304679
    Thanks for your inspiration!! It made me to build something similar for the CET time zone.
    
    //CET time zone
    alias winter_time ntp_timeZoneOfs 1
    alias summer_time ntp_timeZoneOfs 2
    
    winter_time
    
    // summertime
    alias check1 if $month>3 then summer_time
    alias check2 if $month==3&&31-$mday $day<=6&&$day>0 then summer_time
    alias check3 if $month==3&&31-$mday $day<=6&&$day=0&&$hour>=2 then summer_time
    
    // wintertime
    alias check4 if $month>10 then winter_time
    alias check5 if $month==10&&31-$mday $day<=6&&$day>0 then winter_time
    alias check6 if $month==10&&31-$mday $day<=6&&$day=0&&$hour>=3 then winter_time
    
    alias set_time backlog check1;check2;check3;check4;check5;check6
    
    waitfor NTPState 1
    set_time
    addClockEvent 04:00 0xff 1 set_time
    


    Some things are different:
    Sydney is southern hemisphere while CET is northern hemisphere. As a result summer and winter are inverted. That's why the CET version defaults to winter_time

    The really tricky part here is FIRST sunday vs LAST sunday:
    CET DST setting toggles on the LAST sunday of March and October instead of the FIRST sunday. So calculation had to be based on the length of the month. Luckily March and October are both 31 days. Hence the hardcoded 31. The hardcoded 31 should be aligned with the month. So if a DST toggle is done on the last sunday of September (like in New Zealand) then $month==9 and the hardcoded 31 should be changed to 30 for that month.
    As far as I know there are no timezones doing DST toggle in February ;) That would be extra challenging. Psssst..... I have an over engineered version of this script doing leap year and month length calculations using channels to store these values. If you need it.... let me know.

Topic summary

The discussion revolves around automating daylight saving time adjustments for NTP (Network Time Protocol) devices using OpenBeken firmware. Users express the need for a solution to avoid manual changes to the timezone offset twice a year due to daylight saving time in Australia. Various scripting approaches are proposed, including the use of logical operators (AND, OR) and nested IF statements to determine the correct timezone offset based on the date. Some users share scripts that successfully implement these adjustments, while others discuss the limitations of memory and firmware capabilities. The conversation also touches on the integration of MQTT and TuyaMCU for device communication and the challenges faced with LCD displays and firmware stability. Overall, the community collaborates to refine scripts and firmware features to enhance timekeeping functionality in their devices.
Summary generated by the language model.
ADVERTISEMENT