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 2319 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • 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.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11800 posts with rating 9921, helped 563 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20941803
    pepedombo
    Level 11  
    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.
  • #3 20942174
    p.kaczmarek2
    Moderator Smart Home
    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 you can write programs directly in C?

    Of course, you can write in OpenBeken in C, but then you have to perform compilation and the entire OTA process to update the batch, and sometimes it is more convenient to have changes made immediately, 1 second after acceptance, that`s what the command language is for.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 20947093
    pepedombo
    Level 11  
    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`. I just thought it was a different environment, not scripts.
  • #5 20947107
    p.kaczmarek2
    Moderator Smart Home
    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" and it is a framework written in C with a simple HAL for various MCUs, but on the systems that OpenBeken runs on, there is so much space in Flash and RAM that OpenBeken can have its own mini file system (based on LittleFS ) and also its own system of simple scripts and events, basically an interpreter.

    Why do you think that the interpreter is
    pepedombo wrote:
    a burden, i.e. waste

    after all, the whole thing does not slow down the target devices in any way, nor does it require, for example, increasing their Flash memory (the BK7231 has it built-in anyway, so it would not be possible), and it does not force the purchase of "more powerful MCUs" (this would be absurd, because the aim of the project is it to work on the WiFi modules that are included in products from China) and does not discriminate against any WiFi module (it works on all the ones that have been figured out so far)?

    To sum up, both from the point of view of the OBK developer and the end user, the presence of such an interpreter does not harm in any way, it only improves the attractiveness of the environment. For example, I have already seen that users created their own roller shutter controller in the OBK script without modifying the C code.


    PS: Looking at what you write about STM8, do you know that the WiFi modules supported by OBK already have an RTOS, a whole TCP/IP stack, WiFi, etc.? There is also an MQTT client (we use LWIP), the ability to send GET requests (e.g. reporting measurements via GET to the server), etc. This is a slightly different situation than the simple 8-bit computers that I have been programming myself for a long time.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #6 20947140
    pepedombo
    Level 11  
    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 I used to make one in C), I would quickly fail due to the available memory. I know I just started too extreme, but I don`t see the need to add another API level above the current one. Even for 32-bit MCUs, I don`t see the need to tweak it sideways - maybe gently, it`s mainly about TCP. Something like writing shaders in hlsl - why create an additional layer for something that is relatively simple and is supposed to be fast with limited resources? Interpeter is always a parasite - see java or better made .net.
    We do not send data to the server via GET. It is possible, but for some time now this approach has been very strained (CRUD).
  • #7 21005994
    omelchuk890
    Level 11  
    @p.kaczmarek2 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/
    I want to use the W600 module for my ESP-01 based boards and communicate with the rest of the chips using the I2C protocol.
    I really like your interface and stability!
    I would like to test my scripts in the OBK simulator, but there is no way I can get it to work... https://github.com/openshwprojects/obkSimulator
  • ADVERTISEMENT
  • #8 21007275
    p.kaczmarek2
    Moderator Smart Home
    This language is not LUA, it is the OBK scripting language.

    I will try to publish the simulator soon.
    Helpful post? Buy me a coffee.
  • #9 21378225
    SebiTNT
    Level 1  
    Thank you very much for your effort @p.kaczmarek2!

    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 0 seconds. Is there anything I can do about it?

    Thank you very much in advance!

Topic summary

The discussion revolves around scripting an automatic relay shutdown in OpenBeken (OBK) without using Home Assistant. A user presents a sample script for a WiFi-controlled LSPA9 socket utilizing the BK7231 chip, emphasizing the flexibility of scripting directly on the device without firmware modifications. Participants debate the merits of using an interpreter for scripting versus writing in C, with some expressing concerns about the efficiency and necessity of an additional API layer. The scripting language used in OBK is clarified to be distinct from Lua, and there is interest in utilizing the OBK simulator for testing scripts.
Summary generated by the language model.
ADVERTISEMENT