Why is there no `and` in Rule Triggers?

This topic comes up several times a year so here’s a post we can link to in answer.

The source of the confusion for why we can’t use and in Rule triggers is that Rules are triggered based on events, not state.

At it’s core openHAB is an event driven system. Devices generate an event which gets put on an Event Bus. Parts of openHAB listen on this Event Bus and perform actions on those events. One of the actions that can be performed on those events is triggering Rules.

Thus, Rules are completely event driven. The events that trigger a Rule are defined in the when section of the Rule. It makes no sense to have an and between triggers because two events are unlikely to ever occur at exactly the same time.

In Rules DSL, if you only want a Rule to run when an event occurs and some other Item is also some desired state, then you must use an if statement in the Rule. For example:

rule "Do something only when two Items are ON"
when
    Item MyItem1 changed to ON or
    Item MyItem2 changed to ON
then
    if(MyItem1.state != ON || MyItem2.state != ON) return;

    // rule code
end

If you are using the PaperUI Experimental Next Gen Rules Engine, you would put the conditions in the “But only if…” section.

4 Likes

Doh! Thanks.