Unreliabale persistence calculation within rule


I am running openHAB 3.3 with openHABian and I have programmed the following rule which is triggered by any change of the water meter. It shall calculate the water consumption for 1 hour, 24 hours and 30 days:

This rule works fine with the 24 hour and the 30 days calculation. However, the 1 h calculation only works correctly if I run the rule manually. Otherwise I regularly get a value of 0 for water consumption last 1 h. I did some tests. It seems that often the automatic calculation returns “null” for water last 1h.

My persistence service is rr4dj with default settings. Any ideas what could be wrong?

Thanks for your help!

Just a guess, your rule triggers when the item state changes. In that event the changed state might be scheduled to be persisted ( your default rrd4j settings should ensure that) and your rule gets scheduled to be run. Both could happen in seperate threads, which one is run run first can not be predicted. So you might run into a situation when the obviously changed item state is not yet persisted but the rule is requesting the change of the last hour. Since the last value is not yet persisted the rule will show a value of 0.

I would question the need of this trigger. Do you really need to run the rule on each and every change of the water meter? I would go for reasonable fixed timesteps

thanks for your help. I did run the rule every five minutes before I changed to the trigger water meter change - same problem.
Can I somehow force the persistence value to be stored?

If I understand the problem correctly the command for persisting the values is already there, it is the inpredictable sequence when threads are run the might show such results. And no, you can enforce a thread to be run before others.
Do you really need the very last ( changed) reading?

Is your problem that a value of 0 is reported or is it the ‘null’ return?

Hello @opus,
the return is “null” in the logging.

Is it really that complex to get the difference between now and 1 hour ago? I thought that should be a pretty straightforward task for openHAB.

Thanks for your help!

Okay, the ‘null’ problem!
That’s probably another story (read Here). My guess would be that you have gaps in your rrd4j database. You could check that with the API explorer, look for the ti eframe which givesyou a ‘null’ return ( note that the API explorer uses the 24 houres as a default timeframe).

I did some further testing (with my means). Unfortunately I am not an expert in this.

I created a script which logged the following output:
Wasserzahlerlesen_Ruckgabewert.historicState(now.minusHours(1), "rrd4j")
I ran it serveal times and it always gave me the correct answer, never “NULL”.

Meanwhile I have found this thread which seems to have (solved?) a similar issue: deltaSince or historicState sometimes returns null on openHAB 3.2 - #12 by rossko57

Thanks again for your support!

IMHO the used (blockly?) code does use .historicState as well!

It seems as if I have found a suitable workaround.

I run the rule every five minutes (starting 2 min. past the hour), in addition to the trigger water meter change. I added a delay of 2 sec. before the rule actions starts. It now seems to get reliable past values for the calculation.