Convoluted IF Statement

I had an IF statement in an app that looked for several different items. Always worked fine. It is as follows:
if (location.mode == "Home" && doorSwitch.currentValue("contact") == "closed" && disableSwitch.currentswitch == "off") {

I added another contact and wanted to know if either of them was closed so created this IF statement. It doesn't work. No errors, just doesn't work.
if (location.mode == "Home" && (doorSwitch.currentValue("contact") == "closed" || screenSwitch.currentValue("contact") == "closed") && disableSwitch.currentswitch == "off") {

I separated the 2 contacts into a separate nested IF statement and it works as intended.

So my question, is the complicated IF statement syntax not allowed?

What do you mean? What does it do, and what did you expect it to do?

Is this a typo: disableSwitch.currentswitch (should be disableSwitch.currentSwitch)?

I probably wasn't too clear.

It's the center part where I'm looking for either of 2 contacts to be closed. When I have just one contact there, all is fine and the IF evaluates correctly. But when I want to see if either of the contacts are closed, not necessarily both is when it doesn't evaluate correctly.

One suggestion-- I find it much easier to read with nested IF statements, rather than using && everywhere...

For instance, instead of writing (if A && B && C && D... you can write:

if A then
   if B then
      if C then
         if D ...
         endif
      endif
   endif
endif

What does it evaluate to? Can you give a more specific example of how it is failing? It should work the way you wrote it.

Sometimes, purely by coincedence, 2 things go wrong at the same time. Here is what happened.

We have a motion detector at the back door to let us know when the dog wants back in. When the motion trips our Google unit says Bark, Bark. If the door is open we don't need that so it only notifies if motion activates and the door is closed.

Had the door replaced with a new type and it worked better to have 2 sensors. So I put the 2 sensors on right after the door was installed. Then modified the app as above and it didn't work. Since, in my mind, the only change was the second sensor I thought that was the problem.

I checked logs, events, etc. and everything seemed to be detecting as normal. Then I started to putting in some debug statements and finally realized the motion detection wasn't ocurring until the door was opened. Got to checking and it turns out the guys putting in the new door messed up the motion detector and it was pointing in the wrong direction. Once I corrected that everything started working again.

So sorry for the mixup here. Thanks for all who tried to help.

1 Like