Getting higher precision with rrd4j

Hi,

I want to calculate the current power of my meter but my meter only provides the total counter.

So I added persistence to the item “PowerTotal” and want to calculate the current consumption using a rule:

rule "Power Current"
when
	Item PowerTotal changed
then
	var Number secs = 10
	var Number oldValue = PowerTotal.historicState(now.minusSeconds(secs)).state
	var Number currentValue = PowerTotal.state 
	var Number d = currentValue - oldValue
	logInfo(TAG, "old " + oldValue + " current " + currentValue + " delta " + d )
	d = d / secs
	var Number watts = d * 3600 * 1000
	postUpdate(PowerCurrent, watts)
end

Persistence:

Strategies
{
	default = everyChange
}

Items {
	PowerTotal : strategy = everyChange
}

The problem is, that the historic state of 10 secs ago is updated only once a minute. Is there any way to change this? I’ve read about different data stores and I also know how to configure a data store with a precision of 1 secs, but the documentation of the persistence / items lacks information of how to assign an item to a specific data store.

Any help is greatly appreciated.

Create a strategy of once per second or once per ten seconds. But be aware that using your current persistence strategy, if you are only getting historic values from a minute ago it means that it is only being updated once a minute as your strategy is to save every change. This isn’t a persistence problem, it is a sensor reporting problem.

You basically already have it figured out. If you, for example, create a db4o.persist file with the same contents as you have quoted above, every change made to PowerTotal will be saved to db4o. If you only want it saved to db4o remove it from rrd4j.persist.

If you only want to save PowerTotal once a second and everything else set to everyChange your persistence would be:

Strategies
{
    everySecond: "0 * * * * ?"
    default = everyChange
}
Items {
    PowerTotal : everySecond
    * : strategy = everyChange
}

In your rules you can specify where the historic data is coming from by passing the name of the persistence service you want.

For example, PowerTotal.historicState(now.minusSeconds(secs), "db4o").

Sorry for the confusion, my sensor reports the total power about once a second so with “everyChange” I expected values to be written every second.

Current log output:

2015-11-16 17:41:14.472 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1197
2015-11-16 17:41:14.478 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1197 delta0.0042166666651493869721889495849609375
2015-11-16 17:41:14.492 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1518.01200000
2015-11-16 17:41:15.480 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1198
2015-11-16 17:41:15.508 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1198 delta0.0043166666651493869721889495849609375
2015-11-16 17:41:15.515 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1554.01200000
2015-11-16 17:41:16.480 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1199
2015-11-16 17:41:16.492 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1199 delta0.0044166666651493869721889495849609375
2015-11-16 17:41:16.496 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1590.01200000
2015-11-16 17:41:17.492 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1200
2015-11-16 17:41:17.499 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1200 delta0.0045166666651493869721889495849609375
2015-11-16 17:41:17.512 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1626.01200000
2015-11-16 17:41:18.490 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1201
2015-11-16 17:41:18.507 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1201 delta0.0046166666651493869721889495849609375
2015-11-16 17:41:18.518 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1662.01200000
2015-11-16 17:41:19.497 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1202
2015-11-16 17:41:19.526 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1202 delta0.0047166666651493869721889495849609375
2015-11-16 17:41:19.542 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1698.01200000
2015-11-16 17:41:20.500 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1203
2015-11-16 17:41:20.592 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1203 delta0.0048166666651493869721889495849609375
2015-11-16 17:41:20.607 [INFO ] [runtime.busevents             ] - PowerCurrent state updated to 1734.01200000
2015-11-16 17:41:21.509 [INFO ] [runtime.busevents             ] - PowerTotal state updated to 28590.1204
2015-11-16 17:41:21.528 [INFO ] [enhab.model.script.strom.rules] - old 28590.1154833333348506130278110504150390625 current 28590.1204 delta0.0049166666651493869721889495849609375

As you can see, although data is written at “every change”, the “old” value is not updated somehow.

It is possible rrd4j doesn’t save values more than once per minute. I know it has problems with saving values that change less frequently than once per minute. I’d try a different persistence provider and see what happens.