Understanding the logic behind AND and OR

Ok so please enlighten me of hubitat logic when using multiple AND and OR.
So if i have switch 1 AND switch 2 it means both need be true
if i have switch 1 OR switch 2 then only one of them must be true.
Now if I have switch 1 AND switch 2 OR switch 3, how is this evaluated, normally in programming you could use a parentheses and say if (switch 1 AND switch 2) OR switch 3 so it would evaluate the conditions from the paratheses as one then compare it with the last condition.
Or the second outcome could be if switch 1 AND (switch 2 OR switch3) .
Please tell me, thanks

I would assume it would be the same... Are you seeing something different or just asking out of curiosity?

If you are not aware, apps like RM are written using Groovy, a close derivative of Java from what I understand....

Make a test rule and see for yourself how it evaluates the IF-THEN condition.

3 Likes

It's true that it's not clear from the way the rule UI displays it. But the test above should clear it up. I'd like to know the outcome too. Don't forget to refresh the page of the rule each time you change a variable.

1 Like

How would you prefer the UI display it? It's a fundamental concept in programming which RM is ultimately looking to represent in a graphical form of sorts.... but I would argue there are only so many things you can simplify or hide away from the user.

You can add the parentheses in Rule Machine. I have this in a few of my rules. Iā€™ve not tried in the latest version but you should be able to insert an open parenthesis above ā€˜switch 1ā€™ and a close parenthesis above the ā€˜orā€™

4 Likes

I've always opted to include parenthesis, mostly because I can't be bothered to learn how a combination of AND's and OR's is evaluated :slight_smile:

5 Likes

I think on the old interface it wasnā€™t obvious. You had to add an action, conditional and it was somewhere in there - about 5 or 6 clicks to get a bracket

1 Like

But the ease of use and the evaluation of the logic, which I believe was the question, would be two different things.

Well exactly. That's the whole basis of this thread.

On my 1st post I gave 2 examples with 3 conditions so only 1 could be true.

sorry, how do i add parenthesis, is this possible to take away any confusion ?
edit:
found them, thanks, now i should be able to make all clear.

1 Like

Yes, you can include parenthesis. Hopefully others here can guide you through it, I have logged off my PC for the night.

AND has precedence, I believe, if no brackets are used

T AND T OR F = True
T AND F OR T = True
T AND F OR F = False
F AND T/F OR T/F = False

2 Likes

https://groovy-lang.org/operators.html#_precedence

2 Likes

It's rare if not impossible for a platform / language to ignore the precedence of parenthesis, so I always include them..... It removes any ambiguity or questions such as these about how a string of conditions are likely to be interpreted.

3 Likes

When you use parentheses, the evaluation is more obvious. But the general answer to your question is this: Evaluation of an expression proceeds left to right, and there is NO operator precedence. If the evaluation's outcome at any point is TRUE and OR is encountered, the evaluation stops and TRUE is returned. If the evaluation's outcome is FALSE and AND is encountered, the evaluation stops and FALSE is returned.

This should be clear as mud now. :sunglasses:

6 Likes

That would explain some weird behavior I've seen before.

The expression True AND False OR True should evaluate to true no matter which comparison is made first. But if it immediately returns false when encountering the AND False part of the evaluation it will return false.

In a programming language with operator precedence maybe, but True AND False is false no matter what in RM expression.

True AND (False OR True) is true.

1 Like

Well I don't mind which way round it all is, I am just pleased to know.