logo elektroda
logo elektroda
X
logo elektroda

Implementing a Variable Relay Loop in OpenBeken Configuration

angzero 2454 18
Best answers

How can I make an OpenBeken script turn a relay on for 1 minute, off for 30 minutes, and loop without crashing?

Use a simple loop with separate commands, and avoid nested quotes inside `alias` definitions because they are not supported and can trigger crashes [#21102923] Define the cleanup/action in its own alias, for example `alias do_turn backlog turn_off_relay; echo "we are done"`, then call that alias from `do_check` instead of trying to embed quoted `backlog` inside another quoted string [#21102923] For the relay timing loop, the suggested working version is `again: setChannel 1 1; delay_s 60; setChannel 1 0; delay_s 1800; goto again` [#21102923] If `setChannel` still appears to crash inconsistently on your device, the thread suggests it may be device-specific or related to state left over from earlier tests, so do a full reboot between script changes and test again [#21103831]
Generated by the language model.
ADVERTISEMENT
  • #1 21102869
    angzero
    Level 4  
    Posts: 13

    Relay toggle
    Good morning everyone,

    I have an issue writing a script that I can't figure out on my own, I have a feeling that the answer is stupid but I can't figure out what is going wrong.

    I have a BK7231N running the latest 1.17.601 OpenBeken. the device is a simple relay switch with a button.

    I am trying to create a script that runs on startup, turns on the relay for 1 minute and then turns it off for 30 minutes, and then loops again, and I tried running the scripts from github and they run perfectly for running and turning off the script using event handlers, but whenever I try to modify those scripts to get it to what I need it to, the device crashes and restarts.

    Here's the original script that runs fine unmodified:

    // advanced delay script
    // See: https://www.elektroda.com/rtvforum/topic4032982.html
    // When user toggles relay on, relay will turn off after delay
    // The current delay value is displayed on www gui
    // The constant delay value is displayed on www gui and fully adjustable and remembered between reboots 
    // Used channels:
    // Channel 1 - relay
    // Channel 2 - current countdown value
    // Channel 3 - constant time for countdown
    // this will make channel 3 save in memory
    setStartValue 3 -1
    // if channel 3 is not set (default first case), set it to 15 seconds11
    if $CH3==0 then setChannel 3 15
    // display text field for channel 3 on www page
    setChannelType 3 TextField
    // set label
    setChannelLabel 3 TurnOffTime
    // display timer seconds (read only) for channel 2 on www page
    setChannelType 2 TimerSeconds
    // shorthand for setting channel 2 to value of channel 3
    alias turn_off_after_time backlog setChannel 2 $CH3
    // shorthand for clearing current timer value
    alias on_turned_off setChannel 2 0
    // shorthand to set relay off
    alias turn_off_relay setChannel 1 0
    // check used to turn off relay when countdown value reaches 0
    alias do_check if $CH2==0 then turn_off_relay 
    // an update command; it decreases current countdown by 1 and checks for 0
    alias do_tick backlog addChannel 2 -1; do_check
    // event triggers for channel 1 changing to 0 and 1
    addChangeHandler Channel1 == 1 turn_off_after_time 
    addChangeHandler Channel1 == 0 on_turned_off
    // main loop - infinite
    again:
    if $CH2!=0 then do_tick
    delay_s 1
    goto again


    I can't figure out what is going wrong or debug it properly, for example, the script crashes when I run something like:
    // check used to turn off relay when countdown value reaches 0
    alias do_check if $CH2==0 then "backlog turn_off_relay; echo "we are done""


    I appreciate the help of anyone who can test this and see if it's a common theme or if it's something wrong I'm doing
  • ADVERTISEMENT
  • #2 21102923
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    It looks like you are trying to use nested quotes. I don't think it's supported, not just in OBK, but anywhere else....

    I will check this crash in Simulator, but I'd suggest you changing:
    
    alias do_check if $CH2==0 then "backlog turn_off_relay; echo "we are done""
    

    to
    
    alias do_turn backlog turn_off_relay; echo "we are done"
    alias do_check if $CH2==0 then do_turn 
    


    Added after 3 [minutes]:

    Btw : a script that runs on startup, turns on the relay for 1 minute and then turns it off for 30 minutes, and then loops again

    
    again:
    setChannel 1 1
    delay_s 60
    setChannel 1 0
    delay_s 1800
    goto again
    
    Helpful post? Buy me a coffee.
  • #3 21103328
    angzero
    Level 4  
    Posts: 13

    >>21102923

    Thanks a lot for the quick response and I appreciate your help on this, honestly when you posted that simple loop script, I couldn't help but laugh on how did I miss that and why was I complicating things, but unfortunately, it didn't work and the device still crashes (restart) right after "setChannel 1 0" (I think)

    What is interesting though, is that I replaced the setChannel commands with "POWER TOGGLE", and the script ran without crashing, so now I'm confused as to what is happening and why is it crashing

    Were you able to replicate this behavior?, should I wipe the device and test again?, I don't have another one to test so I would need to wipe to see if it's just a specific behavior to my device
  • ADVERTISEMENT
  • #4 21103342
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    This script seems to work for me:
    LittleFS filesystem user interface with editing of autoexec.bat file. Screenshot of the OBK Delay Script Sample interface showing voltage, current, and other energy parameters with buttons for configuration.
    Code: JSON
    Log in, to see the code

    what is your device exactly, what kind of template do you have?
    Helpful post? Buy me a coffee.
  • #5 21103498
    angzero
    Level 4  
    Posts: 13
    >>21103342>>21103342

    The only difference is that i'm not running it as autoexec.bat only to avoid it constantly running at startup and crashing, instead i named mine "simple.bat" which shouldn't make a difference "i think"

    Here are the details of my device:
    Code: JSON
    Log in, to see the code


    and here are some images of the device itself:
    Packaging of a WiFi smart outlet with remote control and app functionality. Close-up of an electronic module labeled CB2S, with a visible QR code and serial numbers.
  • #6 21103545
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Still working, sorry:
    Screenshot of LittleFS file management user interface.

    So you're saying that:
    
    POWER ON
    

    etc, does not crash?
    Helpful post? Buy me a coffee.
  • #7 21103560
    angzero
    Level 4  
    Posts: 13

    >>21103545
    Oh no i believe you, i'm not sure what's happening and i think i might need to reset the device fully and test again, but yes, the POWER TOGGLE option does not cause a crash for me:

    Screenshot of a text editor with an open script.bat file.
  • #8 21103562
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Yea but what about POWER ON or POWER OFF?
    Helpful post? Buy me a coffee.
  • #9 21103594
    angzero
    Level 4  
    Posts: 13
    >>21103562
    using POWER ON and POWER OFF works too it seems
  • #10 21103602
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Which OBK build?
    Helpful post? Buy me a coffee.
  • #11 21103605
    angzero
    Level 4  
    Posts: 13

    >>21103602
    I tested a couple on the past couple of days all with the same result, right now I'm on 1.17.601
  • ADVERTISEMENT
  • #12 21103608
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Ok what happens if you open this page:

    BK7231T_WB3S_WhiteStripWindows tool interface on a webpage.
    And enter your setChannel line there?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #13 21103646
    angzero
    Level 4  
    Posts: 13

    p.kaczmarek2 wrote:
    Ok what happens if you open this page:

    BK7231T_WB3S_WhiteStripWindows tool interface on a webpage.
    And enter your setChannel line there?


    No issue there, i can execute commands here and in the Log section in the webapp, its honestly inconsistent with its breaking so i'm not sure what is the trigger here and whether it is the setChannel command, as that works with no issue using the scripts posted on the OpenBeken github, then suddenly on a simple loop script like the one provided earlier, it crashes, but not when using the POWER TOGGLE or POWER ONOFF variants, and i'm not sure if anyone else has faced this or just me
  • #14 21103673
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Do you have MQTT connected?
    Helpful post? Buy me a coffee.
  • #15 21103733
    angzero
    Level 4  
    Posts: 13
    >>21103673

    yes paired with home assistant
  • #16 21103740
    DeDaMrAz
    Level 22  
    Posts: 594
    Help: 34
    Rate: 122
    @angzero

    Do you mind sharing your full script with us so we can try to replicate that?

    I have several devices running with both seChannel and POWER ON/OFF scripted for days now without any issues.
  • #17 21103780
    angzero
    Level 4  
    Posts: 13

    DeDaMrAz wrote:
    @angzero

    Do you mind sharing your full script with us so we can try to replicate that?

    I have several devices running with both setChannel and POWER ON/OFF scripted for days now without any issues.


    At this point, I'm convinced that there is something wrong with my device honestly, because earlier when I tried the following script:
    
    again:
    setChannel 1 1
    delay_s 60
    setChannel 1 0
    delay_s 1800
    goto again


    The device would crash until I replaced the setChannel with POWER TOGGLE or ONOFF, but now it's working with no issues

    How do you guys debug and test scripts efficiently to determine what crashes the system, is there a specific log level or settings that you use for script development and analysis ?
  • #18 21103831
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14394
    Help: 650
    Rate: 12314
    Are you sure that you did a full reboot between scripts?

    I am finding most of the issues in Windows build (Simulator):
    OpenBeken IoT device simulator - first early alpha version release for testing
    but it's a bit different than running on MCU, obviously, so then I am also using log levels and some manual testing to narrow down what's wrong.

    Your situation seems very strange and not easily replicable. Maybe the stack size should be increased, but I still can't see how you're filling your stack more than us.
    Helpful post? Buy me a coffee.
  • #19 21105148
    DeDaMrAz
    Level 22  
    Posts: 594
    Help: 34
    Rate: 122
    angzero wrote:
    How do you guys debug and test scripts efficiently to determine what crashes the system, is there a specific log level or settings that you use for script development and analysis ?


    On my end I would use a test device on a board that has logport exposed and log it via PuTTY with extra debug options selected from a web app then analyze the log for anomalies.

    Here is a question for you when your device crashed there is a feature in OBK on the index page that is giving a reboot reason, did you manage to maybe catch that info??

    Screenshot showing the device's reboot reason.

Topic summary

✨ The discussion revolves around a user attempting to implement a variable relay loop script on a BK7231N device running OpenBeken version 1.17.601. The user aims to create a script that activates a relay for 1 minute and then deactivates it for 30 minutes in a continuous loop. Initial attempts using the "setChannel" command resulted in device crashes, while using "POWER TOGGLE" and "POWER ON/OFF" commands did not cause issues. Various suggestions were made, including checking for nested quotes in the script, ensuring a full reboot between script changes, and analyzing logs for anomalies. The user expressed uncertainty about whether the problem was specific to their device, as others reported successful execution of similar scripts. Debugging strategies were discussed, including using log ports and monitoring reboot reasons.
Generated by the language model.

FAQ

TL;DR: For OpenBeken users, a 31‑minute relay loop yields 96.8% off‑time; avoid nested quotes—"I don't think it's supported". Implement with setChannel or POWER and delay_s, and test via the console. [Elektroda, p.kaczmarek2, post #21102923]

Why it matters: This helps anyone asking how to fix relay scripts that crash or loop unreliably at startup.

Quick Facts

Whats the simplest OpenBeken script to loop a relay 1 minute on, 30 minutes off?

Use a tight loop with delays. Duty cycle is about 1/31 ≈ 3.23%.
  1. setChannel 1 1; delay_s 60.
  2. setChannel 1 0; delay_s 1800.
  3. goto again to repeat. This minimal pattern avoids timers, handlers, or extra channels. It is easy to test and revert. Start it manually before making it autoexec.bat. [Elektroda, p.kaczmarek2, post #21102923]

Why does my OBK script crash when I use nested quotes inside alias/if?

Nested quotes in command strings are not supported. Split complex actions into separate aliases and call them from the condition instead. Example: create alias do_turn with backlog turn_off_relay; echo we are done, then call do_turn inside your if. As one expert noted, "I don't think it's supported." This prevents parser issues and stack pressure. [Elektroda, p.kaczmarek2, post #21102923]

setChannel crashes but POWER works. How do I diagnose it?

First, confirm the behavior outside the loop and note the exact command that precedes the restart. Compare setChannel 1 0 with POWER OFF in isolation. If POWER works but setChannel crashes, suspect template mapping, state handlers, or memory. Reduce the script to the smallest reproducer, then add one command at a time. Record the reboot reason from the UI after each test. [Elektroda, angzero, post #21103328]

Does naming the file autoexec.bat vs simple.bat change behavior?

The command behavior is the same, but autoexec.bat runs automatically at boot. Running the same code from a manual .bat lets you test safely without repeated restarts. Keep experiments in a separate file, verify stability, then move to autoexec.bat when ready. [Elektroda, angzero, post #21103498]

How do I test a single command safely without running a whole script?

Use the web Console page that lets you execute a single command. Enter setChannel 1 0 or POWER ON/OFF there and observe logs. This isolates script flow from command behavior. If a command works here but fails inside a loop, the issue is sequencing or state. [Elektroda, p.kaczmarek2, post #21103608]

How do I debug OpenBeken scripts and reproduce crashes before flashing to hardware?

Use the Windows Simulator to iterate quickly, then confirm on real hardware. Increase log levels, and narrow failures by manual command tests. As the maintainer notes, "it's a bit different than running on MCU, obviously," so always validate on the device afterward. [Elektroda, p.kaczmarek2, post #21103831]

Where can I see the last reboot reason in OpenBeken?

Open the index page in the web UI and check the Reboot Reason field. Capture this immediately after a reset to aid diagnosis. For deeper traces, log via the serial port with a terminal tool and enable extra debug options. [Elektroda, DeDaMrAz, post #21105148]

Could MQTT or Home Assistant affect my scripts stability?

Yes, include MQTT/HA in your checks. Temporarily disconnect MQTT, then rerun your minimal loop. If crashes stop, review topics, retained messages, and state handlers that react to channel changes. Reconnect stepwise to locate the trigger. [Elektroda, p.kaczmarek2, post #21103673]

Which should I use: setChannel or POWER ON/OFF/TOGGLE for the relay?

Both approaches work in this scenario. If your template maps the relay to channel 1, setChannel 1 X and POWER commands target the same hardware. Choose one style and keep it consistent to simplify debugging. The reporter confirmed POWER ON/OFF worked as expected. [Elektroda, angzero, post #21103594]

What template mappings matter for this loop?

Map your relay pin as Rel;1. Map the user button as Btn;1, and Wi-Fi LED as WifiLED or WifiLED_n as needed. With Rel;1 on the correct pin, setChannel 1 X or POWER commands will actuate the relay. Example mapping: Btn;1 on pin 6, WifiLED_n on 7, Rel;1 on 8. [Elektroda, angzero, post #21103498]

How can I avoid boot-looping when experimenting with startup scripts?

Do not place experimental loops in autoexec.bat. Run them manually from a named .bat first. Add an initial delay_s 2 at the top to allow web access if something goes wrong. Promote to autoexec.bat only after stable tests. [Elektroda, angzero, post #21103498]

Can I adapt the adjustable countdown example to a fixed on/off loop?

Yes. The advanced example uses channels to display and persist a countdown. You can repurpose it by setting your constant to 60 seconds and triggering off logic when the timer reaches zero. For maximum simplicity, condense to the three-line loop shown earlier. [Elektroda, angzero, post #21102869]

Which OpenBeken build was used in the threads tests?

The reporter tested multiple builds and specifically noted version 1.17.601, observing the same behavior until later runs stabilized. Always try the latest available build before deep debugging. [Elektroda, angzero, post #21103605]
Generated by the language model.
ADVERTISEMENT