if (item1.state == ON || item2.state == ON) {
action1
action2
action3
}
if (item1.state == ON) {
action4
}
so, I want to execute some actions if either of the items are ON, but I also have some specific action that should only execute if item1 is ON, and not if item2 is ON.
Is this rule “healthy”? or it should be written in some other way? Will it even work fine? Is there a better way?
There isn’t enough information to tell if this is possible. But if we assume if both Items are not ON the rule should do nothing you can fail fast. This can simplify things.
if(item1.state != ON && item2.state != ON) return;
// Now we know at least one of them is ON so we don't need to test for that again
action1
action2
actiion3
if(item1.state == ON){
action 4
}
If you are using managed rules, you can put the != ON tests into a Condition (but only if… part of the rule).
If your item1 is a trigger, I believe you may need to use one of the implicit variables to get the state instead of item1 itself since it may not be updated when the rule starts running. See Textual Rules | openHAB.
That’s only true for received command triggers. received update and changed triggers happen after the Item changes state but command occur before the Item is updated (and a command may never lead to an update at all).