logo elektroda
logo elektroda
X
logo elektroda

Conditional (if) statement sample for OpenBeken? How to script?

ferbulous 513 7
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21200814
    ferbulous
    Level 18  
    Hi, is there example for doing if statement in openbk?
    In tasmota, i have this rule where single press from button 1 would only toggle relay 1 if wifi is online & relay 3 is on

    rule1
    on wifi#connected do var1 online endon
    on wifi#disconnected do var1 offline endon
    on switch1#state do publish switch1 %value% endon
    on switch1#state=2 do IF ((%var1%=online) AND (%power3%==1)) power1 toggle ELSE power3 toggle ENDIF endon


    How do I do something similar in the script?
  • ADVERTISEMENT
  • #2 21231546
    ferbulous
    Level 18  
    ferbulous wrote:
    Hi, is there example for doing if statement in openbk?
    In tasmota, i have this rule where single press from button 1 would only toggle relay 1 if wifi is online & relay 3 is on

    rule1
    on wifi#connected do var1 online endon
    on wifi#disconnected do var1 offline endon
    on switch1#state do publish switch1 %value% endon
    on switch1#state=2 do IF ((%var1%=online) AND (%power3%==1)) power1 toggle ELSE power3 toggle ENDIF endon


    How do I do something similar in the script?


    Hi @p.kaczmarek2
    Bumping my post again
    Is there an example for IF statement? I’ve looked at the command list here but couldn’t find any

    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands-extended.md
  • ADVERTISEMENT
  • #3 21231558
    p.kaczmarek2
    Moderator Smart Home
    Sorry, I missed that, please bump earlier next time!
    So, there are some samples there:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    I would split your long line into aliases, like:
    
    alias Toggle3 toggleChannel 3
    alias Toggle1 toggleChannel 1
    alias Do_Check if $CH3 then Toggle1  else Toggle3 
    

    And for wifi state:
    https://github.com/openshwprojects/OpenBK7231...docs/autoexecs/wifiled_and_relay_state.bat#L6
    Helpful post? Buy me a coffee.
  • #4 21231694
    ferbulous
    Level 18  
    @p.kaczmarek2
    Thanks but how do I have it check for both condition?
    If channel 3 is on & wifi connected then execute the command?
  • ADVERTISEMENT
  • #5 21231704
    DeDaMrAz
    Level 19  
    For example

    if $CH3==1&&WiFiState==4 then "setChannel 1 1" else "setChannel 1 0"

    You can do that in a loop, for example:

    loop:
    //if CH3 is on and WiFi is connected (==4) then whatever command
    if $CH3==1&&WiFiState==4 then "setChannel 1 1" else "setChannel 1 0"
    delay_s 1
    goto loop
  • ADVERTISEMENT
  • #6 21238808
    ferbulous
    Level 18  
    DeDaMrAz wrote:
    For example

    if $CH3==1&&WiFiState==4 then "setChannel 1 1" else "setChannel 1 0"

    You can do that in a loop, for example:

    loop:
    //if CH3 is on and WiFi is connected (==4) then whatever command
    if $CH3==1&&WiFiState==4 then "setChannel 1 1" else "setChannel 1 0"
    delay_s 1
    goto loop


    Thanks, so it should look something like this?

    alias Toggle3 toggleChannel 3
    alias Toggle1 toggleChannel 1
    alias Do_Check iif $CH3==1&&WiFiState==4 then Toggle1  else Toggle3 
    delay_s 1
    goto loop


    So without loop, it only runs once?
    And can I still add another condition on top of that like in tasmota?
  • #7 21239286
    DeDaMrAz
    Level 19  
    It's impossible for me to explain it all in details as you have the device in hand and an idea how it should work but I can try and explain how scripting works to a degree so you can have a starting point.

    In general I am not that familiar with tasmota format so I don't have the answer for that question in particular but you can use multiple conditions in your loop in OBK.
    If you want to do something repeatedly then yes, loop is the answer.

    Overall structure should be something like this:

    //declare aliases and their functions
    alias ONE command 1 or condition 1
    alias TWO command 2  or condition 2
    alias THREE command 3 or condition 2
    
    //define loop and conditions for it
    whatever loop name
    if <condition 4> then ONE else TWO
    if <condition 5> then THREE else SOMETHING
    if <condition 6> then SOMETHING ELSE
    goto whatever loop name


    Conditional expressions are supported like ==, ||, <>, && etc and you can also use backlog to stack commands that are separated by semi-colon for example:

    alias ONE backlog command 1; command 2;...


    Example I provided is for continual checking of wifi and ch3 state. On the other hand if you don't need to continuously check for wifi state or channel 3 state then what @p.kaczmarek2 wrote is perfectly fine, it will be checked once on every power up/reboot.

    One more thing, there is a OBK simulator EXE available now as well that you can use to test your ideas before implementing them in real HW.
    See the release page here - https://github.com/openshwprojects/OpenBK7231T_App/releases

    Also please take a look in the docs pages for more examples here - https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md
  • #8 21274793
    p.kaczmarek2
    Moderator Smart Home
    OpenBeken simulator is now included in builds, at least the version for Windows is:
    https://www.elektroda.com/rtvforum/topic4046056.html
    https://github.com/openshwprojects/OpenBK7231T_App/releases/tag/1.17.755
    So you can test most of the cases on your PC, well, maybe except the WiFiState, as it's not simulated on Windows yet.
    Screenshot of a download table for OpenBeken files, with the obkSimulator_1.17.755.zip file highlighted.
    Screenshot of a ZIP archive named obkSimulator_1.17.755.zip featuring a list of files and folders.
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around implementing an IF statement in OpenBK to control relay toggling based on WiFi connectivity and the state of another relay. The user seeks to replicate a Tasmota rule that toggles relay 1 only if WiFi is online and relay 3 is on. Responses provide guidance on using aliases for commands and structuring conditions within a loop. Examples illustrate how to check multiple conditions, such as the state of channel 3 and WiFi connectivity, using syntax like "if $CH3==1&&WiFiState==4". The conversation also mentions the inclusion of an OpenBeken simulator for testing scripts on Windows.
Summary generated by the language model.
ADVERTISEMENT