Calculate statistics on item-values

I have this item:

Number:Dimensionless       TibberAPICurrentTotal

I try to evaluate an average price for last week like this:

rule "tibber högt pris"
when
    Item TibberAPICurrentTotal changed or Item Sovrum_4__Kontor__Ljusfunktion_Tak_Status changed from OFF to ON
then
    var snittprisVecka
    snittprisVecka = TibberAPICurrentTotal.averageSince(now.minusDays(7))
    if ((TibberAPICurrentTotal.state as Number) > 1) {
        sendCommand(L_ge_Tibber_h_gt_pris_w,1)
        logInfo ("Debug", "tibber pris (" + TibberAPICurrentTotal.state + ") över snittet (" + snittprisVecka + ")")
    } else {
        sendCommand(L_ge_Tibber_h_gt_pris_w,2)
        logInfo ("Debug", "tibber pris (" + TibberAPICurrentTotal.state + ") över snittet (" + snittprisVecka + ")")
    }
end

but the result of TibberAPICurrentTotal.averageSince(now.minusDays(7)) is null.

Log shows:

2021-11-12 09:33:16.297 [INFO ] [org.openhab.core.model.script.Debug ] - tibber pris (1.2448) över snittet (null)

I refer to this section about possible calculations made on persisted items: Persistence | openHAB

So do you have persistence set up on this item?
Do you have at least seven days worth if data stories for this item?

Thanks for replying.

I have this settings in rr4dj.persist

Strategies {
        everyHour : "0 0 * * * ?"
        everyDay  : "0 0 0 * * ?"

        // if no strategy is specified for an Item entry below, the default list will be used
       default = everyChange
}

/*
 * Each line in this section defines for which Item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * Item (excl. the group Item itself).
 */
Items {
        * : strategy = everyChange, restoreOnStartup

}

Has been so for years, and I have logs for other items.

The tibber-items file is fairly new (about 10 days), so I should have enough days. However looking at the UI on the items, I can “Analize” other items, but not this tibber-item, no data is shown in the graph.

Is it something with a new item-file, does OH need to restart to “fetch” new items?

I’m not sure you can expect sensible charting from an rrd4j database with default configuration, when you don’t have a regular time-based persist strategy.

I’m surprised that has ever worked. rrd4j requires a once a minute strategy or else it can’t generate charts and most of the persistence calls will fail too.

I can’t explain why you’re other items work and this one does not but I can say that that config for rrd4j shouldn’t work for any items. Maybe the items that are working change often enough that this misconfiguration is hidden.

Oh,

Do you have a suggestion for the setup of persistence. I think I setup persistence at some point to have the restore of item status after restarting OH.
I see that my file is more or less a copy of the example from docs.
I haven’t used historical data so far

Skickat från min iPhone

Sorry, I don’t expect you to do the job!

I am reding up on persistance and rrd4j in the documentation now.

Try using influxdb instead of rrd4j?

I have no preferences regarding type of database. I run OH on RPI and no external storage.

And I have to admit also a shortage of knowledge about the persistance.

How would I benefit from using influxdb instead?

My understanding is rrd4j is a time-based storage format for charting. It requires that each data point is recorded at a uniform (equal) interval e.g. every X seconds. Furthermore, because it is a “fixed size” database, it would remove and summarise older data so that they would have less “density”. The older it gets the less dense. It was originally meant to store charting data for rrdtool. See RRDtool - rrd-beginners

In contrast, influxdb is a more traditional storage, what you put in it will stay in there. It does have a retention policy that you can configure, e.g. keep everything or delete anything older than 1 year, etc.

For what you seem to need, influxdb is far more appropriate, imo.

This doc actually explains the granularity in rrd4j better: rrd4j - Persistence Services | openHAB

Thank you @rlkoshak , @rossko57 and @JimT .
(I think) I understand the rrd4j better now. I am fine with archiving older data with less granularity, since I don’t use the older data, not yet anyway.

Item TibberAPICurrentTotal updates once every hour (the binding fetch data from API) while other data updates more often (modbus-binding polling local data consistently)

Probably I have had so many changes per time unit for the other values I randomly checked so I haven’t noticed the lack of data.

If I understand this correct I need to store data I want to use the statistic functionality on at least every minute.

I try this setup now, and I now get the average for TibberAPICurrentTotal, so that works!

  • I want to restore all items on startup.
  • If I in the future need other data to chart or calculate on I add those items to the everyMinute-strategy.
Strategies {
        everyMinute : "0 * * * * ?"
        everyHour   : "0 0 * * * ?"
        everyDay    : "0 0 0 * * ?"

        // if no strategy is specified for an Item entry below, the default list will be used
       default = everyChange
}

/*
 * Each line in this section defines for which Item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * Item (excl. the group Item itself).
 */
Items {
        * : strategy = restoreOnStartup
        TibberAPICurrentTotal : strategy = everyMinute

}

Have I understood the persist-settings would you say?

rrd4j cannot handle all Item types.
The Mapdb persistence service is tailor-made for that job.

As @rossko57 said, Mapdb is a better choice fore restoreOnStartup. It can save all data types whereas rrd4j can only save numerical data.

You can have more than one persistence installed at a time and I believe that MapDB, by default, will restoreOnStartup all your Items. So I’d remove the restoreOnStartup strategy from rrd4j and configure rrd4j to store all your Items once per minute and on every change for your charting.

To have a seamless integration with MainUI, configure rrd4j as the default persistence under Settings.