
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:

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:

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:

The value of channel 2 decreases:

After a while it reaches 0 and the relay opens:

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.
Cool? Ranking DIY Helpful post? Buy me a coffee.