NullPointerException using historicState

I’ve had some rules calculating energy consumption running for a long tme, but for some reason they don’t work in 1.8.3. Is there a problem with “now” in 1.8.3? In the designer that part “now.minusDays(1)” is marked with a curly read line and the error message Couldn’t resolve reference to JvmIdentifiableElement ‘now’.

// Get current counter value
var Number current_value = EnergyCounter.state as DecimalType
// Get historic values
try {
    var Number count_24h   = EnergyCounter.historicState(now.minusDays(1)).state as DecimalType
var Number numWh_24h   = (current_value - count_24h)   / 1000
postUpdate(PowerConsumption_24h, numWh_24h)
}
catch (NullPointerException e) {
    logDebug( "FILE", "No history found!" )
}

I’ve seen reports of now being marked as an error in Designer before but it so works when running.

Check your persistence config and if possible check your values are being saved. A null exception when calling histoeicState is usually caused by persistence returning no results.

Thanks, turned out that was actually the case.


When I checked, I can see that persistence stopped working when I swithed to 1.8.3.
Could I have missed something? my persistence configuration is the same as when I was running 1.8.2, just moved them.

I upgraded without incident. I’m using rrd4j and MapDB. You might try deleting your persistence stores and let OH recreate them from scratch.

Finally found out what went wrong when switching to 1.8.3.
In my rrd4j.persist configuration file, I hade references to items no longer existing. When I removed these, the items were stored properly again. So I guess someting related to that has changed since 1.8.2, because I also had the non.existing items in that config.

Odd. Did you see any errors in the log from rrd4j on startup? This might be something worth opening an issue against because having non-existing items in the .persist file should not completely kill persistence.

One thing that I’ve done that lets you all but set and forget your persistence is to use groups. I have:

  • gMyOpenhab - for Items that get exposed to IFTTT through my.openhab
  • gChart - those Items which I create charts on my sitemap
  • gHistory - those Items which I access the historic state in rules
  • gRestore - those Item which I restorOnStartup

In my .persist files I just refer to these groups (e.g. in my mapdb.persist file I have gRestore* : everyChange, restoreOnStartup). Then, as I add, remove, or change how I’m using an Item I only need change its group membership and never have to touch the .persist file. Furthermore it lets me keep Item behavior information defined with the Item rather than scattered across .items and .persist files.

I’m certain this is why I didn’t see this error when I upgraded.