[SOLVED] Rule, if criteria is furfilled - do nothing

My brain must be fried these days. I simply cant make a simple rule optimized :rage:

This is the exsisting rules which I´m trying to optimize:

rule "Quooker ON or OFF"
when
    Item FF_Sonoff_POW_Load changed 
then
	if (FF_Sonoff_POW_Load.state as Number >= 2){
         Quooker.sendCommand(ON)
           logInfo("info", "Quooker is ON")
    }
 else
	 Quooker.sendCommand(OFF)
end

Simple rule logic explained.
When Sonoff_POW changed.
If value of Sonoff_pow is bigger than 2, then sendcommand ON to Quooker item…
Else sendCommand OFF to Quooker.

Optimized:
Now, what I want to optimize is, that if the criteria is already furfilled, then do nothing…
Ie. If the value is equal to or above 2, and Quooker is already ON, then do nothing. (There is no need to update Quooker item, if its already ON).

I just cant get it to work, even though I believe it should be pretty simple as well… (Hmm I really hate that word, simple).
I tried insert:

if (FF_Sonoff_POW_Load.state as Number >= 2 && Quooker.state == ON) return;
    if (FF_Sonoff_POW_Load.state as Number <= 1 && Quooker.state == ON) { 
        Quooker.sendCommand(OFF)
else
       Quooker.sendCommand(ON)
end

But then I end up with errors

2019-11-24 21:05:38.179 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'quooker.rules' has errors, therefore ignoring it: [6,45]: no viable alternative at input '='
[6,47]: mismatched input '1' expecting '>'
[6,71]: mismatched input ')' expecting 'end'

Anyone got a hint as to how I get my brain back on track :smiley:

Maybe put a check for the Quooker being on inside your check to see if it is >= 2?

rule "Quooker ON or OFF"
when
    Item FF_Sonoff_POW_Load changed 
then
    if (FF_Sonoff_POW_Load.state as Number >= 2) {
        if (Quooker.state != ON) {
            // not ON, so send command to turn it ON
            Quooker.sendCommand(ON)
            logInfo("info", "Quooker is turned ON")
        } else {
            // don't do anything
            logInfo("info", "Quooker is already ON")
        }
    }
    else {
        Quooker.sendCommand(OFF)
    }
end

It’s from
if (FF_Sonoff_POW_Load.state as Number <= 1 && ...

The parser thinks it is has found the beginning of Number<something>

Brackets should disambiguate that
if ( (FF_Sonoff_POW_Load.state as Number) <= 1 && ...

use brackets…

rule "Quooker ON or OFF"
when
    Item FF_Sonoff_POW_Load changed 
then
    var newState = OFF
    if((FF_Sonoff_POW_Load.state as Number) >= 2) 
        newState = ON
    if(Quooker.state != newState) {
        Quooker.sendCommand(newState)
        logInfo("info", "Quooker is {}",newState)
    }
end
1 Like

Kim
when you think the rule should work a certain way and it does not, litter the code with log statements and make sure what you think stuff supposed to be really is what you think

Yeah you´re right… I was just so focused on this beeing so simple, so I thought it wouldn´t need a log :face_with_raised_eyebrow:

Thanks rossko… I had a feeling it might be it… I just couldn´t see it…

Thanks Udo… Seem like this is quite a simple, but look more “readable”.

1 Like