logo elektroda
logo elektroda
X
logo elektroda

How to script automatic switching off of the relay after a certain time in OBK without Home Assistan

p.kaczmarek2  8 2976 Cool? (+6)
📢 Listen (AI):

TL;DR

  • OpenBeken scripting on a BK7231-based LSPA9 WiFi socket automates relay shutoff after a user-set delay, without Home Assistant or cloud control.
  • The script uses Channel 1 for the relay, Channel 2 for the live countdown, and Channel 3 as the saved shutdown time shown on the web panel.
  • Channel 3 is initialized to 15 seconds with setStartValue 3 -1 and displayed as a TextField labeled TurnOffTime.
  • When Channel 1 turns on, addChangeHandler starts a countdown loop that decreases Channel 2 every second and turns the relay off at zero.
  • A minimal addRepeatingEvent version can shrink the logic to one line, but it loses web-panel time preview and MQTT exposure.
Generated by the language model.
Screenshot of OpenBeken script interface with Toggle 1 button and timer.
I will present a small sample script here OpenBeken turning off the device after a given period of time after turning it on. The waiting time will also be user-configurable, and the entire mechanism will be scripted manually, which gives the user ample modification options without any changes to the firmware.

Device used
For the presentation, I used a WiFi-controlled LSPA9 socket based on the BK7231 chip:
White LSPA9 WiFi socket with a power button.
Topic about the socket:
https://www.elektroda.com/rtvforum/topic3887748.html


Scripting basics
We create scripts in the file system hosted on the device. Here is a short tutorial:



The file executed at startup is autoexec.bat , you can put our program in it.
Documentation can be found here:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md
Command documentation has a separate subpage:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
The documentation also includes file examples autoexec.bat : :
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md


We create our own script
First, let`s think about what we want our script to offer:
- the delay time until shutdown should be shown in the text box
- the delay time for shutdown should be remembered after the device is rebooted
- the current value of the waiting time (when the countdown is in progress) should be shown as a timer, separately from the "set" value
- when the user turns on the device, the developed timer automatically turns on and counts down the time
A small note here - if someone insisted, it could be done in OBK with one command, via addRepeatingEvent , but we want to break it down into commands as much as possible so that we can then edit them freely.

Once we have the concept of the script, we need to try to break it down into its first parts, i.e.:
- define the variables (channels in OBK) that we will use - here we will need the relay state, the current value of the time to turn off and the constant initial value to turn off
- define the types of these variables (channel types in OBK) to be able to display them on the web panel, here type list
- define the time countdown method, here, as this is an example, I decided to simply decrease the channel value in a simple loop
- define how to detect a change in the relay state, here I used the change event AddChangeHandler

Based on this, you can develop a ready-made script. Here is my sample autoexec.bat;


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

After restarting the device, we get a modified panel:
WiFi socket control interface with BK7231 chip, showing options for configuring shut-off time.
You can see the relay switch button, the current countdown value and the countdown time setting on the panel.
When the relay is turned on, the countdown starts:
Screenshot of control panel interface for a relay with countdown timer.
The value of channel 2 decreases:
OpenBeken control panel with countdown timer for WiFi relay
After a while it reaches 0 and the relay opens:
Control panel interface of LSPA9 socket with shutdown timer settings.
The script is resistant to repeated power on and off, there is no way to break this logic.

For minimalists
Of course, the entire example here was created to also show the possibilities of scripting. If someone doesn`t want to write too much code, this script will also work:

// This aliased command will turn off relay on CH1 after 10 seconds
// addRepeatingEvent	[IntervalSeconds][RepeatsOr-1][CommandToRun]
alias turn_off_after_time addRepeatingEvent 10 1 setChannel 1 0
// this will run the turn off command every time that CH1 becomes 1
addChangeHandler Channel1 == 1 turn_off_after_time 

You can even compress it:

addChangeHandler Channel1 == 1 addRepeatingEvent 10 1 setChannel 1 0

However, it does not offer the ability to preview and set times on the web panel and does not expose them to MQTT.

Summary
A device scripted in this way can perform various types of automation completely without the participation of the cloud and even without the participation of Home Assistant. What`s more, you can even perform automation on multiple devices via command SendGet , but more about that another time. If in doubt, please refer to our documentation:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
Of course, if you have any questions, I will try to help.
PS: The topic was created in response to increasingly common questions about this type of scripts, including: here and here . The topic will also be translated into English.

About Author
p.kaczmarek2
p.kaczmarek2 wrote 14602 posts with rating 12617 , helped 654 times. Been with us since 2014 year.

Comments

pepedombo 02 Feb 2024 21:44

I thought that openbeken was some normal platform on a normal Mac, but now I`m seriously wrong. :) The script looks like there is an interpreter behind it and a real waste, i.e. waste. [Read more]

p.kaczmarek2 03 Feb 2024 08:44

I don`t think I understand this comment, could you explain what you are trying to say? Do you mean that, in your opinion, the presence of an interpreter of simple commands on the MCU is a waste, because... [Read more]

pepedombo 05 Feb 2024 22:50

That is, an interpreter, that is, a burden, that is, a waste. In stm8, with a minimum amount of memory, it was not even possible to attach floats and create a simple display, and here I am asking - .... [Read more]

p.kaczmarek2 05 Feb 2024 22:59

I have the impression that we probably don m curious about your opinion, so I`ll try to explain the situation a bit. It is generally, as you wrote, a "different environment" and it is a framework written... [Read more]

pepedombo 05 Feb 2024 23:28

I play stm8 for fun and actually the first thing that turned me off was the first discovery with a staggeringly limited amount of memory. (stm8s003f3). If I were to make an interpreter using this (because... [Read more]

omelchuk890 16 Mar 2024 06:51

@pkaczmarek2 Tell me, is it Lua? Does this mean that I have not studied it in vain? Can this documentation be guided by this documentation when writing a script? https://nodemcu.readthedocs.io/en/dev/ ... [Read more]

p.kaczmarek2 17 Mar 2024 08:56

This language is not LUA, it is the OBK scripting language. I will try to publish the simulator soon. [Read more]

SebiTNT 05 Jan 2025 13:51

Thank you very much for your effort @pkaczmarek2! I am running the above example script on a LN882H. Unfortunately the Controller is restarting itself with "Reboot reason: 2 - Wdt" when the timer reaches... [Read more]

FAQ

TL;DR: Use autoexec.bat in OpenBeken to start a local relay auto-off script: the sample defaults to 15 seconds, and the maintainer states, "This language is not LUA." It helps users who want automatic relay shutdown, editable delay, and web-panel visibility without Home Assistant; a simpler fixed-delay version uses 10 seconds. [#20940575]

Why it matters: This thread shows how to build device-side automation on OBK, so timing logic keeps working without cloud services or Home Assistant.

Option Delay setup Web panel visibility MQTT exposure Edit speed
Full autoexec.bat script User-set, remembered Yes Yes Immediate on device
Minimal addRepeatingEvent script Fixed, e.g. 10 s No No Immediate on device
Custom C firmware Whatever you code Not stated Not stated Slower; needs compile + OTA

Key insight: In OBK, the best trade-off for quick relay automation is usually a startup script, not a firmware rebuild. You get local control, editable timers, and event-driven behavior with simple commands.

Quick Facts

  • The full example stores the editable off-delay in Channel 3, initializes it to 15 seconds when empty, and saves it across reboot with setStartValue 3 -1. [#20940575]
  • The live countdown uses Channel 2 with TimerSeconds, while the relay itself stays on Channel 1. That split lets the UI show both current time left and the configured delay. [#20940575]
  • The main loop ticks every 1 second using delay_s 1, then subtracts 1 from Channel 2 until it reaches 0 seconds. [#20940575]
  • The minimalist version can shut off the relay after 10 seconds with one event line, but it does not expose the timer in the web panel or MQTT. [#20940575]
  • One LN882H user reported a reboot at countdown end with "Reboot reason: 2 - Wdt", so module-specific testing matters before deployment. [#21378225]

How do I script an automatic relay turn-off after a set time in OpenBeken without using Home Assistant?

Create an autoexec.bat script that starts at boot, watches Channel1, copies the configured delay into Channel2, and counts down locally until it reaches 0. The sample uses addChangeHandler Channel1 == 1 turn_off_after_time, then loops with delay_s 1 and addChannel 2 -1. When Channel2==0, it runs setChannel 1 0. This gives fully local automation, with no cloud and no Home Assistant required. [#20940575]

What is the OBK scripting language, and how is it different from Lua in OpenBeken?

The OBK scripting language is OpenBeken’s own command language, not Lua. The maintainer answered this directly: “This language is not LUA, it is the OBK scripting language.” It is designed for quick device-side changes through commands, events, aliases, and startup scripts, so users can update behavior immediately instead of recompiling firmware. [#21007275]

How does autoexec.bat work in OpenBeken, and where should I place a startup automation script?

autoexec.bat is the startup script file that OpenBeken executes automatically when the device boots. Place your automation commands in the device file system hosted on the module itself. In the sample, the whole relay timer logic lives in autoexec.bat, so it starts again after every reboot without external software. [#20940575]

What do addChangeHandler and addRepeatingEvent do in OBK, and when should I use one instead of the other?

addChangeHandler reacts when a channel changes state, while addRepeatingEvent schedules a command to run after an interval. Use addChangeHandler when you want logic tied to relay state, such as starting a countdown when Channel1 becomes 1. Use addRepeatingEvent for a short fixed-delay solution, such as turning CH1 off once after 10 seconds. [#20940575]

How can I make the relay off-delay value user-configurable and visible on the OpenBeken web panel?

Store the editable delay in a separate channel and expose that channel as a text field in the web UI. In the sample, Channel3 holds the saved delay, setChannelType 3 TextField makes it editable, and setChannelLabel 3 TurnOffTime names it on the panel. When the relay turns on, an alias copies Channel3 into the live countdown channel. [#20940575]

Which OBK channel types should I use to show a countdown timer and an editable turn-off time in the web UI?

Use TimerSeconds for the live countdown and TextField for the editable off-delay value. The sample sets Channel2 to TimerSeconds so users can see the remaining time, and sets Channel3 to TextField so they can enter a new delay directly in the web panel. That combination separates display-only time from configuration. [#20940575]

Why does the example OpenBeken script use Channel 1, Channel 2, and Channel 3, and what does each one store?

It uses three channels to keep switching, countdown, and configuration separate. Channel1 stores the relay state, Channel2 stores the current countdown value, and Channel3 stores the constant turn-off time. This layout lets OBK show the current timer and the saved default independently, which makes the panel clearer and MQTT-ready. [#20940575]

What is LittleFS in OpenBeken, and how is it used for storing scripts on the device?

"LittleFS" is a file system that stores files on embedded devices, providing lightweight on-device persistence for scripts and settings. In this thread, the maintainer explains that OpenBeken has a mini file system based on LittleFS, and that design allows startup scripts and simple automation files to live directly on supported Wi-Fi modules. [#20947107]

OpenBeken scripting vs writing custom C firmware — which approach is better for quick automation changes on BK7231 devices?

OBK scripting is better for quick automation changes. The maintainer explains that C is possible, but it requires compilation and the full OTA update process, while command-language changes apply almost immediately, even “1 second after acceptance.” Choose scripting when you want to test timer logic, events, or UI changes fast on BK7231-class devices. [#20942174]

How can I make an OpenBeken countdown timer survive a reboot and remember the previously set delay value?

Save the configured delay in a persistent channel, not in the live countdown channel. In the sample, setStartValue 3 -1 makes Channel3 survive reboot, and if $CH3==0 then setChannel 3 15 gives a first-run default of 15 seconds. After boot, the script copies that saved value into Channel2 whenever the relay turns on. [#20940575]

Why does an LN882H running the example OBK turn-off timer reboot with "Reboot reason: 2 - Wdt" when the timer reaches 0 seconds?

The thread confirms the symptom on LN882H, but it does not provide a confirmed fix. A user reported that the controller restarts with Reboot reason: 2 - Wdt exactly when the timer hits 0 seconds. Treat this as a module-specific failure case and test the script on your target hardware before relying on it. [#21378225]

What are the limitations of the minimal addRepeatingEvent one-liner compared with the full OpenBeken countdown script?

The one-liner is shorter, but it loses visibility and configurability. addChangeHandler Channel1 == 1 addRepeatingEvent 10 1 setChannel 1 0 gives a fixed 10-second off-delay, yet it does not show the timer on the web panel, does not let users edit the delay there, and does not expose those values to MQTT. [#20940575]

How can I expose the automatic turn-off timer to MQTT in OpenBeken instead of only using a hardcoded delay?

Use the full channel-based script instead of the fixed addRepeatingEvent one-liner. The author states that the minimal version does not expose times to MQTT, while the fuller design keeps the delay in Channel3 and the live countdown in Channel2. That structure gives MQTT-visible state because the timer exists as channels, not just as a hardcoded 10-second event. [#20940575]

What should I check when the OBK simulator does not start or my OpenBeken scripts cannot be tested in it?

Check first whether you are using the OBK simulator repo the thread mentions, then verify that the simulator is actually available in a usable state. The user linked the simulator repository and said they “can get it to work,” and the maintainer replied, “I will try to publish the simulator soon.” That means the problem may be tool readiness, not your script. [#21007275]

How could I adapt this OpenBeken auto-off relay script for other modules mentioned in the thread, such as W600 or LN882H, especially when using I2C-based boards?

Keep the script logic the same, but validate module support and edge cases on each target board. 1. Start with the same channel structure: relay on Channel1, countdown on Channel2, saved delay on Channel3. 2. Test boot, countdown, and 0-second shutdown on the specific module. 3. If you use LN882H or W600-class boards, verify simulator behavior and watchdog stability before adding I2C logic. [#21378225]
Generated by the language model.
%}