Accumulated Rainfall from Dary Sky Precip Intensity

I created an Accumulated rainfall sensor based on Dark Sky precipitation intensity. I have used it for a week and it is proportional to actual rainfall. I suggest if you need it represent actually rainfall, is to calibrate its reading to actually rainfall using a ratio.

I use it to know when to turn on a drain pump so I only need to know the level when the pump should go on, not the real rainfall measurement. I reset the measurement when the pump drains but you could change it to end of day or something like that.

  - platform: template
    sensors:
      accumulated_rainfall:
        friendly_name: Accumulated Rainfall
        entity_id: sensor.time
        value_template: >-
          {%- if is_state('switch.pool_pump', 'on')  -%}
           0.0
          {% elif  is_state('sensor.accumulated_rainfall', 'unknown')  -%}
           0.0
          {% else %}
            {{ states('sensor.accumulated_rainfall')| float  + states('sensor.dark_sky_precip_intensity') | float  }}
          {% endif %}   
        unit_of_measurement: 'mm'
1 Like

This new component can probably replace this sensor.

1 Like

Hey Rob, I can’t really map my mind around why the Riemann sum is the most appropriate method for this sensor? Can you explain?

I think Riemann sum will add the Dark Sky precipitation intensity values over time which is the same as my sensor.

I would think this is proportional to accumulated rain fall.

sensor:
  - platform: integration
    source:  sensor.dark_sky_precip_intensity

Thanks Rob – Reading through the Python code that makes sense, I initially was reading through the Wikipedia article on Reimann sums and wasn’t getting it.

But reading through the source of the sensor this looks straight forward:

try:
                # integration as the Riemann integral of previous measures.
                area = 0
                elapsed_time = (new_state.last_updated
                                - old_state.last_updated).total_seconds()

                if self._method == TRAPEZOIDAL_METHOD:
                    area = (Decimal(new_state.state)
                            + Decimal(old_state.state))*Decimal(elapsed_time)/2
                elif self._method == LEFT_METHOD:
                    area = Decimal(old_state.state)*Decimal(elapsed_time)
                elif self._method == RIGHT_METHOD:
                    area = Decimal(new_state.state)*Decimal(elapsed_time)

                integral = area / (self._unit_prefix * self._unit_time)
                assert isinstance(integral, Decimal)

I’ve setup both the integration sensor and your template sensor and will monitor them for awhile. Thank you!

FYI, the integration sensor never seems to reset it’s value, it’s just a running sum total of all the rainfall since the sensor was setup.

So also the template sensor example is not correct. It triggers off of sensor.time so it will sum up the value whenever that entity_id updates.

The template sensor really needs to only be evaluated when sensor.dark_sky_precip_intensity is updated, but then also needs to reset at midnight — AND – it gets more complicated because by default sensor.dark_sky_precip_intensity updates every five minutes with the projected “rate” of rainfall in inches for the next hour.

If you simply sum up these values you will again have a grossly exaggerated rain fall total.

Here is my sample of data from today:

MariaDB [homeassistant]> select state,created from states where entity_id = 'sensor.dark_sky_precip_intensity' and created BETWEEN '2019-05-07 06:00:00' AND '2019-05-08 06:00:00';
+--------+---------------------+
| state  | created             |
+--------+---------------------+
| 0.002  | 2019-05-07 12:44:54 |
| 0.003  | 2019-05-07 12:49:55 |
| 0.002  | 2019-05-07 12:59:57 |
| 0.003  | 2019-05-07 13:09:58 |
| 0      | 2019-05-07 13:19:59 |
| 0.002  | 2019-05-07 13:35:02 |
| 0.003  | 2019-05-07 13:45:03 |
| 0.002  | 2019-05-07 14:10:06 |
| 0.003  | 2019-05-07 14:15:07 |
| 0      | 2019-05-07 14:25:08 |
| 0.003  | 2019-05-07 14:30:09 |
| 0.005  | 2019-05-07 14:35:10 |
| 0.003  | 2019-05-07 14:45:11 |
| 0.0032 | 2019-05-07 14:55:12 |
| 0.006  | 2019-05-07 15:00:13 |
| 0.003  | 2019-05-07 15:10:14 |
| 0      | 2019-05-07 15:15:15 |
| 0.002  | 2019-05-07 15:40:18 |
| 0.003  | 2019-05-07 15:55:19 |
| 0.006  | 2019-05-07 16:05:20 |
| 0.016  | 2019-05-07 16:15:21 |
| 0.008  | 2019-05-07 16:25:22 |
| 0.003  | 2019-05-07 16:35:23 |
| 0.004  | 2019-05-07 16:50:25 |
| 0.007  | 2019-05-07 16:55:26 |
| 0.006  | 2019-05-07 17:00:27 |
| 0.0115 | 2019-05-07 17:15:29 |
| 0.023  | 2019-05-07 17:20:30 |
| 0.018  | 2019-05-07 17:30:31 |
| 0.02   | 2019-05-07 17:35:32 |
| 0.015  | 2019-05-07 17:40:33 |
| 0.012  | 2019-05-07 17:45:34 |
| 0.024  | 2019-05-07 18:00:36 |
| 0.0264 | 2019-05-07 18:05:37 |
| 0.026  | 2019-05-07 18:15:38 |
| 0.0187 | 2019-05-07 18:25:39 |
| 0.019  | 2019-05-07 18:30:40 |
| 0.012  | 2019-05-07 18:35:41 |
| 0.011  | 2019-05-07 18:40:42 |
| 0.008  | 2019-05-07 18:45:43 |
| 0.005  | 2019-05-07 18:55:43 |
| 0.003  | 2019-05-07 19:00:44 |
| 0.0015 | 2019-05-07 19:10:45 |
+--------+---------------------+
43 rows in set (0.006 sec)

MariaDB [homeassistant]> select SUM(state) from states where entity_id = 'sensor.dark_sky_precip_intensity' and created BETWEEN '2019-05-07 06:00:00' AND '2019-05-08 06:00:00';
+--------------------+
| SUM(state)         |
+--------------------+
| 0.3523000000000001 |
+--------------------+
1 row in set (0.011 sec)

What is ideally needed is to somehow cherry pick one reading per hour and add that to the running total for the day.

EDIT: Here is the data from the template sensor example for the same time period: https://hastebin.com/zobobevilo.rb

If you take the dark_sky_precip_intensity every minute for an hour and then divide the total by 60, should it give you the average rain per hour expected for the next hour?

Do you think the total of the template sensor is proportional to the actual rainfall?

For example, in your hastbin data, I note the sensor increased by 1 point from 12:45 to 17:19 and then another 1 point till 18:12. Was the actual rain fall during those times about equal?

1 Like