ERROR in RuleEngine - Rule XXX: null

Hi,

still the same question. I get the following error from several rules. Not an a regualr basis but sometimes, mainly at startup.

2018-04-06 10:51:13.702 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Convert Battery Level To Switch State': null

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

Has anyone an idea on that?

Thomas

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

  • Log statements are there and there are giving no more information if the error occurs
    . restoreOnStartup is set

and on the other two questions: How do i find out?

The logInfo statements will tell on what line of the rule the error occurs.
You need to add a logInfo statement after each line

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

Be sure to make the very first line of the rule be a log statement.

You probably don’t need the thread sleep. You are not doing anything that depends on persistence in that rule so there is no need to sleep.

But if you don’t have a log statement as your first line and your actual first line in the rule has the error you would not see any log statements.

I still have this error from time to time and according to my opinion, it has nothing to do with the rule code itself.

Even if you check everything with an if statement the error still occurs.

Sometimes (not reproduceable, it’s some kind of a race condition) openHAB is unable to run the rule.