In a rule, how do i only run it if 2 or more item states equal something?

I want to turn on lights but i have items like sleep, home, and do not disturb and i want it to check these items. I have a motion detector that only needs to run when im home, not asleep and not in do not disturb. Thanks!

You can use multiple statements in an the if-condition with “&&” eg.:
if(home.state.equals(ON) && asleep.state.equals(OFF) && doNotDisturb.state.equals(OFF))
{
check the motion detector
do something…
}

1 Like

Thanks alot! I tried to write And, & etc but didnt work. Not quite familiar with the language yet. Thanks again!

It’s almost Java. And you can read the Java documentations if you have a question about syntax. This helps a lot. Some syntax is a little different, but most Java will work. For example methods without a parameter are called without the ().

//openHAB
timer.cancel

//Java
timer.cancel()

This is confusing for me.

It’s actually called Xtend and you can find all syntax details here:

@Oliver_Roed_Scholer are you talking about “2 or more item states” in the when part of your rule?

1 Like

Thats exactly what i mean. Its like java, but still not. Quite confusing.

Thanks for the links! It sure helps with the limited documentation so a proper syntax is great. This should be noted in the official documentation, as im sure it’ll help many. And i didnt think of it in the when part of the rule, although that would be the same, as checking it in the “then” part. This is what i have, thanks to Nawygator.

when
Item OliverPIR changed
then
if (astro_sun_set_start.state == ON && Sleep.state==OFF) {
something }

Careful here, it would NOT be the same. The When part is looking for events, not steady state conditions. Using && conditionals there would never work ,as two events would never arrive simultaneously.

2 Likes

Thank you ThomDietrich! I have been manipulating Openhab for a few years now and love it. I built many rules copying others work but not really comprehending some. What would be great, I mean really great, is if the above links could be added to the openhab page for us beginners as reference.