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 
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 11822 posts with rating 9927 , helped 564 times. Been with us since 2014 year.

Comments

Add a comment
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 - `commands`.... [Read more]

p.kaczmarek2 05 Feb 2024 22:59

I have the impression that we probably don`t understand each other, but I`m curious about your opinion, so I`ll try to explain the situation a bit. It is generally, as you wrote, a "different environment"... [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]