Help with rule, persistence and how sumSince works

Hi!

I am trying to count how many occurrences of an event that has happened in the last couple of days by using persistence (rrd4j), setting an item to 1 every time the occurrence happens and then use the Persistence extension sumSince but I can not get a grip on the result and how it works.

Is there anyone who can explain how this extension works or is there an easier way to count the number of occurences for a specific event during a specified timeperiod?

Thankful for any help.

The rule I am using looks like this

rule "strava activity"
when
	Item runTemp received command
then
	runRegister.postUpdate(1)
	Thread::sleep(60000)
        runRegister.postUpdate(0)
	val runsTemp=runRegister.sumSince(now.minusDays(7))
	runLast7days.postUpdate(runsTemp)	
end

This probably wont work with rrd4j. The problem is rrd4j saves the value once per minute when properly configured. sumSince has no way to tell the difference between a 1 saved because it was time or a 1 because of a change. And there is no way to synchronize your 1 and 0 values (like you appear to be trying to do with the Thread::sleep) with the rrd4j’s timed saving of the value so occasionally you will get two 1’s in your DB when you only want one.

You can minimize the risk of this by removing the Thread::sleep and make sure your rrd4j is configured to save every minute and every change. This will minimize the amount of time that the state remains 1 and minimize the chance that rrd4j will save two values instead of one. However, there will still be rare cases where two 1s will get saved.

To make this work accurately you must use some other database and save only on every change.

In addition to what Rich said, if you are using the default setup of rrd4j, it is setup to hold the data for every minute only for 8 houres. Older data is already consolidated.

1 Like

I thought it was longer. Good to know.

These are the settings:

[consolidation function: AVERAGE, archives (covering time / resolution) 1 (8 hrs / 1 min), 2 (24 hrs / 4 min), 3 (150:16 hrs / 14 min), 4 (30 days / 1 hr), 5 ( 365 days / 12 hrs), 6 (3640 days / 7 days) ]

Thanks for all the replies and all the help, changed to the mysql database on another server that I got and now seems to work axactly as it should.

One hopefully small followup question. In the documentation for persistence it says that it these extensions defaults to the default persistent service set for openhab but that it is possible to use a different persistence service by adding an optional parameter to the end, in the example it says: Lights.persist(“rrd4j”)
I tried this so I could keep existing functionality with rrd4j as default service and only use mysql where necessary by typing:
val runsTemp=runRegister.sumSince(now.minusDays(7))(“mysql”)
but that does not seem to work. Is it possible to use another service than the default service in some way?

afaik it should be

val runsTemp=runRegister.sumSince(now.minusDays(7),"mysql")

Hi again!

When I se it written it seems obvious but for some reason I could not see that last night.

Thank you very much for all help.