Not Equals in rules?

I’m probably missing something but I can’t seem to find a reference to running a not equals statement in an if.

if(results!=ON) {
   do something
}

doesn’t work

if(results==ON) {
   do something
}

does work, but is the opposite of my requirement.

That’s the correct syntax for not equals.

Can you post the whole rule?

1 Like

The complete rule is:

rule "Lights turned off"
when
    Item mqtt_topic_195bff72_lightswitch_tuya_stairs changed from ON to OFF
then

        results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key OFF",5000)
        logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key STATUS",5000)
        logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        if(results3!="OFF"){
                results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key OFF",5000)
        }

end

The log displayed the following:

2020-07-09 09:26:51.032 [INFO ] [se.smarthome.model.script.tuya.rules] - SD-Off Script output:
2020-07-09 09:26:52.172 [INFO ] [se.smarthome.model.script.tuya.rules] - SD-Off Script output: ON

so it should have executed the if statement.

What makes you think it didn’t?

You could extend your diagnostic

logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        if(results3!="OFF"){
                logInfo("tuya.rules", "third exec coming up")
                results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key OFF",5000)
                logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        }

There are 3 lights that have the same rules. This one I missed one line. Added in now.

        if(results3!="OFF"){
                results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key OFF",5000)
                logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        }

So when trying to turn off, it executes the off. checks status, recorded in the variable results3 as “ON”. So if the IF statement had worked, then the off command again should execute again and a third log entry.

Not sure if xtend does some magic with String comparisons, but in Java, you need to use the equals() method. Try it like this:

if (!results3.equals("OFF")) {
//do stuff
}

Well if you’re showing us the wrong rule or only parts, maybe we’re not seing the full log either.
Try this

        if (results3 != "OFF"){
                logInfo("tuya.rules", "not-off")
                results3 = executeCommandLine("node /etc/openhab2/scripts/njsTuya/njstuya.js -mode local -ip 192.168.1.44 -id idhere -key key OFF",5000)
                logInfo("tuya.rules", "SD-Off Script output: {}", results3)
        } else {
                logInfo("tuya.rules", "not not-off then")
        } 

I put spaces in my 'if’s for clarity, but sure it doesn’t matter.

so using that rule, the following was in the log:

==> /var/log/openhab2/openhab.log <==

2020-07-09 14:41:46.569 [INFO ] [se.smarthome.model.script.tuya.rules] - SU-Off Script output1: OFF
2020-07-09 14:41:47.655 [INFO ] [se.smarthome.model.script.tuya.rules] - SUOff Script output2: OFF
2020-07-09 14:41:47.665 [INFO ] [se.smarthome.model.script.tuya.rules] - not not-off then

So now it seems ok. Maybe it was the “OFF” instead of OFF. Will test some more.

results3 from exec action will certainly be string "OFF" or "ON" or whatever, and would never match the OnOffType OFF as used with switches.

Is it working for you with ON case, which seemed to be the one giving you trouble?

Are you sure this is from the rule you posted above? Because the text in the log lines aren’t the same.

Have you checked for whitespaces in the script output?

logInfo("tuya.rules", "SD-Off Script output: ({})", results3)

If there are any between the parentheses in the log output, this could be the issue.

Hi Anders,

I wrote the rule (without logging), copy and pasted for x6 and changed some to be ON some OFF. Then I added the loginfo afterwords manually. So the wording in the log might not be 100% but the rules are.

The first time I wrote the rule I used :

if(results!=ON) {
and if(results!=OFF) {

it didn’t work.

Second time:

if(results3!="OFF"){

again no luck.

then using Rosko37’s example:

 if (results3 != "OFF"){

It did work.

So thanks for the help everyone, resolved. :grinning:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.