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
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.