Rule to calculate rain amount of the last 12h/day/week/month

Hi Rule-Experts,
I hope you can help on my issue as I have no clue how to start.
I’m using a Z-Wave Rain sensor from Popp to measure the rain. The sensor has multiple values which does not really help in my case. The sensor provides following values:

  • Basic Value - shows if rain is detected
  • Sensor General - counting rain in mm/m² (always adding current measure to last value)
  • Sensor Rain Rate - current rain in mm/h
  • Water meter - counting Liter in m³ (always adding current measure to last value)
  • Battery Level
    My plan was to take the value from the Sensor General and calculate on any change the difference from current to last value. This was the easy part. but now I need to work with some timers to add this value to some values for the rain amount of the last 12h, last day, last week, last month.
    Here I’m lost (only beginner in programming) :slight_smile:
    Rain amount of last 12h, day, week, month should be stored too to create a graph.
    Is there someone who can help?
    THX
    Marcus

Hi Marcus,
As you may have guessed, this is to be achieved by using a persistence service with the help of some rules. First you have to persist (store every changed counter value from your rain gauge in a time series database) and then retrieve the cumulated values for half-day, day, week, etc. with a persistence query, e.g. for the last 12 hours:

<YourStoredItem>.sumSince(now.minusHours(12))

So best is to start reading more about persistence, e.g.:

So the rules are just needed to update your items regularly, because the the math is done by the persistence service and the retrival functions yet. You don’t have to invent the wheel again with some own clever math’s in your rules :wink:

And of course: The persistence database serves the charting functions for example in your sitemap for Basic UI.

My hint: Start simple with rrd4j-persistence and the integrated graphing from Basic UI and the mobile apps. When you’re more familiar with it, you may consider InfluxDB as persistence service in combination with Grafana as charting tool. This give your more charting options and prettier graphs.

There is a great tutorial how to make it work:

But as I said, start simple :wink:

1 Like

I’ll offer slightly different advice.
rrd4j is good for things like temperatures, where you don’t mind a bit of averaging in the distant past, trading off against fixed storage size.

Not so good if your primary interest is an accumulating counter. You won’t be wanting to average that, just store precise periodic reports. Influxdb maybe.
Getting the rainfall for any period is then easy; retrieve start and end readings and do a subtraction.

Something like hourly rates are a good prospect for rrd4j though, and there is no reason you cannot run both.

Agree … at least partially… :wink:
But to be honest, I don’t see a “real” disadvantage of rrd4j regarding the aggregating function in this use case. For a counter, the aggregation function may bring some loss of precision here, but this might not be justifying the hassle with InfluxDB for the first steps with persistence :wink:

THX for your fast reply, I think I have it understood, need to check on the weekend if I can build it :slight_smile:
Best
Marcus

Hi Marcus,

I’m using the Popp Z-Rain, too. As already mentioned here, influxdb and persistence is your friend.
You said you wanted to create a graph. I’m using Grafana for graphing (highly recommended) and if you’re going down that road you can save yourself some trouble creating proxy items that store your calculations:

Influxdb has a function called non-negative difference. You can use this in grafana on the fly to get the amount of rain in a certain time frame. Here for example, I display the non negative difference of the sensor general value per hour as blue bars (the ones left of the yellow line, the rest is prediction).

I know it’s all a bit much for the beginning, but there are good tutorials on the forum and lots of helpful people :slight_smile:
Since you seem interested in graphing and data, grafana will make you very happy!

Hi BobMiles, thanks, that’s what I’m looking for. I was off a long period of time and need to step in again. Could I ask you to she the relevant ITEMS, SIDEMAPS, THINGS files? It is always easier to steel with pride than to reinvent the wheel. :slight_smile: This will be very helpful. THX

Hi Marcus,

once you got grafana and persistence up and running you don’t need anything else.
Item is the sensor_general that you get from the Z-Rain. Then set up a grafana panel with the following query (e.g. For daily rain amount):


You can adjust the time span of the aggregation as you like, time(1h) for hourly rain amount for example.

THX Bob, finally I’ve got Grafana running and got data from influxdb shown. Finally I’ve got the graph as I wish to have it, THX for your support. But please let me ask an additional question on influxdb and how do you handle storage size. Did you store all items in influxdb and how do you clean old data re. size issue on RaspberryPI?

Hi Marcus,

that’s great! Regarding the storage size: I store (“persist”) every item, a lot of them hourly or when they change. Influxdb has a function where it can delete old data automatically which is referred to as the “policy”.
I personally have not yet deleted data despite running it for 2 years now. I can check how big my dB is, but I’m running on an SSD connected to the pi so there’s lots of space.
What’s your setup?