If or condition not working -help!

Hi Everyone,
I’m new to OpenHAB but have been using Vera device and Zway on a RPI for some years to run my home automation - finally got fed up of the rubbish Vera UI and decided to switch so I’m trying to move all my rules over to OH although for now leaving the devices attached where they are.
Thanks to all the great info here in the community, I’ve got OH2 running on the Raspberry Pi 3B and all working ok, showing and controlling my Vera and Zway devices ok but I’ve hit a problem trying to write my first rule.

Code is below, but is not behaving as expected and I’d appreciate some advice as to whether the code is right or not. Basically none of the motion states after the first two in the ‘OR’ part of the if condition trigger correctly. It gets to the ‘Hall lamp on’ bit if front door or loungemotion are triggered but not with landingmotion or dining4in1sensor. When I test the conditions individually they all work ok, just not together. Any help much appreciated, I’ve spent a couple of hours on this already!

rule "Hall Light"
when
    Item vTimeOfDay received update or
    Item Frontdoor2Tripped received update or
    Item LoungemotionTripped received update or
    Item LandingMotionTripped received update or
    Item Dining4in1sensorTripped received update
then
if (vTimeOfDay.state=="EVENING" && (
        Frontdoor2Tripped.state == OPEN ||
        LoungemotionTripped.state == OPEN ||
        LandingMotionTripped.state == OPEN ||
        Dining4in1sensorTripped.state == OPEN
        )
    )
    {
        logInfo("lighting.rules","Hall light on")
    }
    else
    {
        logInfo("lighting.rules","Hall light off")
    }
end

Mark

Use logInfo() to find out what those states are at rule runtime
Are all your Items Contact types?

1 Like

First, you used the wrong tick marks for code fences. You want a back tick ```, not a single quotes ‘’’. On a US keyboard it the key to the left of the ‘1’.

As rossko57 suggests, add logging to log out the states of all the Items in that if clause.

Look at events.log and verify that those Items are changing state and the state is what you expect. For example, as rossko57 asks and hints at by asking, if Dining4in1sensorTripped is a Switch Item instead of a Contact Item, it’s state will be ON, not OPEN.

Next, assuming these are all Contact Items, you should use Groups. Add these four Items to a

Group:Contact:OR(OPEN, CLOSED) HallLightControls

The OR(OPEN,CLOSED) means if any one of the members of HallLightControls is OPEN, HallLightControls will be ON. Otherwise only if all members of HallLightControl are CLOSED will HallLightControl be CLOSED.

This basically implements your if statement. So your Rule would become:

rule "Hall Light"
when
    Item vTimeOfDay changed or
    Member of HallLightControls received update
then
    var newHallLightState = OFF
    if(vTimeOfDay.state == "EVENING" && HallLightControls.state == OPEN) newHallLightState = ON

    logInfo("lighting.rules", "Hall light " + newHallLightState)
end

The first two lines can be reduced to one using the trinary operator.

    val newHallLightState = if(vTimeOfDay.state == "EVENING" && HallLightControl.state == OPEN) ON else OFF

With this approach, if you need to add new sensors or take some away, you just need to add/remove them from the HallLightControl Group and you never have to touch the Rule again.

Thanks for the replies - I’ve fixed the code fences - sorry about that, I was posting from my phone so didn’t realise the difference!
The new Hall light rule is working now and much neater too.
thank you
Mark