logo elektroda
logo elektroda
X
logo elektroda

Automating Daylight Savings Adjustments for NTP with Scripting or driver update

randomalias324 2514 58
ADVERTISEMENT
  • #1 21301007
    randomalias324
    Level 8  
    Hi

    OpenBeken is great.

    I have some devices that display the time. The time is sent to the device over NTP and this is relayed to the rest of the unit via TuyaMCU.

    This works fine. ntp_timeZoneOfs is used to set the timezone.

    BUT: Australia has daylight savings. I need to go in and adjust this offset in autoexec.bat code twice a year on about 5 devices.

    So, I have been looking to see if the NTP driver can handle daylight savings. looks It cant.

    Can I do it via autoexec.bat scripting?
    Theoretically I could wait for the first NTP sync, then check if the date is within a range and set ntp_timeZoneOfs accordingly.
    But, I don't know how I achieve this without ANDs and ORs or nested IF's

    So:
    Are there any AND or OR operators?
    Can I make code blocks?
    Is it possible to have nested if's?

    There seems to be plenty of rather advanced examples but very limited documentation about how scripting works.
    Have I missed the scripting documentation?

    Do you think it is possible to set the NTP offset based on date, through scripting or some other mechanism?

    I imagine the NTP driver could be modified. Give it a base offset, daylight saving start date, daylight saving end date, daylight saving offset for example:
    ntp_timeZoneOfs = 10
    // Daylight saving, when active, adds 1 hr
    ntp_daylightSavingOfs = 1
    // Use unadjusted time for setting limits as its simpler (ignore year)
    ntp_daylightSavingStart = 2000-10-05T16:00
    ntp_daylightSavingEnd = 2000-04-05T16:00


    This is still not ideal, as daylight saving is often 'the first Sunday of the month of October.
    Victorian government page with daylight saving rules
    But, that could be expressed as:
    ntp_timeZoneOfs = 10
    // Daylight saving, when active, adds 1 hr
    ntp_daylightSavingOfs = 1
    // Add daylight saving offset: October, sunday, 1st <day> of the month, 2am
    ntp_setDaylightSavingStart oct sun 1 02:00
    // Rem daylight saving offset: April, sunday, 1st <day> of the month, 2am
    ntp_setDaylightSavingEnd apr sun 1 02:00

    Needs to be expressed in local time to cover the possibility that the 1st of the the month falls on a sunday

    Cheers
  • ADVERTISEMENT
  • #2 21301146
    JacekCz
    Level 42  
    Time whether universal or astronomical has a deep meaning, and our 'civil' time will not eliminate it.
    In my opinion, certain devices should work in universal time without summer/winter time. E.g. in an automation system "something" happened between 2:00 and 3:00 a few days ago - there is no way to express this in jumping "civil" time.
    NTP rightly works as it does - in my opinion. The desire to change it stems from erroneous assumptions elsewhere

    And the conversion to local is, in my opinion, a matter of PRESENTATION at the viewer's interface, they may want it (they often do) or they may not.
  • #3 21301328
    randomalias324
    Level 8  
    >>21301146
    You're not wrong. For logging UTC is fine, even preferable. But, in this case the time is displayed on LCD screens throughout my house. It is for presentation. Yes, the main purpose of these devices is to log temperature and humidity, but if they are going to display time, I want it to be useful and correct.
    If we have an option for setting offset, it's not too much of a stretch to add in daylight saving adjustments
    The device on elektroda
  • Helpful post
    #4 21301498
    miegapele
    Level 15  
    It would be nice to have timezone support, but that probably takes too much space in flash to do properly....

    If supports && and || operators , so it should be possible to write a script to do timezone change. Except that it might not be exactly correct during the changeover hours, but that should be ok.
    I'll try to do an example later this weeek
  • Helpful post
    #5 21301565
    max4elektroda
    Level 20  
    miegapele wrote:
    but that probably takes too much space in flash to do properly

    That depends. I think it's quite possible with a decent memory footprint:

    You will need to code the DST "rules" (e.g. in Europe "start last sun in march at 2 local time" and "end last sun in October at 3 local (dst) time") and a function to convert this to timestamps (like e.g. tasmotas "RuleToTime").

    I did a realisation of the code, you can find the relevant parts starting here:

    https://github.com/MaxineMuster/OpenBK7231T_A...e_driver_ng/src/driver/drv_deviceclock.c#L346
  • ADVERTISEMENT
  • #6 21302332
    randomalias324
    Level 8  
    >>21301565
    ❤️❤️ For your work!

    Yeah, if its up to the user to specify the daylight saving rules, unlike windows linux that maintain a database of selectable timezones, I can't imagine it would take much flash.

    I'll have to brush up on my C and read your code.

    If I were to write it, I'd do something like this:
    Start with NTP supplied UTC time.
    Apply base timezone offset.
    If that time is within the range of daylight saving time:
    Apply the daylight savings offset.

    To calculate the start and end of the daylight saving period based on days, what you'd need is a reliable function to calculate the day of the week from a date. Then:
    Start with NTP supplied UTC time.
    Apply base timezone offset. (incase you've specified a day in the first week of January)
    Calculate what day of the week the 1st of the specified month is for the current year. (ie in this example, what day of the week October 1st is)
    Compute the day of of the month based on the rule (1 + [specified day of the week index] - [1st week day of month index] - + 7*(1-[occurrence] if [specified day of the week index] > [1st week day of month index] + 7)

    For example, say Monday to Sunday are indexed 0 to 6, we want the first Sunday of October. Say, the 1st of October is Tuesday:
    The 1st of of October is Tue and therefore gets the index 1. Specified day is 1st Sunday, index 0. 1 !> 6
    Day = 1 + 6 - 1 + 7*(1-1) = 6th

    Say Sunday to Saturday are indexed 0 to 6:
    The 1st of of October is Tue and therefore gets the index 2. Specified day is 1st Sunday, index 6. 2 > 0
    Day = 1 + 0 - 2 + 7*(1-1) + 7 = 6th

    You could do something similar if you needed the last day in a certain month.

    EDIT: A quick read reveals that this is basically what you've done :D

    You might be able to specify that in the function call with negative indexing, like python
    // Second last Sunday of October:
    ntp_setDaylightSavingStart oct sun -2 02:00
  • Helpful post
    #7 21302513
    max4elektroda
    Level 20  
    If you have some spare device and/or spare time feel free to play with the version here:

    https://github.com/MaxineMuster/OpenBK7231T_App/tree/new_time_ng

    You need to have a git account, then you can download the "artifacts" file at the end of the page which contains all firmwares for this branch:
    https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/11825574490

    The basic idea was to enable time keeping without network connection (without NTP), so this is the focus of the branch.

    This made it necessary to move a lot of functions to a "neutral" code, for most time related functions were flagged as "ntp_XY" for this was the only time source....

    You can e.g. set the clock simply to the browsers clock by clicking a link in the GUI.
    Timezone settings are done with a new (and kind of complex) command clock_setConfig (at least I tried to explain it in the source code, I can also give the needed code for a region).
  • ADVERTISEMENT
  • #8 21302691
    randomalias324
    Level 8  
    >>21302513
    Yep, I bought a few as a gift for my dad, so I do have spares
    Boxes of smart temperature and humidity sensors lying on a couch.

    I got the artefacts/binaries.

    You might need to help me with what to put in my autoexec.bat etc to get it going

    Currently it's this: Pretty basic.
    startDriver TuyaMCU
    startDriver NTP
    ntp_setServer 192.168.4.45
    ntp_timeZoneOfs 10:00
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 101 val 1
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 102 val 2
  • Helpful post
    #9 21302925
    max4elektroda
    Level 20  
    O.k.. let's start, but I'm afraid it will not directly be shown on the display, I'll need to do some adjustments (if I got it right, tuya clock is "set" by ntp time, which actually is not set by my DST code.)
    But this can be changed ...

    To set timezone, you could use the tasmota table for that and adjust the values to match "my" command:
    https://tasmota.github.io/docs/Timezone-Table/

    You will find e.g. for "ATC" (I just picked one entry with +10 h)

    Australia/ACT    Backlog0 Timezone 99; TimeStd 1,1,4,1,3,600; TimeDst 1,1,10,1,2,660


    We use an all in one setting command:

    Clock_SetConfig TZ,H,We,Me,De,he,Tstd,Ws,Ms,Ds,hs,Tdst
    TZ:   Timezone (+/-HH or +/-HH:MM or 99 for DST)
    

    To "match" the two Tasmota commands to our

    TimeStd H,W,M,D,h,T --> He=H; We=W; Me=M; De=D; he=h; Tstd=T
    TimeDST H,W,M,D,h,T --> Hs=H; Ws=W; Ms=M; Ds=D; hs=h; Tdst=T-Tstd
    


    If I did the transfer correct, this would be the setting:

    Clock_SetConfig 99,1,1,4,1,3,600,1,10,1,2,60
     
    Meaning:
    99:      Use DST
    1:      Southern Hemisphere
    1,4,1,3:   DST ends / standard/winter time starts at first Aprils sunday at 3 (local summer time)
          (yes, sounds a bit strange, but the words should match the numbers ;-))
    600:      during "standard" time you are 600 Minutes = 10 hours ahead of UTC
    1,10,1,2:   DST starts at first Octobers sunday at 2 local time 
    60:      DST is 60 minutes ahead of standard time
    

    That looks o.k. for me

    The settings of "Clock_SetConfig ..." are kept in flash, so you only need to set them once, unless you need a change.

    Actually DST is not used with NTP (to keep NTP exactly working as before).

    To test the Clock without NTP you can "fake" any time you whant by setting the time with an HTTP-Request:

    http://<your IP>/pmntp?EPOCH=<UTC epoch>&OFFSET=<local clock offset seconds (including DST-offset during DST time)>

    Since usually this is done with javascript, the OFFSET will always be the "complete" offset versus UTC.

    In the log (and the time displayed in GUI) you should see DST working if you set the time shortly before a DST change.

    I'll take a look into the tuya and NTP code to see, how I can set NTP offset following DST settings.

    Added after 1 [hours] 57 [minutes]:

    You may try this new firmwares, they should set NTP offset, too when DST changes:

    https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/11838463085

    Caution, this is not foolproof. E.g. when DST is set, it will ignore whatever you set for "ntp_timeZoneOfs" but only use the "Clock_SetConfig" values.

    I didn't test it yet, but it looks like it's working (since it breaks the windows simulation by changing the time to DST ;-)):

    2024-11-14T13:22:35.3741400Z Info:NTP:NTP driver initialized with server=217.147.223.78, offset=0
    2024-11-14T13:22:35.3741827Z 
    2024-11-14T13:22:35.3741932Z Info:MAIN:Started NTP.
    [...snipp...]
    2024-11-14T13:22:35.3745214Z Info:MAIN: Date: 2022-06-10T09:27:35, idle 0/s, free 100000, MQTT 0(57), bWifi 1, secondsWithNoPing 596, socks 1/9999 
    2024-11-14T13:22:35.3745866Z 
    2024-11-14T13:22:35.3746199Z Info: epoch 1654853255 in year 2022 -- b=1648342800 -- e=1667091600
    2024-11-14T13:22:35.3746596Z 
    2024-11-14T13:22:35.3746600Z 
    2024-11-14T13:22:35.3746604Z 
    2024-11-14T13:22:35.3746608Z 
    2024-11-14T13:22:35.3746948Z In DST of 2022. Next switch at 1667091600 (Sun, 2022-10-30 01:00:00 UTC)
    2024-11-14T13:22:35.3747367Z 
    2024-11-14T13:22:35.3747847Z Info:MAIN:DST switch from normal time to DST at epoch 1654853255 -- next switch at 1667091600!! 
    2024-11-14T13:22:35.3748420Z 
    2024-11-14T13:22:35.3748590Z Info:GEN:CHANNEL_Add channel 1 has changed to 54
    2024-11-14T13:22:35.3749124Z 
    2024-11-14T13:22:35.3749130Z 
    2024-11-14T13:22:35.3749341Z Info:GEN:CHANNEL_Set channel 1 has changed to 54 (flags 0)
    2024-11-14T13:22:35.3749707Z 
    2024-11-14T13:22:35.3749711Z 
    2024-11-14T13:22:35.3749881Z Info:GEN:CHANNEL_Add channel 3 has changed to 312
    2024-11-14T13:22:35.3750202Z 
    2024-11-14T13:22:35.3750206Z 
    2024-11-14T13:22:35.3750416Z Info:GEN:CHANNEL_Set channel 3 has changed to 312 (flags 0)
    2024-11-14T13:22:35.3750793Z 
    2024-11-14T13:22:35.3750797Z 
    2024-11-14T13:22:35.3750958Z Info:GEN:CHANNEL_Add channel 3 has changed to 400
    2024-11-14T13:22:35.3751267Z 
    2024-11-14T13:22:35.3751271Z 
    2024-11-14T13:22:35.3751435Z Info:GEN:CHANNEL_Set channel 3 has changed to 400 (flags 0)
    2024-11-14T13:22:35.3751725Z 
    2024-11-14T13:22:35.3751729Z 
    2024-11-14T13:22:35.3752214Z Info:MAIN: Date: 2022-06-10T11:27:36, idle 0/s, free 100000, MQTT 0(57), bWifi 1, secondsWithNoPing 597, socks 1/9999 
    2024-11-14T13:22:35.3752844Z 
    2024-11-14T13:22:35.3753306Z Info:MAIN: Date: 2022-06-10T11:27:37, idle 0/s, free 100000, MQTT 0(57), bWifi 1, secondsWithNoPing 598, socks 1/9999 
    2024-11-14T13:22:35.3753802Z 
    


    Added after 5 [hours] 8 [minutes]:

    Did some tests and adjustments. latest version is here:

    https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/11843479155

    I tested with a python "fakeNTP" from here:

    https://github.com/CuckooEXE/FakeNTP

    So here is the DST change in 2029 in EU (using "sudo python3 FakeNTP.py --static-time --time 1869094795"):

    Info:NTP:Seconds since Jan 1 1900 = 4078083595
    Info:NTP:Unix time  : 1869101995
    Info:NTP:Local Time : 2029-03-25 02:59:55
    Info:MAIN: Date: 2029-03-25T02:59:55, idle 0/s, free 35272, MQTT 0(17), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info: epoch 1869094795 in year 2029 -- b=1869094800 -- e=1887843600
    
    
    Before first DST switch in 2029. Next switch at 1869094800 (Sun, 2029-03-25 01:00:00 UTC)
    Info:MAIN:DST switch back from DST at epoch 1869094795 -- next switch at 1869094800!! 
    Info:MAIN: Date: 2029-03-25T01:59:56, idle 0/s, free 34036, MQTT 0(17), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T01:59:57, idle 0/s, free 34204, MQTT 0(17), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MQTT:mqtt_host empty, not starting mqtt
    Info:MAIN: Date: 2029-03-25T01:59:58, idle 0/s, free 36480, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T01:59:59, idle 0/s, free 34368, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T02:00:00, idle 0/s, free 36800, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T02:00:01, idle 0/s, free 33288, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info: epoch 1869094801 in year 2029 -- b=1869094800 -- e=1887843600
    
    
    In DST of 2029. Next switch at 1887843600 (Sun, 2029-10-28 01:00:00 UTC)
    Info:MAIN:DST switch from normal time to DST at epoch 1869094801 -- next switch at 1887843600!! 
    Info:MAIN: Date: 2029-03-25T03:00:02, idle 0/s, free 37036, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T03:00:03, idle 0/s, free 38060, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-03-25T03:00:04, idle 0/s, free 36260, MQTT 0(18), bWifi 1, secondsWithNoPing 1, socks 0/0


    And the switch back ("sudo python3 FakeNTP.py --static-time --time 1887843595"):

    Info:NTP:Seconds since Jan 1 1900 = 4096832395
    Info:NTP:Unix time  : 1887850795
    Info:NTP:Local Time : 2029-10-28 02:59:55
    Info:MAIN: Date: 2029-10-28T02:59:55, idle 0/s, free 35096, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info: epoch 1887843595 in year 2029 -- b=1869094800 -- e=1887843600
    
    
    In DST of 2029. Next switch at 1887843600 (Sun, 2029-10-28 01:00:00 UTC)
    Info:MAIN:DST switch from normal time to DST at epoch 1887843595 -- next switch at 1887843600!! 
    Info:MAIN: Date: 2029-10-28T02:59:56, idle 0/s, free 35304, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T02:59:57, idle 0/s, free 35096, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T02:59:58, idle 0/s, free 35096, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T02:59:59, idle 0/s, free 35132, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T03:00:00, idle 0/s, free 35132, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T03:00:01, idle 0/s, free 34652, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info: epoch 1887843601 in year 2029 -- b=1869094800 -- e=1887843600
    
    
    After DST in 2029. Next switch at 1901149200 (Sun, 2030-03-31 01:00:00 UTC - thats next year)
    Info:MAIN:DST switch back from DST at epoch 1887843601 -- next switch at 1901149200!! 
    Info:MAIN: Date: 2029-10-28T02:00:02, idle 0/s, free 35132, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T02:00:03, idle 0/s, free 34924, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    Info:MAIN: Date: 2029-10-28T02:00:04, idle 0/s, free 34960, MQTT 0(38), bWifi 1, secondsWithNoPing 1, socks 0/0 
    


    [Edit on 20241116: corrected wrong (swapped) description of summer/winter time in code block for Australia]
  • #10 21303719
    randomalias324
    Level 8  
    >>21302925
    Love your work, Sir!

    Will spin it up tonight if we don't go for beers after work
  • ADVERTISEMENT
  • #11 21303888
    max4elektroda
    Level 20  
    randomalias324 wrote:
    ... if we don't go for beers after work

    First things first! Cheers!  🍻
  • #12 21304679
    miegapele
    Level 15  
    Following script should work to set timezone automatically in Sydney
         //Sydney tome zone
    alias winter_time ntp_timeZoneOfs 10
    alias summer_time ntp_timeZoneOfs 11
    
    summer_time
    
    alias check1 if $month>4 then winter_time
    alias check2 if $month==4&&$mday-$day>0&&$day>0 then winter_time
    alias check3 if $month==4&&$mday-$day>0&&$day==0&&$hour>=3 then winter_time
    
    alias check4 if $month>10 then summer_time
    alias check5 if $month==10&&$mday-$day>0&&$day>0 then summer_time
    alias check6 if $month==10&&$mday-$day>0&&$day==0&&$hour>=3 then summer_time
    
    alias set_time backlog check1;check2;check3;check4;check5;check6
    waitfor NTPState 1
    set_time
    addClockEvent 04:00 0xff 1 set_time



    The tricky part is $mday - $day being positive on first Sunday or later which is what we need for time zone change
  • #13 21305059
    max4elektroda
    Level 20  
    Good work!
    Just one additional note: The script should work with the release version, so no need for the automated approach in the alternate firmware version.

    Added after 7 [hours] 46 [minutes]:

    miegapele wrote:
    addClockEvent 04:00 0xff 1 set_time

    Since we did call "set_time" once before the recurring event, we can be sure, the time is set correctly to winter_time/summer_time.

    So if I'm not missing something, it should be o.k to do the recurring event
    only on Sundays
    at 3:01

    so we have a maximum "derivation" for 1:01 hours?

    We always will have the event called twice when turning time back, no matter when
  • #14 21306212
    randomalias324
    Level 8  
    Had a difficult time using cloudcutter on these units for some reason. Eventually got there.
    I think running it off the VM on my main machine was changing modes on the WiFi device too slowly. Worked better on a dedicated Linux machine (that I had to set up 😒)

    Anyway, keen to try both approaches now I can update the firmware easily.

    But hey, are there any docs that describe the autoexec "language"??
    Really keen to get an answer on that. Here is the list of constants, functions, etc and a few examples but nothing more general, like list of operators, setting variables, i.e. general syntax. Is it just bash? A simplified bash? If so, what is the subset?
    Like, I couldn't find out that && and || operators existed till you told me miegapele.

    Can I set my own simple variables? If so, how? Is it just like bash?
    var = 10
    ntp_timeZoneOfs $var


    or does alias work like that
    alias var 10
    ntp_timeZoneOfs var


    Can I put logical statements in parentheses?

    I would love to contribute a docs page but my level of understanding is not there
  • #15 21306272
    miegapele
    Level 15  
    There is no general description, what you found is probably everything, you can read code or search this forum for more :)

    There is no variables, but you can use channel values to simulate that to some extent(there is 64 channels to use I think)
    There is if statement and then there is backlog to run multiple statements. if statement now supports parenthesis I think
    Also, there is also label and goto. 

    That's mostly it, I think.
  • #16 21306671
    randomalias324
    Level 8  
    >>21302925
    Hey mate I couldn't get your build to work properly.

    Got it flashed (like this)
    Screenshot of an OTA (Over-The-Air) interface showing information about the BK7231T chipset and file details.

    This is what one of my existing sensors looks like
    Screenshot of a user interface showing information on temperature, humidity, and device status.

    This is what the one with the new firmware is doing.
    I note that on the bottom of the page, near the build version, there is an extra field called device time.
    When no time has been set, there is a hyperlink to set it to browser time.
    User interface for the OpenBK7231T device displaying temperature, humidity, and network status information.

    Note that I have set my PC to Korean time (randomly selected) to test whether the page was getting the time from the device (in which case it would have needed to know the timezone string, which I doubted it did) or from the browser. It seems to get it from the browser.

    Note that the time is a couple of minutes fast compared to the NTP supplied time and the local PC time.

    Having the Clock_SetConfig in autoexec does not seem to alter the time displayed on the page.

    With NTP turned off in autoexec I can set the time displayed on the page using the browser command /pmntp?EPOCH=1731205706&OFFSET=0 (as an example)

    In neither case is the time correct on the LCD screen of the physical device. So the time is not getting passed through TuyaMCU. It just shows UTC time.

    Also the device appears to crash regularly.

    This was my autoexec
    startDriver TuyaMCU
    startDriver NTP
    //ntp_setServer 192.168.4.45
    //ntp_timeZoneOfs 10:00
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 101 val 1
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 102 val 2
    Clock_SetConfig 99,1,1,4,1,3,600,1,10,1,2,60
  • #17 21306789
    max4elektroda
    Level 20  
    Thanks for your test.
    Before I forget it: The Clock_SetConfig command only needs to be set once, no need to set in autoexec (though afaik setting the same values again shouldn't trigger a flash write).

    The "Clock_SetConfig" setting should set the clock according to the UTC time and DST.

    randomalias324 wrote:
    It seems to get it from the browser.

    Yes, time is taken from the browser and if the clock is set via HTTP-Link (with wget or from the gui) the time received is taken as "correct", as Javascript should always report the correct local time depending on the local settings (with or without DST). After the time was reported, the routine only checks, whether we are in DST or not to decide, if the offset reported is "only" TZ offset or "TZ+DST" offset. But in the end, the time of the clock will be the time reported by the browser.

    The clock is only changed if there is an DST switch after the initial setting, otherwise the clock simply counts on.

    The accuracy of the time depends on the time of the browsers device, so it might wrong if the browsers device clock is not exact (plus a little overhead from the handling). This method is not as exact as ntp, but it should be sufficient for many cases.

    So, which time is displayed on the LCD? I expected it to be the NTP time including the offset?
    So in this case it's neccessary to start NTP and setting the clock via NTP to some seconds before an DST change (e.g. to Oct 6th 2024 at 01:59:55 UTC+10) then I would expect at least the NTP time on the main page to switch to 3:00:0X during the first few seconds after 2:00).

    I'll try this later.

    If you can confirm the "LCD time" is the same as the "NTP time as seen on main page" I also can take a look if I can change the time source to the "device-time" as reported in the bottom lines.

    Added after 1 [hours] 34 [minutes]:

    O.k, found some errors in the code, so DST isn't working as described atm:
    I did something wrong or command handling changed, so now you will need another syntax for Clock_SetConfig (comma no longer works, need space):

    So one pint is, the correct setting will be:
    Clock_SetConfig 99 1 1 4 1 3 600 1 10 1 2 60


    And there was an error when calculating the switching time in UTC.
    In your case it will be "one (UTC) day earlier" and I didn't take this into account...

    Please stay tuned...

    Added after 17 [minutes]:

    Hope it's fixed now.

    Using "Clock_SetConfig" as above.
    Getting epoch from "5 seconds before switch":
     date --date='TZ="Australia/Sydney" 20241006 01:59:55' +%s
    1728143995


    and sending "fake" time:
    sudo python3 FakeNTP.py --static-time --time 1728143995






    As you see, only NTP is set to the correct time, the device time is still showing local time in Germany and this display is not updated...

    New artifacts can be found here:

    https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/11880370107

    Added after 10 [minutes]:

    Oops, didn't correct the display for "DST in next year" - but it's only an information, the calculation should be o.k.. Will be fixed in some minutes ...

    Added after 3 [hours] 12 [minutes]:

    https://github.com/MaxineMuster/OpenBK7231T_App/actions/runs/11880470957
  • #18 21307605
    randomalias324
    Level 8  
    Alright. Looks like it works!

    Flashed .rbl OTA
    Set the following autoexec.bat
    startDriver TuyaMCU
    startDriver NTP
    //ntp_setServer 192.168.4.45
    //ntp_timeZoneOfs 10:00
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 101 val 1
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 102 val 2

    Ran the command as a 'Custom Command'
    OpenBK7231T tool screen displaying control commands for a device.

    And we've got time displayed
    Monitor display with system information and a timer next to it.

    Still have LCD issues. Now, it doesn't seem to actually crash, can still load the landing page. But, the LCD looks like it resets. Including the wifi level indicator.
    This happens every minute to every few minutes
    Computer screen with visible clock and LCD display showing temperature.
    Could it be setting the clock often? Is it sending junk via TuyaMCU that upsets the LCD driver?
  • #19 21307760
    max4elektroda
    Level 20  
    Thanks again for your testing!
    I understand that the issues with the LCD are not present in the release firmware?
    I can try to silence the version a bit, since it's using new features there might be to much "debugging output".
    And if compared to the other firmware please check if this might be related to the usage of the web gui, which takes quite some resources.
  • #20 21308016
    randomalias324
    Level 8  
    >>21307760
    Yeah I have 4 that are flashed with the release firmware ntp_setOffet 10 and they are flawless (except for daylight savings rollover, of course)

    I've played around with it a little tonight. It feels like MCU load could be a possibility. The LCD is still pausing regularly, every couple of minutes.

    Its running currently, but I can't load the page to check up time, but I think its been up this whole time.

    I was able to get into the main page and open the javascript app which let me take a few logs. You know how it normally trickles in, through a constant stream of messages maybe 1 or two lines a second with some faster bursts. When the LCD stops and goes 00:00 the logging pauses for maybe 15 seconds. Twice I was able to pause logging, clear history, and re-enable while it was paused. When it comes back you immediately get about 30 lines or so appear nearly instantly. And they all come back with the same set of messages. One in particular is noteworthy:
    Error:CMD:no file early.bat err -2


    Actually, just pasting in the snapshots, I think it could be resetting - Looks like its running autoexec.bat and reinitialising the drivers each time. Although WiFi doesn't drop, so its not like a hard reset?

    Anyway I don't fully understand. I just power cycled it, still cant get into the web page.

    Snapshot 1:
    nges count.
    Error:CMD:no file early.bat err -2
    Info:GEN:PIN_SetupPins pins have been set up.
    Info:MAIN:Main_Init_Before_Delay done
    Info:MAIN:Main_Init_Delay
    Info:MAIN:Main_Init_Delay done
    Info:MAIN:Main_Init_After_Delay
    Info:MAIN:Using SSID [homearseistant]
    Info:MAIN:Using Pass [MonItoring_Various_Sheeite]
    Info:MQTT:MQTT_RegisterCallback called for bT obk066B1CE5/ subT obk066B1CE5/ /set
    Info:MQTT:MQTT_RegisterCallback called for bT bekens_t/ subT bekens_t/ /set
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/obk066B1CE5/ subT cmnd/obk066B1CE5/ 
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/bekens_t/ subT cmnd/bekens_t/ 
    Info:MQTT:MQTT_RegisterCallback called for bT obk066B1CE5/ subT obk066B1CE5/ /get
    Info:NTP:CLOCK driver initialized.
    Info:CMD:CMD_StartScript: started autoexec.bat at the beginning
    Info:MAIN:Main_Init_After_Delay done
    Info:MAIN:Started TuyaMCU.
    Info:NTP:NTP driver initialized with server=217.147.223.78, offset=0
    Info:MAIN:Started NTP.
    Info:GEN:Channel 1 type changed to temperature_div10
    ...


    nges count.
    Error:CMD:no file early.bat err -2
    Info:GEN:PIN_SetupPins pins have been set up.
    Info:MAIN:Main_Init_Before_Delay done
    Info:MAIN:Main_Init_Delay
    Info:MAIN:Main_Init_Delay done
    Info:MAIN:Main_Init_After_Delay
    Info:MAIN:Using SSID [homearseistant]
    Info:MAIN:Using Pass [MonItoring_Various_Sheeite]
    Info:MQTT:MQTT_RegisterCallback called for bT obk066B1CE5/ subT obk066B1CE5/ /set
    Info:MQTT:MQTT_RegisterCallback called for bT bekens_t/ subT bekens_t/ /set
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/obk066B1CE5/ subT cmnd/obk066B1CE5/ 
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/bekens_t/ subT cmnd/bekens_t/ 
    Info:MQTT:MQTT_RegisterCallback called for bT obk066B1CE5/ subT obk066B1CE5/ /get
    Info:NTP:CLOCK driver initialized.
    Info:CMD:CMD_StartScript: started autoexec.bat at the beginning
    Info:MAIN:Main_Init_After_Delay done
    Info:MAIN:Started TuyaMCU.
    Info:NTP:NTP driver initialized with server=217.147.223.78, offset=0
    Info:MAIN:Started NTP.
    Info:GEN:Channel 1 type changed to temperature_div10
    ...
  • #21 21308033
    randomalias324
    Level 8  
    Managed to get back into the app and take some logs just prior to freezing. The last line is the last thing received before it resets.

    1
    ...
    Info:MAIN: Date: 2024-11-18T21:25:37, idle 256478/s, free 82160, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:GEN:dhcp=0 ip=192.168.4.44 gate=192.168.4.134 mask=255.255.255.0 mac=50:8a:06:6b:1c:e5
    Info:GEN:sta: 1, softap: 0, b/g/n
    Info:GEN:sta:rssi=-61,ssid=homearseistant,bssid=00:24:2b:ee:d5:d7,channel=4,cipher_type:MIXED
    Info:MAIN: Date: 2024-11-18T21:25:38, idle 250531/s, free 82160, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:Received: 55 AA 03 00 00 01 01 04 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 0 (Hearbeat) len 8
    Info:TuyaMCU:Received: 55 AA 03 24 00 00 26 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 36 (SetRSSI) len 7
    Info:TuyaMCU:ProcessIncoming: received TUYA_CMD_SET_RSSI, so sending back signal strength
    <crash>


    2
    ...
    Info:TuyaMCU:Received: 55 AA 03 2B 00 00 2D 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 43 (NetworkStatus) len 7
    Info:TuyaMCU:ProcessIncoming: (test for S09 calendar/IR device) received TUYA_CMD_NETWORK_STATUS 0x2B 
    Info:MAIN: Date: 2024-11-18T21:26:43, idle 251046/s, free 82152, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MAIN: Date: 2024-11-18T21:26:44, idle 514034/s, free 82152, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MAIN: Date: 2024-11-18T21:26:45, idle 248313/s, free 82152, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:Received: 55 AA 03 00 00 01 01 04 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 0 (Hearbeat) len 8
    Info:TuyaMCU:Received: 55 AA 03 24 00 00 26 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 36 (SetRSSI) len 7
    Info:TuyaMCU:ProcessIncoming: received TUYA_CMD_SET_RSSI, so sending back signal strength
    Info:TuyaMCU:Received: 55 AA 03 1C 00 00 1E 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 28 (SetTime) len 7
    Info:TuyaMCU:ProcessIncoming: received TUYA_CMD_SET_TIME, so sending back time
    Info:TuyaMCU:MCU time to set: 1731965205
    Info:TuyaMCU:ptime ->gmtime => tm_hour: 21
    Info:TuyaMCU:ptime ->gmtime => tm_min: 26
    <crash>


    3
    ...
    Info:MAIN: Date: 2024-11-18T21:27:45, idle 245971/s, free 82160, MQTT 0(3), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:Received: 55 AA 03 2B 00 00 2D 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 43 (NetworkStatus) len 7
    Info:TuyaMCU:ProcessIncoming: (test for S09 calendar/IR device) received TUYA_CMD_NETWORK_STATUS 0x2B 
    Info:MAIN: Date: 2024-11-18T21:27:46, idle 255131/s, free 82160, MQTT 0(3), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MQTT:mqtt_host empty, not starting mqtt
    Info:MAIN: Date: 2024-11-18T21:27:47, idle 251953/s, free 82160, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:GEN:dhcp=0 ip=192.168.4.44 gate=192.168.4.134 mask=255.255.255.0 mac=50:8a:06:6b:1c:e5
    Info:GEN:sta: 1, softap: 0, b/g/n
    Info:GEN:sta:rssi=-61,ssid=homearseistant,bssid=00:24:2b:ee:d5:d7,channel=4,cipher_type:MIXED
    Info:MAIN: Date: 2024-11-18T21:27:48, idle 249579/s, free 82160, MQTT 0(4), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:Received: 55 AA 03 00 00 01 01 04 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 0 (Hearbeat) len 8
    Info:TuyaMCU:Received: 55 AA 03 24 00 00 26 
    Info:TuyaMCU:ProcessIncoming[v=3]: cmd 36 (SetRSSI) len 7
    Info:TuyaMCU:ProcessIncoming: received TUYA_CMD_SET_RSSI, so sending back signal strength
    <crash>
  • #23 21308069
    randomalias324
    Level 8  
    Hmm, this is strange. I just set up the WiFi connection on one with release firmware to capture a baseline log and I'm getting the same behavior with it... v1.17.689, same as all the others in my house that operate with no issue

    I can see MQTT is changing channels before some of the crashes, and I haven't enabled MQTT...

    I might try setting the one with the modified firmware with a connection to the MQTT broker and see if that alters the behavior

    Also grabbed a log from one of my working sensors which has 20 days up time
  • #24 21308108
    randomalias324
    Level 8  
    Well there you bloody go. When connected to the MQTT broker, the new build works fine.
    Before that I also added in the IR pins to spin up the IR driver, but that did not appear to do much.
    OpenBK7231T software interface showing network and system status information.
    Attached is a log for good measure

    So I think I'll flash this to the ones I'm giving to my father, that way we can reconfigure everything through the native webpage and avoid the javascript app (which, unless I'm wrong, needs connectivity to the internet and the IoT wifi, if they're separate)

    For mine, I might now play around with miegapele's script which will let me avoid flashing them. And it will test another approach that some others might like to use.

    Great firmware max4elektroda, it's an excellent feature to support clock-like devices.

    I wonder if the MQTT thing is a bug in release? Surely being off MQTT shouldn't cause resets
  • #25 21308139
    max4elektroda
    Level 20  
    Thank you so much for your time spent to investigate, especially for beeing successfull in finding the cause!

    I must addmit that if it comes to MQTT I'm no help at all. I simply don't use it and have no knowledge.
  • #26 21308160
    randomalias324
    Level 8  
    >>21308139
    Well I hope it's not crashing your devices. Keep an eye out for it.
    I guess it could be an interaction between MQTT and TuyaMCU, but the only other thing I had running was NTP. I hadn't even touched the MQTT settings

    Anyway no problem! Thank you for sharing your great feature! ❤️ The least I could do was test it
  • Helpful post
    #27 21310759
    p.kaczmarek2
    Moderator Smart Home
    I've seen this thread a bit late, but can you check what is the cause of the reboot, more specifically, is it really a crash in the WiFi module firmware, or maybe it's just that WiFi module CEN pin is connected to the MCU (TuyaMCU) and TuyaMCU forces WiFi module reboot when there is no WiFi state 0x04?
    Can you check if the "crash" occurs if you do:
    
    tuyaMcu_defWiFiState 4
    

    in autoexec.bat? tuyaMcu_defWiFiState 4 means that OBK sends to TuyaMCU wifi state "connected to cloud", it's only send either if forced with command or if there is MQTT connected...

    So, I suspect, that there may be no "crash", it's just the MCU resetting WiFI module... but I will investigate.

    Added after 27 [minutes]:

    EDIT: There is nothing related to MQTT in the send time function:
    View of Visual Studio debugging window with program code for TuyaMCU.
    For now, I need one thing to progress. Please check if it also crashes if you add mentioned "default wifi state" setting to autoexec.bat.

    Added after 49 [minutes]:

    I've also added a self test so you can debug it yourself on Windows:

    Screenshot of software debugging in Visual Studio 2017 development environment.
    Helpful post? Buy me a coffee.
  • #28 21311015
    randomalias324
    Level 8  
    >>21310759
    Hey mate, I don't know how to determine what is causing the 'crash', I don't understand the logs well enough. I have listed quite a few logs above, if you wanted to dig in.

    All I know is that: When I configured MQTT, without changing anything else, the behavior stopped.

    Considering the web page doesn't load after the LCD drops out, and when the LCD information comes back it does so in the following sequence:
    1) Time is displayed as the NTP offset
    2) WiFi indicator displays signal a second or so later
    3) The time is set to the correct time and date, indicating NTP has synced
    suggests that the WiFi is dropping out.

    I have added tuyaMcu_defWiFiState 4 to autoexec.bat, and it looks like it's stopped the behavior.
    So autoexec.bat now it looks like this:
    (I am currently experimenting with some things)
    startDriver TuyaMCU
    startDriver NTP
    //ntp_setServer 192.168.4.45
    ntp_timeZoneOfs 11:00
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 101 val 1
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 102 val 2
    tuyaMcu_defWiFiState 4
    setChannelType 4 ReadOnly
    setChannelType 5 ReadOnly
    setChannelType 6 ReadOnly
    setChannelType 7 ReadOnly
    setChannelType 8 ReadOnly
    setChannelType 9 ReadOnly
    waitFor NTPState 1
    setChannelFloat 4 $year-1
    setChannelFloat 5 $month
    setChannelFloat 6 $mday
    setChannelFloat 7 $day
    setChannelFloat 8 $hour
    setChannelFloat 9 $minute


    EDIT: Oh now I completely understand you. It must be connected to that pin, like you said. Sounds like it's an uncommon issue, as it requires the other MCU to be connected in this particular way AND be programmed to send the reset command.

    I wonder if it is a good idea to send the 'connected to cloud' signal regardless of MQTT when MQTT is not configured? Same with WiFi. It might suppress confusing issues / random resets with some of these devices while people are setting up their system
  • Helpful post
    #29 21312619
    p.kaczmarek2
    Moderator Smart Home
    Thank you for testing and your activity (I mean, all users in this topic), I really appreciate it.
    randomalias324 wrote:

    I wonder if it is a good idea to send the 'connected to cloud' signal regardless of MQTT when MQTT is not configured

    Well, now it brings another issue - there are TuyaMCU door sensors (or water sensors, or humidity sensors) that are using this MQTT mechanism to know when they can report state to HA and then go back to sleep ...
    But from what I can see, you want to specifically add an exception to when MQTT is not configured, not just "not connected"?
    Very well, I can agree to that. Here's the first draft:
    Screenshot of a code snippet with added lines regarding MQTT configuration.
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1434/files
    Can you check it that's what works for you? Shall I merge it?

    Futhermore, I also need opinion on that:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1429
    maybe also from @divadiow, is it ready to merge?
    Helpful post? Buy me a coffee.
  • #30 21313456
    randomalias324
    Level 8  
    >>21312619
    OK, I'll give it a flash when I have time tonight :)

    Quote:
    You want to specifically add an exception to when MQTT is not configured, not just "not connected"?

    Yeah exactly :) The default behavior I guess seemed like a surprise, as I thought there were use cases for devices without MQTT.

    But yeah, the case with the door sensors is interesting let's think about it. So, for people with the door device, with the modified logic they will now be going to sleep before they've set up MQTT. Do you know time delay? Is there a confirmation mechanism that confirms an MQTT message has been successfully sent, if so do the devices use that? I'm just trying to put myself in the shoes of someone with one of those devices. So I've just connected it to WiFi, and I'm now in there setting up the MQTT user password and it goes to sleep. I suppose I might be expecting it to go to sleep when connected, so I would go back and put it in hot-spot mode for further config or try to keep it awake with multiple door opening closings? I'm not sure what it would be like.

    When I'm setting these up I usually put WiFi settings and MQTT in at the same time. I am just not currently doing it because I am concentrating on the clock functionality

    I guess that's a long winded way of saying the new logic seems more logical from my perspective and any biases I have from working with these clocks. But not everyone is me

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