Get 5 minutes Average from rrd4j

Hi, from my powermeter i get the power consumed in [Watt] and the Energy-Counter in [kWh].

It is stored in my rrd4j persistence every minute.

I can calculate the power average of the last 5 mins with this code:

sdm_630_Power_Avrg_5min.postUpdate(sdm_630_Power.averageSince(now.minusMinutes(5), "rrd4j").toString)

But i only have values inside my rrd4j persistence from every minute. So every peak between this stored values are not saved anywhere.

Now i want to use the energy counter to get the whole energy used - i have the value from 5 mins ago and the value from now - so i can calculate the real average from the last 5 mins.

Can anybody tell me, how i can get this done with my rrd4j data?

Second question: How long will i get “real” values out of my rrd4j-database (standard, no settings changed)? At some time it will get rounded? When i store absolute values from my counter, then it is not good, when this is rounded…

You have to store data in rrd4j at least once every minute. That doesn’t mean you can’t also use everyChange or everyUpdate in addition to every minute.

@opus can confirm but I think it’s somewhere between one hour and one day.

At some point the data gets replaced with averages. For example, instead of every minute being stored, it will average ten minutes of data and replace the ten entries with the one average.

It’s all in the Documentation, the default datasource holds the values for every minute for eigth houres.

You could setup a custom datasource that does NOT consolidate any value, just use a single archive that holds ther data for each minute and select the total number of values you want to keep (rrd4j does allways presist for a predefined time! The default datasource for 10 years.).

1 Like

In the docs i saw, only everyminute to visualize the data in a chart.

I made dome tests with 10secs, but in the charts i got only new points every minute.

Ok, so o can use my average calculation, i only want to calculate max of 1 hour.

Any hint how i can calculate the average with the absolute counter values?

Never done it myself, however using the .historicState (now.minusMinutes (5)) as documented Here should do the trick to get a value,. Getting two and subtracting them shouldn’t be that hard.

Thanks, now i got it:

	var Number counter_5min = (sdm_630_Energy.state as DecimalType - sdm_630_Energy.historicState(now.minusMinutes(5)).state as DecimalType)
	var Number counter_5min_average = counter_5min * 12

But the new value is significantly differnet to my value from the real-time value average.

Is there a way to get the timestamps of the historic values also? So i can see, if i use the right values.


I made some loggings - i have to use historic state 4 minutes ago to get the value from 5min ago…

I made some calculations:

now - 1min ago
1min ago - 2min ago
2min ago - 3min ago

The first values is double of all the others…

And another question: Can i display a chart with values from every 10 seconds? I only get charts with changes every minute. Even if i have changed the persistence to save values at every 10 secs.

The openhab chart, not the grafana one…

Are you sure that rrd4j really does keep those values? Frankly I am not sure it would do…

I don´t know, @rlkoshak mentioned in his answer, that i can do it with faster intervals…

But i had no success with this.

So i don´t know if the limitation is in the charts or in the persistence.

I made some more loggings:

i calculated:
now - 1min ago
now - 30sec ago

Most time i get the same values, but sometimes i get half the value (seems that this is the right one).

–> now - 1min ago gives me ===> value of 2mins
–> now - 30sec ago gives me ==> most time value of 2mins — but sometimes value of 1min

But i saw that i can not use this in my way. I know calculate the new average at every new value from the powermeter (every 10secs). And every 40-50 secs i get only average with 4 mins * 12 - so it is to small.
When i have 5 mins * 12 i get the average from 1 hour.
This is no problem in the calculation with


But when i do the calculation on my own with absolute counter-values, it is an error.

Maybe i have to change the rule to start every minute (cronjob) instead of every item-change (every 10secs).

Comment for clarity.
All the persistence services behave as follows -
If you ask for historicState() for some instant in time, it is most likely that the data does not exist for that particular millisecond.
The persistence service therefore returns the “next oldest” data, the record from earlier than your target instant, on the assumption that data was still valid at the target instant.

Example - persisting data every hour, on the hour.
Request historicState() for 02:10, you’ll get 02:00 data
Request historicState() for 02:59, you’ll get 02:00 data

Example - persisting data every minute
Request historicState() for “now”, you’ll get data from somewhere up to a minute ago.
Request historicState() for “now - 1 min”, you’ll get data from somewhere between a minute and two minutes ago.

Yes, of course - this is in the docs.

You’re right, it will probably help you understand better what is going on.

val myRecord = myItem.historicState(someInstant,"rrd4j")
if (myRecord !== null) {
   logInfo("test", + "data " + myRecord.state.toString + " at " + myRecord.getTimestamp.toString)
} else {
   logInfo("test", + " no data")

For dealing with ever-increasing counters (like a kWh meter) you might also find minimumSince() to be a useful tool. This actually works better where you have “gaps” in the time sequence in an rrd4j database. Found that out the hard way.

Actually what I was saying was to use everyMinute and everyUpdate, not speeding up the intervals.

I have tried to persist data in 10 second steps, although openHAB is reporting to be doing that (log:level set to DEBUG for rrd4j), I can’t find such values.
When setting a every10Second strategy openHAB will start to be reporting that, however looking into the database with the REST API does not reveal any stored value in between to “normal” minute steps.
Even changing the datasource for a step of 10 seconds does give a different result.

I can’t say if the RRD-Inspector would show different results since I couldn’t get one to recognise an .rrd as a valid source. That used to work, :face_with_raised_eyebrow:

So the problem is on the rrd4j side? The openhab chart engine would be able to display values in 10sec steps?

I suspect you can persist as often as you like, but that rrd4j only holds one-a-minute wit standard config.
rrd4j is not really the best choice if you are looking for this kind of precision.

What is your purpose here, are you looking for brief micro changes in power, like a bird shadow falling over a solar panel?

1 Like

Just scanned through the OH3 code for rrd4j and found:

// add default configurations

        RrdDefConfig defaultNumeric = new RrdDefConfig(DEFAULT_NUMERIC);
        // use 10 seconds as a step size for numeric values and allow a 10 minute silence between updates

Which sounds as if a 10 second! step is set as default. I will give it try on my test-system, although I did set something like that on a custom setup with no effect.

It is perhaps worth reminding that you have no control over what steps the sitemap Chart widget uses, you can only set the overall span e.g. one hour.
It is also the Chart widget that insists on one-minute rrd4j data. No idea how it might work otherwise.

If you need more flexibility then you will need to use another tool like Grafana.

Tested it on OH3, there are values stored at 10 second intervalls (by default, even for items set to a everyMinute strategy??). I am presently not able to understand the stored values, since they seem to be calculated somehow. Thinking,searching… :thinking:

BTW: The default coniguration as posted partly above IMHO are not coded correctly, posted a change request.