Possible to get rules to log WHERE a null error occurred?

  • Platform information:
    • Hardware: RPi-4
    • OS: OpenHabian
    • openHAB version: OH3
  • Issue of the topic: Where’s the error (Rules)

When a rule errors with a NULL variable… is there any way to get it to say WHERE in the rule the error was? e.g.

2021-05-05 19:45:01.761 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'heating-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.lib.NumberExtensions.operator_greaterThan(org.openhab.core.types.Type,java.lang.Number) on instance: null in heating

There’s an error somewhere… A variable is null… but trying to find it is pretty much drop logs in place and retry. Which wouldn’t be as annoying if there was a debug option (i.e. enable/disable a flag so ‘debug’ logs were shown/not shown - yes I could wrap it in an if() but… Ease of use…

No. Logging is the answer. See Add lots and lots of logging to your rules in

With Rules DSL the variable may not be null. In fact, most of the time when you see this error it’s because the two operands are not compatible. When the Rules DSL is unable to coerce a type into one that is suitable for the operation it generates this sort of error.

It can’t tell you which line in your rule it failed on because this error is generated and kills the rule deep inside the rules engine where knowledge of which line of your rule is being interpreted is unknown. And the error stays there and never filters back up to your rule where you could catch it.

But there is a lot of useful information in that error which should help you find the line pretty quickly, with the help of some logging.

The rule UID is “heating-1”. That means the error occurred in the first rule defined in heating.rules.

The method it failed on is “operator_greaterThan” so the line that failed involves a > comparison. So that narrows things down to those lines where you see >.

Finally, now that you know that this error usually means you have a type error, and knowing that a Number Item can carry the non-number states NULL and UNDEF, I suspect your root problem is that you have a Number Item involved in a > comparison whose state is NULL or UNDEF.

2 Likes

Thank you Rich, this was a very important piece of information I learned from this post. Since I have all my rules in one file, it was very difficult to figure out going through 400+ rules to identify the “null in default” error.

Thank you again! This also was a HUGE help because this same rule worked fine with OH 2 but errors on OH 3, this narrowed down my issue even more !

Best, Jay