RulesDSL: How to run rule as a subcode of other rule

Hi guys,
I am trying to figure out, how to write a RulesDSL code, which has a link to other RulesDSL code inside.

Example:

first rule:

rule "RULE1"
when
    Time is noon
then
    if(condition1.state == 50){
        logInfo("obyvacka-rolety.rules", "sing a song")
    }
end

second rule should run the first inside:

rule "RULE2"
when
    Channel "mihome:sensor_cube:286c0785f092:158d000110649b" triggered SHAKE_AIR
then
    if(RoletaKuchyna_Position.state == 0){
        logInfo("obyvacka-rolety.rules", "kocka_rolety_dole")
        RUN RULE1
    }
end

how should the RUN RULE1 command looks like?
Thanks,
Michal

Add another trigger condition to rule 1 such as “or Item XXX received command”, then call it via XXX.sendCommand()

so like this?:

rule "RULE1"
when
    Time is noon or
    Item RULE2 received command
then
    if(condition1.state == 50){
        logInfo("obyvacka-rolety.rules", "sing a song")
    }
end
rule "RULE2"
when
    Channel "mihome:sensor_cube:286c0785f092:158d000110649b" triggered SHAKE_AIR
then
    if(RoletaKuchyna_Position.state == 0){
        logInfo("obyvacka-rolety.rules", "kocka_rolety_dole")
        RULE1.sendCommand()
    }
end

No
RULE2.sendCommand(ON)

And define the item RULE2, of course

1 Like

You can also put the second rule into a .script file in the scripts folder and use callScript to execute it. There are some limitations though. You can’t pass any data to the Script nor can you get anything back. And you can’t import anything so if you are using anything that isn’t already imported by default you’ll have to use the class’s full name. More on Scripts at A simple scene management: finally a good use of scripts.

A third potential option is lambdas. These too have significant limitations including not being thread safe, only being accessible from the file it’s defined in, and you have to pass everything it needs to use as an argument. See Reusable Functions: A simple lambda example with copious notes. Note though because of the limitation, I usually recommend against their use.

In OH 3 and the UI, there is an action to trigger another rule which, if your rules remain as simple as the example, should be easily implemented without any code at all.

And of course, the other rules languages support importing libraries as well as interacting with openHAB core to run another rule from a rule.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.