Using OR and/or AND in IF statements in rules

I have a rule:

// ###########Activitiy
rule "Activity scene selection"
    when
        Item Scene_Activity received command
    then
        switch(receivedCommand) {
            case 0 : return false                             //no selection
            case 1 : {                                        //TV
                callScript("LG_srcHDMI2")
                if (HTPC_Radio.state != "OFF") {
                    sendCommand(HTPC_Radio, "OFF")
                    sendCommand(Marantz_Power, OFF)
                }
                if (Scene_Activity.state == 2)) {
                    sendCommand(HTPC_Radio, "OFF")
                    sendCommand(Marantz_Power, OFF)
                }
               
            }
...

As you can see i have two nearly identical if statements in the rule, but i’ don’t know how i can join them:
i was thinking something like:

            case 1 : {                                        //TV
                callScript("LG_srcHDMI2")
                if (HTPC_Radio.state != "OFF") or (Scene_Activity.state == 2) {
                    sendCommand(HTPC_Radio, "OFF")
                    sendCommand(Marantz_Power, OFF)
                }
            }

but this throws an error:

2016-04-13 19:46:23.237 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Activity scene selection': Ambiguous methods [protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAssignment(org.eclipse.xtext.xbase.XAssignment,org.eclips

I haven’t been able to find any examples of using or and and in if satements in rules in this way. I know it is possible to use them in the when part of the rule but not in the if statement in the then part of the rule.

Any suggestions?

That is because the proper syntax is:

// OR
if(HTPC_Radio.state != "OFF" || Scene_Activity.state == 2) {

// AND
if(HTPC_Radio.state != "OFF" && Scene_Activity.state == 2) {

Only “or” is allowed in the if part of a rule. Rules are event triggered and no two events will ever occur at the exact same time so it makes no sense to have an “and” in the when clause of a rule.

NOTE: I highly recommend using Designer which will tell you about this sort of syntax error before you run it. And with <ctrl><space> key combo you can discover valid ways to end a particular statement.

9 Likes

Thanks!
Is it also possible to use the same concept with sitemaps i.e. valuecolor, labelcolor and visibility?

Not quite. Per the documentation on the sitemap wiki page, for dynamic parameters like that you can specify more than one condition, separating them with “,” but the first one to be true will take precedence. However, you cannot “or” or “and” multiple tests into one conditional. To get an OR like behavior, you can use the ordering of the conditionals. For an AND you need to create a virtual switch which gets set by a rule (e.g. ON if your AND conditionals are true) and use that in your sitemap conditional.

Join with the pipe sign “II” for OR. Join with the “&&” sign for AND