# Calculate total rain fallen in last # hours

When creating automations for irrigation systems, the amount of rain fallen with the last # hours needs to be known (where # refers to the amount of past hours you want to consider).

The current rainfall (the amount of water that is right now falling from the sky) is provided as kind of a flow rate. This is similar to the amount of water running through a pipe, which will be given in a volume unit per time e.g. m³/s.
Flow rate for rain is given as mm/h usually, because the area the rain falls onto is not necessarly known. If you wanted to know the amount of water falling onto one mm², you would multiply the rain rate by that square millimeter and end up with mm³/h, a volume unit per time.

This said, we now calculate the total amount of mm rain in Home Assistant, that has fallen over a given period of time from now on backwards.

You need:
An entity that provides you with the current rain rate in mm/h or something similar.
You could use the rain entity from Open Weather Map (I will use it in the guide) or the current rain value from your personal wheather station. It should look like this in the history:

What we want to know, is the area under the graph within the last # hours. (marked in red)

Target:
Calculate a rolling integral over a given amount of time based on the above mentioned entitiy.

Solution:
Create an “Integration - Riemann sum integral sensor” helper.

Name: “Integration Rain Fallen”
Input Sensor: OpenWeatherMap Rain
Integration Method: Left Riemann sum
Precision: 2
Metric prefix: none
Time unit: Hours

Make sure, time unit is set to the same time your rain rate uses (e.g. set time unit to Hours if rain rate is given in mm/h).
Furthermore, the Left Riemann sum is what will provide the most accurate results (see wikipedia and check the history graph of rain sensor above to understand why).

Now you have a sensor, that calculates the area under the rain rate curve, which basically is what we are looking for already, a rolling integral. The only problem is, that this helper will go back in time forever and not stop at our left red line from above.

To only take the data of the last # hours into account (e.g. until the left red line from above), we will be using an additional sensor based on the statistics platform. As the integration helpers value will always increase (new rain just adds to the already exisiting amount), we only need the difference between the value right now and the one # hours ago. The statisctics platform is capable of doing exactly that.
``````sensor: