Rule logic question - repeating the same condition

Hi,
question, i have rule (dsl)

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? :slight_smile:

ok i guess I can nest the if condition inside the first if

if (item1.state == ON || item2.state == ON)  {
action1
action2
action3
if (item1.state == ON) {
action4
}  
  }

Is one item part of the trigger?

Greets

Yes, item1 is one of the triggers, item2 is not

I think your first rule is ok.
But you wrote the second condition the item2 has not to be on. So I would check this too.

if (item1.state == ON || item2.state == ON)  {
action1
action2
action3
}
if (item1.state == ON && item2.state == OFF) {
action4
}

or

if (item1.state == ON && item2.state != ON) {

Greets

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

I am actually using triggeringitemname…
thanks everyone

1 Like