Item receives update but is null

Hi There,

I’m fighting with a rule that won’t work.

Because it didn’t work I’ve disabled everything and add a simple log output. But even this log output fails.

2017-06-16 18:55:29.780 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Energy chart PV Production': Could not invoke method: org.openhab.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

The strange thing here is, that the rule is only triggered if there is an update on the item, but the message tells mit, that the item is null

rule "Energy chart PV Production"
when
        Item pv_0281 received update
then
        //var Number tmp
        //var Number stopValue

        //stopValue =  pv_0281.maximumSince(now.toDateMidnight).state
        //startValue = pv_0281.minimumSince(now.toDateMidnight).state

        logInfo("TEST", pv_0281)
        //tmp = stopValue - startValue
        //postUpdate(PvEnergyProductionToday, tmp)
end
Number pv_0281                    "PV Anlage Zähler [%.0f kWh]"    	  (Energy)
2017-06-16 18:56:45 - pv_0281 state updated to 28656.1178

Any idea what went wrong here?

Bests
Pascal

Try calling logInfo("TEST", pv_0281.toString)

With toString I’m getting this output (don’t mind the different item):

2017-06-16 22:58:50.665 [INFO ] [org.openhab.model.script.TEST ] - evs_0181 (Type=NumberItem, State=4405.2249)

but when calling

logInfo(“TEST”, pv_0281.state)

it leads to

2017-06-16 23:01:16.715 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Energy chart EVS Consumption': Could not invoke method: org.openhab.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

It seems to be having difficulty with adding the toString itself. Try:

pv_0281.state.toString

Are there other rules in the same rules file? Maybe some typos before the rule? Do you use the Smarthome Designer?

Here is the whole rule. I’m writing the rules only using a editor - I don’t wanna use the Smarthome Designer. It’s one of over 20 rules but the only one that didn’t work and I don’t have any idea why:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "Energy chart EVS Consumption"
when
        Item evs_0181 received update
then
//      var Number tmp
//      var Number stopValue
logInfo("TEST", evs_0181.state.toString)
//      stopValue =  evs_0181.maximumSince(now.toDateMidnight).state
//      startValue = evs_0181.minimumSince(now.toDateMidnight).state

//      tmp = stopValue - startValue
//      postUpdate(EvsEnergyConsumptionToday, tmp)
end

rule "Energy chart PV Production"
when
        Item pv_0281 received update
then
//      var Number tmp
//      var Number stopValue
logInfo("TEST", pv_0281.state.toString) 
//      stopValue =  pv_0281.maximumSince(now.toDateMidnight).state
//      startValue = pv_0281.minimumSince(now.toDateMidnight).state               

//      tmp = stopValue - startValue
//      logInfo("TEST", tmp)
//      postUpdate(PvEnergyProductionToday, tmp)
end

@rikoshak: As you can see, state.toString works. But how will this help?

Are you using OH2 ? You should not have those imports, perhaps you picked them up from following some OH1 example

It is a diagnostic step. Now you know that your Item has a sensible state, i.e. that is not null at all.
Now you can another step to find out what is the null being complained about.
That next step, now that you know how to use logInfo, might be to logInfo() out those .maximumSince values you are using.
You do have persistence running on that Item, yes?

1 Like

No, it’s really a OH1 installation. I didn’t find the time for migration.

But thanks to your help I’m now having a working version:

rule "Energy chart PV Production"
when
        Item pv_0281 received update
then
        var Number result
        var Number stopValue
        var Number startValue

        stopValue =  pv_0281.maximumSince(now.toDateMidnight).state
        startValue = pv_0281.minimumSince(now.toDateMidnight).state


        result = stopValue - startValue
        postUpdate(PvEnergyProductionToday, result)
end

That is too bad because it is frankly the ONLY way to discover subtle syntax errors. When a rule looks right but will not run opening it in Designer should be your first thing to try when trying to solve the problem.

No one is asking you to work day by day in it but refusing to even use it for syntax checking when you have a problem is shooting yourself in the foot.

It shows definitively that your rule is triggering and that your Item is valid and has a state.