I have a bunch of rules that update rain data.
In principle rain gets added to the total rain fall, then yesterday, week, month quarter, year are updated with deltaSince(START_OF_DAY.minusDays(number of days).
However, the year refuses to be updated.
The test rule:
rule "trial"
when
Time cron "5 0/1 7 ? * * *"
then
val RULE_ID = 01
val START_OF_DAY = ZonedDateTime.now().with(LocalTime.MIDNIGHT)
ar_RainYear.postUpdate(ar_RainTotal.deltaSince(START_OF_DAY.minusDays(365), "rrd4j") as Number)
logInfo(LOG_PREFIX + RULE_ID + ".01",
"ar_RainYear = {}", ar_RainYear.state)
// ar_RainYear = ar_RainYear (Type=NumberItem, State=232.400, Label=Rainfall last 365 days,
// Category=clouds_rain, Groups=[gAcuRite, gPersist_rrd4j])
end
I assume, you don’t have another persistence besides standard rrd4j running?
Depending on what OH-version you started, your rrd4j might be different, but I think standard (also in OH3) is 10 years worth of data. rrd4j slowly “degrades” the granularity over time, so with the standard OH-configuration it should not degrade to more than a day.
If you’re keen on keeping “clear” data, you should use another persistence like SQL DB (MariaDB/MySQL, PostgreSQL), which ootb don’t alter stored information unless you do it yourself. My MariaDB for instance is around 2GB of data from >1200 items over at least 8 years, but I do delete data I deem not worthy like states of lights or similar:
Having said that, I just tried to do the same like you did in your script with the working hours of my solar pump, and my rrd4j and mysql allow for more than a year (I used 2 years here):
2024-11-14 07:57:45.026 [INFO ] [g.openhab.core.model.script.INFO1.01] - 730 days of rrd4j = 4146.0
2024-11-14 07:57:45.030 [INFO ] [g.openhab.core.model.script.INFO1.01] - 730 days of mariadb = 4146.0
Oops, I seem to always forget something: yes, I use rrd4j.
I have to go back in my personal logs, or even OH forum posts. I had a restart many months ago where OH did not write anything held in zram, incl. persistence files. SO I think it is true that I do not have data that is 365 days old in the persistence files.
I figured it out be enabling the the if() and adding an else(), which showed the return value was indeed null.
rule "trial"
when
Time cron "5 0/1 11 ? * * *"
then
val RULE_ID = 01
val START_OF_DAY = ZonedDateTime.now().with(LocalTime.MIDNIGHT)
val DAYS_TO_TEST = 365
if (ar_RainTotal.deltaSince(START_OF_DAY.minusDays(DAYS_TO_TEST), "rrd4j") !== null)
{
ar_RainYear.postUpdate(ar_RainTotal.deltaSince(START_OF_DAY.minusDays(365), "rrd4j") as Number)
logInfo(LOG_PREFIX + RULE_ID + ".01",
"ar_RainYear = {}", ar_RainYear.state)
}
else
{
logInfo(LOG_PREFIX + RULE_ID + ".02",
"ar_RainYear = {}", "Can't go back that far")
}
// ar_RainYear = ar_RainYear (Type=NumberItem, State=232.400, Label=Rainfall last 365 days,
// Category=clouds_rain, Groups=[gAcuRite, gPersist_rrd4j])
end
Because it returned null the yearly value never got updated, but did when specifying a lower number of days.
I think I leave things as they are, as eventually the history will reach >365 days and the thin works again.