logo elektroda
logo elektroda
X
logo elektroda

[BK7231N] I'm thinking about adding sunrise and sunset to NTP events, anyone interested?

rickbronson 3639 59
ADVERTISEMENT
  • #31 20861998
    rickbronson
    Level 3  

    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...
  • ADVERTISEMENT
  • #33 20865997
    p.kaczmarek2
    Moderator Smart Home
    Thank you, I will review it today or tomorrow morning.
    Helpful post? Buy me a coffee.
  • #34 20869118
    omniron
    Level 10  

    I can try it out over the weekend. I have a unit that controls an outdoor light.
    Configuration interface for outdoor lighting control timer.
    Thank you both for implementing this.
  • #35 20869378
    p.kaczmarek2
    Moderator Smart Home
    @rickbronson merged, but not enabled yet, I am adding also self tests for the code:
    https://github.com/openshwprojects/OpenBK7231...mmit/bc580f0d0acba49846b0d554e29a251b5c2f3679

    Added after 15 [seconds]:

    @rickbronson why did you change default ntp time offset?
    Helpful post? Buy me a coffee.
  • #36 20869402
    rickbronson
    Level 3  

    > @rickbronson Why did you change the default NTP time offset?

    Not really sure, to be honest. You are welcome to change it back.
    Rick
  • ADVERTISEMENT
  • #37 20869410
    p.kaczmarek2
    Moderator Smart Home
    No problem, it just suprised me because suddenly my self tests were failing because they were written at the time when the offset was 0.

    We're still testing, but it's merged now. Good job!
    Helpful post? Buy me a coffee.
  • #38 20869471
    DeDaMrAz
    Level 19  
    @rickbronson

    Did it work for you? If so, how big is discrepancy with clocks and ON/OFF action if any?

    And can you share your final autoexec here maybe?

    EDIT: exclude your GPS coordinates please if you share your autoexec.bat
  • #39 20869623
    p.kaczmarek2
    Moderator Smart Home
    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.
    Helpful post? Buy me a coffee.
  • #40 20870355
    rickbronson
    Level 3  

    >>20869471

    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
  • #41 20870698
    p.kaczmarek2
    Moderator Smart Home
    Helpful post? Buy me a coffee.
  • #42 20878116
    ilengyel
    Level 11  
    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:

    PowerSave 1
    ntp_setServer 192.168.1.12
    startDriver ntp
    ntp_timeZoneOfs 11
    ntp_setLatlong <lat> <long>
    
    alias high_lights backlog led_temperature 300; led_dimmer 30; led_enableAll 1; echo lights_set_high
    alias low_lights backlog led_temperature 500; led_dimmer 5; led_enableAll 1; echo lights_set_low
    alias off_lights backlog led_enableAll 0; echo lights_set_off
    
    addClockEvent sunset 0xff 1 high_lights
    addClockEvent 21:00 0xff 2 low_lights
    addClockEvent 23:00 0xff 3 off_lights
    
    waitFor NTPState 1
    
    // Set initial light state to match the above clock events
    // sunset - 21:00   lights set to high
    // 21:00 - 23:00    lights set to low
    // 23:00 - sunset   lights turned off 
    if ($hour>$sunset_hour||($hour=$sunset_hour&&$minute>=$sunset_minute))&&$hour<21 then high_lights
    if $hour>=21&&$hour<23  then low_lights
    if $hour>=23||($hour<$sunset_hour||($hour=$sunset_hour&&$minute<$sunset_minute)) then off_lights


    Another thing, would there be a neat way to delay the sunset event by about 30mins to mimic the Civil sunset mode in tasmota ?
  • #43 20879224
    rickbronson
    Level 3  

    > Another thing, would there be a neat way to delay the sunset event by about 30 minutes to mimic the Civil sunset mode in Tasmota?

    Yes, I thought about that because I am familiar with the Tasmota features. I really haven't looked into what it would take to do this.
  • Helpful post
    #44 20879370
    p.kaczmarek2
    Moderator Smart Home
    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)
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #47 20917700
    p.kaczmarek2
    Moderator Smart Home
    Hello, are all the self-tests passing now?

    Did you add the new autoexec sample to the JSON source file of docs?
    Helpful post? Buy me a coffee.
  • #48 20917760
    ilengyel
    Level 11  

    p.kaczmarek2 wrote:
    Hello, are all the self-tests passing now?

    Yes, there were timezone issues as my Windows machine is UTC+11. Please test on your machine also as I aimed to fix for all time zones.

    p.kaczmarek2 wrote:
    Did you add the new autoexec sample to the JSON source file of docs?

    Updated. I also missed updating the constants.json file. Do these changes automatically get applied to the constants documentation?
  • ADVERTISEMENT
  • #49 20917764
    p.kaczmarek2
    Moderator Smart Home
    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.

    So, the PR is ready to merge?
    Helpful post? Buy me a coffee.
  • #50 20919133
    ilengyel
    Level 11  
    Yep, I believe this PR is ready to merge. Finally went through a sunset test on a BK7231T device 😎
  • #51 20949936
    omniron
    Level 10  

    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?

    The nice
    https://tasmotatimezone.com/
    would get our coordinates etc.
    Thanks in advance.
  • #52 20949949
    p.kaczmarek2
    Moderator Smart Home
    Hello, have you checked our autoexec examples page?
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    Some of the samples there may be useful for you:
    Screenshot of autoexec examples for Tuya mini smart switch.
    Helpful post? Buy me a coffee.
  • #53 20950188
    omniron
    Level 10  
    Didn't know there are samples already, will check, thank you for that!

    Would this work for

    https://www.elektroda.com/rtvforum/topic4015180.html
    on its relay 2

    // autoexec for mini smart switch
    PowerSave 1
    addEventHandler OnHold 8 SafeMode
    startDriver ntp
    ntp_timeZoneOfs -8
    ntp_setLatlong 44.002130 -123.091473
    // pins are defined for my device already
    // setPinRole 1 WifiLed
    // setPinRole 24 Btn_Tgl_All
    // setPinRole 14 TglChanOnTgl
    // setPinChannel 14 0
    // setPinRole 15 Rel
    // setPinChannel 15 0
    removeClockEvent 12
    removeClockEvent 13
    waitFor NTPState 1
    addClockEvent sunrise 0x7f 12 POWER2 OFF
    addClockEvent sunset 0x7f 13 POWER2 ON


    Thanks!
  • #54 20950213
    p.kaczmarek2
    Moderator Smart Home
    I think you also need to update this line:
    
    ntp_setLatlong 44.002130 -123.091473
    

    unless, you happen to live there:
    Screenshot from Google Maps showing a location in Eugene, Oregon with geographical coordinates 44.002130, -123.091473.
    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?
    
    ntp_timeZoneOfs -8
    
    Helpful post? Buy me a coffee.
  • #55 20950255
    omniron
    Level 10  

    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.
  • #56 20950287
    p.kaczmarek2
    Moderator Smart Home
    I think that this extra line for SafeMode is not needed. You can easily enable SafeMode just by doing 5 quick power off and on cycles.
    Helpful post? Buy me a coffee.
  • #57 20950315
    omniron
    Level 10  
    Great, so here is what I have now, including the other autoexec.bat stuff:

    // 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...

    Must say,
    this looked so much easier.
    But great to have this feature, thanks again

    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
    
  • #58 20954022
    omniron
    Level 10  

    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.

    And vice versa.
  • #59 20971275
    ilengyel
    Level 11  
    omniron wrote:
    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.
  • #60 21389444
    w2q
    Level 4  
    I tried to calculate the sunset on a BL602, but it does not work. I found a probable reason in this commit. Can anybody explain why BL602 is ignored?

Topic summary

The discussion revolves around the implementation of sunrise and sunset events in NTP (Network Time Protocol) for the BK7231N device. Rick proposes adding these features and shares a patch for feedback. Contributors express concerns about memory usage and suggest merging latitude and longitude commands to optimize space. Testing reveals the algorithm's accuracy within ±2 minutes, though issues arise with event setup in autoexec.bat due to NTP offset timing. Solutions include using "waitFor NTPState 1" for synchronization and adjusting polling intervals. The feature is successfully merged, with users sharing autoexec examples and discussing enhancements for manual overrides after power outages.
Summary generated by the language model.
ADVERTISEMENT