logo elektroda
logo elektroda
X
logo elektroda

BK7231N Clock Event Script: Configurable Time for Relay Off Pulse, $CH expansion problem

tinkertechie 774 5
ADVERTISEMENT
  • #1 21150984
    tinkertechie
    Level 2  
    I have another "reboot" scenario with OpenBK scripting, this time trying to give an off pulse of a few seconds at a configurable time.

    I'm attempting to use a clock event to turn off the relay like this:

    addClockEvent $CH5:$CH7 0xFF 567 turn_off_relay

    with the CH5 and CH7 being configurable from the GUI, but I'm guessing from its behavior that the $CH5 and $CH7 are not being expanded. If I put in a readable time like 18:23 it works just fine.

    I can of course just change the code to set a new time, but I'd like the client to be able to adjust this themselves.

    Could someone confirm whether this expansion is possible somehow with the existing codebase? I haven't yet dug into the parser and interpreter enough to see how it's done.

    Thanks!
  • ADVERTISEMENT
  • #2 21151448
    p.kaczmarek2
    Moderator Smart Home
    I think it's disabled, but I can look into it.

    Alternatively, maybe a custom simple C code can help? Have you seen:
    https://www.elektroda.com/rtvforum/topic4056286.html
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21151728
    tinkertechie
    Level 2  
    >>21151448I have seen it, I actually have it open in another tab, but I haven't had time to digest it. Expansion seems helpful anyway ;) but I'll have a look at the custom driver option too.
  • ADVERTISEMENT
  • #4 21152576
    tinkertechie
    Level 2  
    >>21151448

    Here's the script I've been attempting to use. I haven't gone over this fully for presentation, and I believe it's fine as-is, but just in case you want to put it in the examples, here it is :)

    // A pulse-off script designed to reset a device once a day.
    // Based on the "advanced delay script" example script
    // See: https://www.elektroda.com/rtvforum/topic4032982.html
    
    // When the relay is turned off off, relay will turn back on after delay
    // The current delay value is displayed on www gui
    // The off time delay value, and the hour and minute at which it should occur,
    // is displayed on www gui and fully adjustable and remembered between reboots 
    // NTP is used for this and would presumably need to synchronise at least once in order 
    // to operate at the correct time. Timezone needs to be ajusted in the code.
    
    
    // Used channels:
    // Channel 1 - relay and button
    // Channel 2 - current countdown value
    // Channel 3 - preset time for countdown
    // Channel 4 - new hour for auto off
    // Channel 5 - actual hour for auto off
    // Channel 6 - new minutes for auto off
    // Channel 7 - actual minutes for auto off
    
    // this will make channel 3,4 and 6 save in memory
    setStartValue 3 -1
    setStartValue 4 -1
    setStartValue 5 0
    setStartValue 6 -1
    setStartValue 7 0
    
    // if channel 3 is not set (default first case), set it to 15 seconds
    if $CH3==0 then setChannel 3 15
    
    // other sanity checks for times are done in the main loop
    
    // display fields for channels 3-7 on www page
    setChannelType 3 TextField
    setChannelType 4 TextField
    setChannelType 5 TimerSeconds
    setChannelType 6 TextField
    setChannelType 7 TimerSeconds
    
    // set label
    setChannelLabel 3 "Off Duration"
    setChannelLabel 4 "Auto Off Hour"
    setChannelLabel 6 "Auto Off Minute"
    
    // set starting values for auto reset time
    setChannel 5 $CH4
    setChannel 7 $CH6
    
    // display timer seconds (read only) for channel 2 on www page
    setChannelType 2 TimerSeconds
    
    // All the variables are set up. Now turn on the outlet
    SetChannel 1 1
    
    // Get rid of old handlers
    clearAllHandlers
    
    // shorthand for setting channel 2 to value of channel 3
    alias start_timer backlog setChannel 2 $CH3
    
    // shorthand for clearing current timer value
    alias stop_timer setChannel 2 0
    
    // shorthand to set relay on or off
    alias turn_on_relay setChannel 1 1
    alias turn_off_relay setChannel 1 0
    
    // check used to turn on relay when countdown value reaches 0
    alias do_check if $CH2==0 then turn_on_relay 
    
    // an update command; it decreases current countdown by 1 and checks for 0
    alias do_tick backlog addChannel 2 -1; do_check
    
    // this doesn't currently work because the expansion of $CH variables doesn't seem to be done
    // for addClockEvent
    
    // alias new_time backlog setChannel 5 $CH4; setChannel 7 $CH6; addClockEvent $CH5:$CH7 0xFF 567 turn_off_relay
    
    // currently using a hard coded time
    alias new_time backlog setChannel 5 $CH4; setChannel 7 $CH6; addClockEvent 04:04 0xFF 567 turn_off_relay
    
    // event triggers for channel 1 changing to 0 and 1
    addChangeHandler Channel1 == 1 stop_timer
    addChangeHandler Channel1 == 0 start_timer
    
    // Don't make any progress until WiFi connects - we need that for NTP
    // wait for wifi to become WIFI_STA_CONNECTED
    // waitFor WiFiState 4
    
    // Start the NTP driver and set the time zone to Melbourne. Not adjusting for DST at this point.
    startDriver NTP
    ntp_timeZoneOfs 10
    
    turn_on_relay
    
    new_time
    
    // main loop - infinite
    again:
    if $CH2!=0 then do_tick
    
    // Sanity check in case anything has been set to a silly value
    
    if $CH3<=0 then "setChannel 3 1"
    if $CH3>120 then "setChannel 3 120"
    
    if $CH4<0 then "setChannel 4 0"
    if $CH4>23 then "setChannel 4 23"
    
    if $CH6<0 then "setChannel 6 0"
    if $CH6>59 then "setChannel 6 59"
    
    // if the reset time changes, update the handler
    if $CH4!=$CH5 then new_time
    if $CH6!=$CH7 then new_time
    
    delay_s 1
    goto again
  • ADVERTISEMENT
  • #5 21153148
    p.kaczmarek2
    Moderator Smart Home
    Thank you, but here is little update from my side.
    I checked the automatic self-test code and apparently addClockEvent already expands vars at the start:
    C code in the Visual Studio editor with functions related to addClockEvent.
    So your issue must be somewhere else...
    Helpful post? Buy me a coffee.
  • #6 21153565
    tinkertechie
    Level 2  
    Thanks for checking! I hadn’t thought to look at the test code 🤦‍♂️

    I’ll double-check the firmware version. Maybe I’m out of date. The calls to addClockEvent didn’t seem to be showing up in the log, even at Debug level, but the version with the explicit time did.

    If it turns out that there are code changes I’ll repost the script, but I hope it’s just firmware. Either way, I’ll let you know what I find.

    Thanks again, as always!
ADVERTISEMENT