I have the following rule that works fine, but I get a start-up error that I have tried to filter out (unsuccessfuly) with a Null check in the rule.
rule "Poll with Main Setpoint change Cool"
when
Item MainThermostat14_SetpointCooling changed
then
var pollcount = Furnace_Poll_Count.state as Number
var mainmode = MainThermostat14_ThermostatMode.state as Number
var mainstate = MainThermostat14_ThermostatOperatingState.state as Number
logWarn("events", "pre-if status new state{}, old state {}, mainstate {}", newState, previousState, mainstate)
if(previousState == 'NULL' || previousState === null ) {
logWarn("events", "Null found , new state {}, old state {}", newState, previousState)
}
else {
if ( mainmode == 2 && ((newState >= previousState && mainstate == 2) || (newState <= previousState && mainstate == 0))) {
logWarn("events", "poll triggered new state{}, old state {}, mainstate {}", newState, previousState, mainstate)
MainThermostat14_ThermostatOperatingState.sendCommand ("REFRESH")
Furnace_Poll_Count.sendCommand (pollcount + 1)
}
}
end
The log items are;
2022-02-03 08:23:12.226 [WARN ] [org.openhab.core.model.script.events] - pre-if status new state75 °F, old state NULL, mainstate 0
2022-02-03 08:23:16.804 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'refresh-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.lib.NumberExtensions.operator_greaterEqualsThan(org.openhab.core.types.Type,java.lang.Number) on instance: null in refresh
Any idea how to avoid getting to the >= operator, if the previousState is NULL (or null)?
This can never happen. Either previousState will exist with a proper value (e.g. NULL) or it wonât exist at all.
The problem isnât that the previous state is NULL. The problem is that newState and previousState are both State Objects. Thatâs the parent of all State type. The engine doesnât know that they are Numbers and can be compared and canât figure that out on itâs own. Unfortunately it reports that as âon instance nullâ because the result of trying to figure out a proper type to work with resulted in nothing being found.
You need to help it along.
(newState as Number >= previousState as Number)
Note that if either newState or previousState are in fact NULL or UNDEF a different exception will occur.
So Iâm trying to understand. Maybe I wasnât clear or maybe Iâm lost. This rule only springs an error on startup (works fine otherwise). I thought the sequence of events at S/U is that persistence is restoring values 09:55:17.347 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'MainThermostat14_SetpointCooling' changed from NULL to 75 °F
Then it seems like the rule engine goes does some rule check and the previous state was NULL. Do I want to compare 75 °F as Number >= NULL as Number (not really)? My naive thought was to try to prevent that part of the rule from trying to run on startup.
All I can say is that the problem is caused by the fact that one of the operands (either newState or previousState) cannot be coerced to something that can be compared.
Now that I look closer, previousState == 'NULL' will never be true. NULL isnât a string. Itâs not the word âNULLâ. It is an enmeration. previousState == NULL. The same goes for ON, OFF, OPEN, CLOSED, INCREASE, etc.