logo elektroda
logo elektroda
X
logo elektroda

[OpenBK Scripting] Autoexec.bat Execution, Looping Scripts, State Machines & Best Practices

jkwim 2727 7
ADVERTISEMENT
  • #1 20604716
    jkwim
    Level 12  
    I understand how the autoexec.bat is picked up at boot time and excuted.

    I would like to understand how a looping script can be run afterwards.

    Do we create a second .bat file and tie that to a state machine of some sort or do we continue on the autoexec.bat by introducing a loop?
  • ADVERTISEMENT
  • #2 20608557
    p.kaczmarek2
    Moderator Smart Home
    Sorry for the late reply.
    There are multiple ways to do a looping automation in OpenBeken. The most simple way is to use a repeated event, which is defined by an interval (in seconds) and repeats count (as integer):
    
    addRepeatingEvent 1 -1 led_basecolor_rgb rand
    

    Repeats count "-1" means "forever".

    You can also use a goto statement (most likely along with if statement) to make some kind of simple loop. It can be done anywhere, even in autoexec.bat, for example:
    
    // start some drivers
    startDriver NTP
    startDriver SSDP
    // wait for MQTT to connect
    waitFor MQTTState 1
    // wait 5 seconds
    delay_s 5
    // create a label
    again:
    delay_s 5
    POWER TOGGLE
    goto again
    

    This will first start drivers, then wait for MQTT to connect, and then, in a loop, it will toggle power.

    Autoexec.bat is started at boot time, but the delay_s commands inside are respected, so you can use delays in autoexec.bat

    Please see out autoexec.bat examples for more information:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    You can also see command examples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commandExamples.md
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 20608567
    jkwim
    Level 12  
    Thanks for the clear explanation. I got the idea.

    BTW, as for my second question:
    Can a different file contain the loop which will run continously?
    Can it be called from the autoexec.bat?

    I would prefer the have a common autoexec.bat for multiple devices but different actions running in a loop for each device. So in order to maintain some order, would like to to separate them.
  • ADVERTISEMENT
  • #4 20608599
    p.kaczmarek2
    Moderator Smart Home
    You can create another file with loop and run it with startScript command. See our command docs at:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #5 20735295
    jkwim
    Level 12  

    I am posting a few more scripting-related questions in the same thread so that new users might also find this useful.

    Question:
    Is there a way to SUBSCRIBE to an external TOPIC and pick the PAYLOAD of the TOPIC into a variable (Channel)?

    Say I would like to SUBSCRIBE to the POWER status of some other device or the Power Level of some other device.

    Added after 5 [minutes]:

    Question:
    What is the syntax of "if ... then ... else" if I want to run multiple commands for each condition?

    I saw this code snippet:

    if $hour<8 then Dimmer 50
    if $hour>=20 then Dimmer 50
    if $hour>=23 then Dimmer 90

    How do you extend this to multiple commands?

    Also, are nested "if ... then ... else" supported?
  • #6 20735315
    p.kaczmarek2
    Moderator Smart Home
    jkwim wrote:

    I there a way to SUBSCRIBE to an external TOPIC and pick the PAYLOAD of the TOPIC in to a variable (Channel)?

    Say I would like to SUBSCRIBE to a POWER status of some other device or Power Level of some other device.

    I am not sure if our MQTT library supports it, I would have to check.

    Alternatively you can setup an AddChangeHandler or AddEventHandler OnChange for the second device and use SendGet there to send POWER 0 or 1. Or use Tasmota Device Groups.



    jkwim wrote:

    if $hour<8 then Dimmer 50
    if $hour>=20 then Dimmer 50
    if $hour>=23 then Dimmer 90

    How do you extend this to multiple commands?




    There are at least two ways to do this, first:
    
    if $hour<8 then backlog Dimmer 50; Power 1; Etc
    

    Second:
    
    // somewhere at the start of the script
    alias MyEarlySetting backlog Dimmer 50; Power 1; etc
    
    if $hour<8 then MyEarlySetting
    

    You can also use script threads for that.
    Helpful post? Buy me a coffee.
  • #7 21028113
    jkwim
    Level 12  
    p.kaczmarek2 wrote:
    jkwim wrote:
    I there a way to SUBSCRIBE to an external TOPIC and pick the PAYLOAD of the TOPIC in to a variable (Channel)?

    Say I would like to SUBSCRIBE to a POWER status of some other device or Power Level of some other device.

    I am not sure if our MQTT library supports it, I would have to check.


    I wonder whether there was any progress on this feature.

    Tasmota supports SUBSCRIBE method via custom compile options.

    That is, a Tasmota device can SUBSCRIBE to another device's MQTT Topic and once such message is received, act on the contents of the message via RULES or SCRIPTING.

    https://tasmota.github.io/docs/MQTT/#subscribeunsubscribe

    https://github.com/arendst/Tasmota/discussions/14403
  • #8 21438523
    jkwim
    Level 12  
    Has there been any feature added for this?

    The requirement is to send a message to MQTT broker with SUBSCRIBE TOPIC and assign to a variable.

    Eg:
    If another sensor device periodically sends a MQTT message in the format say
    devicename/topic/temperature 100

    then by sending a message like
    SUBSCRIBE devicename/topic/temperature
    to MQTT broker,
    the MQTT broker will forward all
    devicename/topic/temperature
    messages to current device.
    The SUBSCRIBE command should also allow the received value to be assigned to a variable say "temperature".

    Then whenever the MQTT message is received its value will be updated in the variable "temperature".

    Added after 9 [minutes]:

    AI Tool gives me following answer.
    However I do not find such command in OpenBK commands list.


    Instructions for configuring MQTT on OpenBK with a subscription command.

Topic summary

The discussion focuses on executing looping scripts in OpenBeken, particularly in relation to the autoexec.bat file. Users inquire about methods to implement continuous loops, with suggestions including using a repeated event command or a goto statement within the autoexec.bat. It is also possible to create a separate script file that can be invoked from autoexec.bat using the startScript command. Additional questions arise regarding subscribing to external MQTT topics and the syntax for conditional statements in scripting, with references to Tasmota's capabilities for subscribing to MQTT topics and executing commands based on received messages.
Summary generated by the language model.
ADVERTISEMENT