Get minimum of last 24h - but not with rrd4j

Hi,
i currently use rrd4j to persist all my data vom from my heatpump. I want to see the minimum temperature of one item in the last 24h. rrd4j is not the right persistence service for this, because i only get original values for 7h, after 7h i only get average values. I don´t want to change rrd4j settings, because in this case i would lose all my saved data.

So i´m looking for another persistence service, where i can store the minimum value of an item with a rule. I dont´want to install a database like mysql, i want to have a simple persistence service like rrd4j, where all data is stored inside openhab without the need of external database-servers.

Which persistence would be the best for my use case?

I want to store mimimum value of latest 24h and the timestamp of this minimum value. I only want to persist 2 temperature items. Can i use mapdb? I already use this to persist some items.

You might be able to make it work with MapDB and a Rule. You will need to make sure that your two temperature Items are not already being persisted by MapDB for any reason. Then in your rule you can do something along the lines of:

    if(MyTempItem.lastUpdate.before(now.minusHours(24).millis) || (MyTempItem.state as DecimalType) < MyTempItem.previousState("mapdb")) MyTempItem.persist("mapdb")

In words the above basically says if the lastUpdate was more than 24 hours ago then this new value is the minimum on the past 24 hours. Or if the current State is less than the previousState as stored in MapDB, this value is the new minimum.

However, if you are already storing these Items in MapDB for some other reason (e.g. restoreOnStartup), this won’t work.

You can use db4o for these Items and just use the minSince method. db4o saves all the values so you don’t have to worry about them being averaged up as they get old. Though db4o will grow forever, though it will take many many years before the size becomes a problem, if it ever does become a problem.

Thank you! Is there a way to remove old values from the persistence? Maybe delete all values which are older then 1 week?

This is not working…

I get this error:

This is my rule:

rule "Update Sole-VL Temperatur Min-Werte tgl ohne Rundung"
    when
        Item HeatPump_Temperature_6 received update
    then
	HeatPump_Sole_VL_Min_ungerundet.postUpdate(HeatPump_Temperature_6.state)
        if(HeatPump_Sole_VL_Min_ungerundet.lastUpdate.before(now.minusHours(24).millis) || (HeatPump_Sole_VL_Min_ungerundet.state as DecimalType) < HeatPump_Sole_VL_Min_ungerundet.previousState("mapdb")) HeatPump_Sole_VL_Min_ungerundet.persist("mapdb")
end

In mapDB the two items in this rule are not persisted yet. Do i have to edit mapdb.persistence and add the two items?

Do i get timestamp of last min value with this code or do i have to add some more code?

Could it be, because of there is nothing “before” saved in the mapdb - so the rule couldn´t compare the new value with the value “before”?

Is there nobody who can help me please?

I’d change the already used persistence. Since you ruled that out, easy solutions won’the work.
Without changing your rrd4j setting ( wich is most probably the default one) the values for the last 24 houres are averages over 14 minutes, consider yourself if such an average would be usable or not.

All other solutions would need a second persistence service running.

But then i can only get last 24h - then i want to have last 7 days - next i want to have another time-step…

I already use mapdb for persisting some items. So why not use it for this too?

If i want to store the minimum value of last 24h i can edit the rule, if i want to persist the minimal value of the last 7 days, i only have to edit the rule…

So whats wrong with the rule i tested above?

@rlkoshak

Do you know, why the rule isn´t working?

There is currently no way to do this inside OH. This isn’t a problem for MapDB or rrd4j since both of these manage that on their own (MapDB because it only saves the most recent value and rrd4j because of its data compression). For other DBs you need to find and use an external tool to delete older values, or periodically completely wipe out the DB entirely and start over.

What is your default persistence as configured in openhab.cfg? If it isn’t where HeatPump_Sole_VL_Min_ungerundt saved you need to specify where it is saved in the call to lastUpdate, e.g. lastUpdate(“mapdb”).

I am not sure. You might need to add it to the .persist file using a strategy that causes the value not to be saved unless you explicitly save it, as you are doing in that second line.

If there is nothing for this Item in the default persistence store the call to lastUpdate returns null. You might need to test for null to get past this very initial state where the Item is not yet in the database.

I switched to db4o. I think this is much easier.

So here is my rule - i used this with rrd4j before…

rule "Update Sole-VL Temperatur Min-Werte tgl ohne Rundung"
    when
        Item HeatPump_Temperature_6 received update
    then
	var Number Min
        var String tmp
        var SimpleDateFormat df = new SimpleDateFormat( "dd.MM., HH:mm" ) 

        if (HeatPump_Temperature_6.state instanceof DecimalType) {
            Min = (HeatPump_Temperature_6.minimumSince(now.minusDays(1), "db4o").state as DecimalType)
            tmp = (Math::round(Min.floatValue*10.0)/10.0) + " °C (" + df.format(HeatPump_Temperature_6.minimumSince(now.minusDays(1), "db4o").timestamp) + " )"
            postUpdate(HeatPump_Sole_VL_Min_ungerundet, tmp)
        }
end

It is working, but now i only have values from a few minutes, so i can´t control. But thwere is no error any more in the log file.

I got the rule here from the forum some time ago, but i don´t know exactly, what every thing is used to do.

So i want to know: Is this rule ok or should i change something? Especially i don´t know, why there is so much math in the rule? Why is there rounding and *10.0 /10.0 ? Is this to get only values with 1 digit?

I get the values from my heatingpump and there is already only 1 digit, so do i need this?


And another question regarding db4o persistence:

I din´t change default values in openhab.cfg. So there is saving to persistence every 5 seconds. But my heatingpump is providing new values only every minute. db4o.persist is configured “on change”.

So do i have to edit my openhab.cfg to “1 minute” or can i stay with this because it only saves new values to the database when there is really a new value provided from the heatingpump?