Can I call a rule from within a rule?

I’m trying to call a rule from within another rule but I cannot get it to work.

I created a rule to control home ventilation in speeds 1-2-3 using a double switch (Philio PAN06-1).
I have three switch items for the PAN06 and the rule sets a combination of the relays to obtain a certain fan speed. I have three of them: FanHigh, FanMedium, FanLow.

Now I’d like to be able to call these rules from another script where I monitor humidity in the rooms, as not to repeat the code for the Fan* rules in different places.

Any suggestions?
Thanks,
Niels

There are three approaches which can work. It isn’t clear from the information provided which would be the best.

The first is lambdas. This lets you create a function that contains the reused code which your rules can call. The link is to the wiki page that describes how to write them. One limitation with lambdas is that they can only be called from rules in the same file the lambda is defined. Another is they can only take up to six arguments. Finally they don’t return a value so you have to use globals to store the results if there are any.

The second approach is to create a trigger on the rule you want to reuse for an Item. Then your other rules will sendCommand to this Item to cause the rule to execute. A limitation here is that it isn’t easy to tell which item triggered the rule. But this will work whether or not the rule that called sendCommand is in the same file as the rule to execute or not.

The third is to use openHAB’s scripting capability. A limitation here is you cannot pass anything into a script. It has to be wholly self contained. A script can be called from any rule.

3 Likes

Thanks rikoshak, I will give it go tomorrow.

Thanks that had an answer i was looking for there i think i will use the trigger method

See Design Pattern: Separation of Behaviors for a full writeup and example for the trigger method.