Rule to check using OR and using &&

Hi All

I have a scene that I want to run when the door is locked, problem is, I want it to check for 3 different time of days using:

rule "Play arrival message when the door is locked"
when
    Item FrontDoor_DoorLock changed to ON
then
     if(Scene_Arriving_Home.state == ON && gPresenceSensors.state == ON){
        gOutsideFrontLights.sendCommand(OFF)
        KitchenBarSw1.sendCommand(ON)
        Scene_Arriving_Home.postUpdate(OFF)
      }

But, then I want to also only run this, if its night time so using vTimeOfDay I need to check for EVENING or BED or NIGHT.

And I want it to check using AND, using

  if(Scene_Arriving_Home.state == ON && gPresenceSensors.state == OFF){
      if(vTimeOfDay.state == "EVENING" || vTimeOfDay.state == "NIGHT" || vTimeOfDay.state == "BED"){
        gOutsideFrontLights.sendCommand(OFF)
      }

Im struggling to work out how to use them both in a rule, is this possible?

Thanks

Hi,
You can try to nest your if statements…something like this?

    if(Scene_Arriving_Home.state == ON){
    	
    	if(gPresenceSensors.state == ON){
    		gOutsideFrontLights.sendCommand(OFF)
    		KitchenBarSw1.sendCommand(ON)
    		Scene_Arriving_Home.postUpdate(OFF)
    	}
    	
    	if(gPresenceSensors.state == OFF){
    		if(vTimeOfDay.state == "EVENING" || vTimeOfDay.state == "NIGHT" || vTimeOfDay.state == "BED"){
    			gOutsideFrontLights.sendCommand(OFF)
                    }
    	}
    }

Hope it helps.
A.

1 Like

Hi Alessio thanks I did try this. Whilst it was accepted it didn’t work for me. Is this valid syntax ?

Add some logInfo() to check progress

1 Like

I recommend failing fast. Check whether it’s the right time to run the ruke and if not exit. I’ve documented that approach in Design Pattern: How to Structure a Rule.

1 Like

If the rule was loaded it means it’s syntactically correct, I personally have some rule with many nested statements.

Like @rossko57 said, you could try to insert some logInfo() at the beginning of every “if” to see if you are matching the states, otherwise there is something wrong either with the logic or the items’ states values…these are the only things I can think of.

Thanks @rlkoshak for the link…it gives many useful hints (and of course the rule above could be better rewritten…I was giving the quicker answer.

Thanks guys, ill give the suggestions a go.