I changed my rules several times, but not luck to eleminate this error. The above mentioned rule simply converted a value to a switch state.
rule "Convert Battery Level To Switch State"
when
Member of GRP_BatteryLevel changed
then
// The system (persistence etc.) is always late, so wait a little bit
Thread::sleep(100)
//val String ruleName = "ConvertBatteryLevelToSwitchState"
val String groupName = triggeringItem.name.substring(0, triggeringItem.name.lastIndexOf('_'))
var SwitchItem lowBattery = GRP_LowBattery.members.filter[ aItem | aItem.name == groupName+"_LowBattery" ].head as SwitchItem
//logInfo("rule."+ruleName, "CBLTS: Rule triggered by thing {}", groupName)
if (triggeringItem.state < 5)
{
lowBattery.sendCommand(ON)
//logInfo("rule."+ruleName, "CBLTS: Battery level of thing {} is {}% and that's below 5% -> Switch low battery to ON", groupName, String::format("%d", (triggeringItem.state as DecimalType).intValue))
} else {
lowBattery.sendCommand(OFF)
//logInfo("rule."+ruleName, "CBLTS: Battery level of thing {} is {}% and that's above 5% -> Switch low battery to OFF", groupName, String::format("%d", (triggeringItem.state as DecimalType).intValue))
}
//logInfo("rule."+ruleName, "CBLTS: Rule finished")
end
First add some logInfo statements to find out WHAT is null then we’ll have a better idea on what is going on.
It could be restoreOnStartup not set
It could be that one of your items in the group is null
It could be that the lowBattery is null because it a new object instance
That error usually means one thing: you are trying to sendCommand or postUpdate or otherwise use an Object in a way that Rules DSL cannot figure out how to convert it to the right type in that context.
For example, if you send "123.4 " to a Number Item you will get this error because it cannot parse that String into a Number because of the trailing white space. Most of the time I see this error when an Item is unexpectedly NULL (see Vincent’s reference to restoreOnStartup, it might not be done restoring Item states when this Rule runs).
Add a few more logging statements and uncomment those you currently have commented out to identify exactly what line is failing. Then examine the states and values of all the Items and variables used on that line.
But the fact that it only happens occasionally implies the problem is you have an Item that is unexpectedly NULL.
I understand the options. Unfortunately the problem is, that if i uncomment the log line and add some more log lines nothing changes. I still get this error without any hint
The log lines do not fix the error. You use the logs to identify what line is generating the error. You can’t fix it until you know what line is causing the error.
Look in the logs. The log statement immediately preceding the error tell you that the error takes place after that logInfo in your rule. If you make a log statement be every other line in your rule you will know that the line immediately after that log statement is the one generating the error.
Now you have identified the exact line generating the error you know what Items and variables might be causing the problem. From there you need to know what those Item states are and variables are set to and you should be able to see what is causing the error.
But that is the problem. Even if i put a log line after each statement i get no more information, because if i get the “null” error the rule ins’t executed at all. the the logline before the error is a stement from anotehr rule and the line after as well
I will extend the rule and post the logfile so that you will see the problem