Check for NULL not working

i have a smartmeter inside a small box connected to my car. while charging I send MQTT messages. like absolute kwh, voltage, amps,…

Now a rule makes problems that the EVchargeWhImp (absolute kwh at the meter) often is NULL when unplugged an openhab got resetted…and i wanted the rule to check if NULL and so skip the rule and next time when MQTT is sent another time the NULL got overwritten and all is fine and rule can be processed… Should the NULL-killer work like this? >>>> EVchargeWhImp.state != NULL <<<<<<

rule "ChargeStart Detection"
    Item EVchargePower received update
    if ((EVchargePower.state > 1000) && Car_Charging.state != ON && EVchargeWhImp.state != NULL)
           { Car_Charging.sendCommand(ON)
             EVchargeStartMeter.postUpdate(EVchargeWhImp.state as DecimalType)

Ifs are evaluated from left to right. With ANDs the evaluating stops at the first case that is not true.
I would think that a small reordering will fix the problem:

if (EVchargeWhImp.state !== NULL && (EVchargePower.state > 1000) && Car_Charging.state !== ON)

Also, note the double “==” :slight_smile:


The idea is absolutely correct, test for state NULL before trying to e.g. evaluate as number in a comparison, which would fail with non-numeric NULL.

But no, you shouldn’t use !== or === with NULL, the usual != and == should be used.


oh, I have many statements like this in my rules:

if ( banana.state !== NULL && banana.state == "peeled" ) { .. ..}

It does work, but it might not be the correct thing to do :slight_smile:

OK thanks a lot for your input!

So with NULL (undefined)…i have to use != …and not !== ??

But isn’t the order in the IF or completely irrelevant for the performance…or you mean that if the NULL part is checked … it would immediately close the IF branch and not go through the rest, is that the intention of different order in the if?


That’s it. So it will then avoid generating an error when, say, trying to convert NULL to a number in order to compare with another number.

Technically the if() test has to be completed at that point.
if (X is true) AND (Y is true)
If X turns out to be false, there is no need to even look at Y
if (X is true) OR (Y is true)
If X turns out to be false, we must still look at Y to solve the whole statement