Help with statistics sensor: calculate increase in the last 48 hours

Hi I’m trying to calculate the total rain fall (= increase or change) in the last 48 hours.


- platform: statistics

    name: "Rainfall last 48 Hrs"

    entity_id: sensor.rainfall_total

    state_characteristic: change

    max_age:

      hours: 48

Sensor works not as expected: see in graphs it reports only arround 5 mm, while total rain fall increased from 55 to 70 in last 36 hours only. What am I doing wrong?


My guess is that you need to increase the sampling_size:

sampling_size integer (optional, default: 20)
Maximum number of source sensor measurements stored. Be sure to choose a reasonably high number if the limit should be driven by max_age instead.

Correct. The sensor must be configured precisely to your needs but the statistics documentation explains it well: Statistics - Home Assistant

And just to add to this: It is on my todo list to improve the statistics component in various ways to become easier and more intuitive to use. Cheers.

1 Like

Another thought: The “change” characteristic will produce wrong results as soon as your sensor.rainfall_total resets to 0. You have to be aware of this.

Please check whether your weather station produces absolute rain measurements (“rain since last measurement”) which you can sum up using the “total” characteristic.

Aha, I increased the sampling size to be higher than the maximum number of samples that I expect (500 in my case). Will report back in 48 hours if that works :slight_smile:

Although I checked the documentation before posting, I didn’t think the sampling size was relevant. I thought it only needed to extract two samples from the HA log (the current, and the one from 48 hours ago in my case), but I now understand the integration stores its own history and needs enough samples to cover the whole 48 hours of events.

It would make sense the “max_age” forces enough samples to be taken in my case, but I suppose there are other scenario’s in which that it is desired to work as it is working now.

I noticed as well that the sensor resets to 0 when HA restarts, but I suppose that is a known bug.

it is a cumulative sensor; it doesn’t reset to zero so that’s covered :slight_smile: but thanks for the warning.

It would make sense the “max_age” forces enough samples to be taken in my case, but I suppose there are other scenario’s in which that it is desired to work as it is working now.

No you are absolutely correct. The current implementation is weird in it’s way. We have the two limits for sample size and age. The better strategy should be to require only one of them and to assume the other as “infinite”. If both limits are given explicitly, that’s also fine.

A tighter integration with the recorder, e.g. to immediately calculate your 48h rain difference is btw also on the todo list.

I noticed as well that the sensor resets to 0 when HA restarts, but I suppose that is a known bug.

No it’s not. A “total” value is never absolute. What would it be total against? The beginning of time? :slight_smile:

but thanks for the warning.

Still, you sparked the idea that we should probably add characteristics, which sum up increasing values only. That would solve your use case even in the case of a total=0 reset.

Thanks for the reply and your efforts to improve this integration!

I may not have been clear, what I meant was

I noticed as well that the sensor the history statistic sensor resets to 0 when HA restarts, but I suppose that is a known bug.

See image below there is a restart and the “Rainfall last 48 hours” history statistic resets to 0, while the “total rain fall” is keeping its value during the restart as expected.

By the way, issue solved with increasing sampling size :slight_smile:

One other issue related to this: if no change during 48 hours, the sensor reads “unkown” and not 0. For this reason my automation was no longer working (if rainfall last 48 hours < 2, start watering). I think it makes more sense for the sensor to return 0 if there no no change, is there a way to enforce this?

unknown

No but you are not the first to ask about this…

All statistic characteristics return “unknown” when there are no samples in memory. Makes sense mathematically, but clashes in some use cases. Hard to calculate the mean of “nothing” and who can say it’s exactly 0?

I have to admit though, mean is especially difficult but characteristics like change could return 0 for a sample size of “nothing”. How would you like this to work? Happy to discuss.

Until resolved, maybe this will help?

automation:
  - trigger:
      - platform: numeric_state
        entity_id: sensor.rainfall_last_48h
        below: 2
      - platform: state
        entity_id: sensor.rainfall_last_48h
        to: "unknown"
    action:
1 Like