Trying to understand total_increasing with statistics (rain over last 3 days)

So I wanted to create a sensor representing how much rain has been received over the last 3 days.
I have a custom integration (Australian BoM by @bremor) that provides a sensor for rain since 9AM, and this gets reset daily.
Sounds like a perfect use for ‘state_class: total_increasing’
I set up another template sensor:

  - name: "Rain total"
    unit_of_measurement: mm
    state_class: "total_increasing"
    state: "{{ states('sensor.sydney_observatory_hill_rain_since_9am') }}"

I use a separate template sensor rather than customising the original sensor since it seemed cleaner.
I then have a sensor:

  - platform: statistics
    name: Recent rain
    entity_id: sensor.rain_total
    state_characteristic: change
    sampling_size: 144
    precision: 1
    max_age:
      days: 3

What I see is that total_increasing will set the statistical history sum to the accumulating amount, but the state gets reset each day. So it appears the statistical sensor is taking the ‘state’ rather than the ‘sum’ of the sensor.rain_total sensor, and so the 3 day calculation is incorrect.
I thought that total_increasing would set the state to the accumulating value?

So now I am quiet confused about how the ‘total_increasing’ state_class is meant to be used.
Is this only for storing in the statistical database (as ‘sum’), and is never available as a ‘state’ value?

My next step is to start looking at the code, but there’s a lot to navigate through, so hence I am asking here.

You have it back to front. Changing the state class does not change the behaviour of the sensor. It is a way of describing the sensor’s behaviour.

https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes

I get that, and originally I had a customize entry to add ‘state_class: total_increasing’ to the original sensor, to describe what this sensor did.
But what I wanted to feed into the statistics sensor is not the state, but the sum (as processed by recorder).
I thought that perhaps using a template sensor with that state_class may process it, but I agree in reading the docs that it’s only the sum that is being accumulated.

So my real question is how to use the state_class ‘total_increasing’ to get the actual accumulated ‘sum’ (as stored by recorder) and use it as an input to the statistics sensor.
In other words, I have an accumulating value that’s reset daily, and I want to use that as an accumulating value that doesn’t get reset (as a state).
Do you see what I mean?

It may be that I need to modify the integration itself and add a new sensor that basically does behave like a ‘total_increasing’ sensor, and happy to do that, but I thought there may be a sensor platform that did this already.

Use a utility meter without a cycle option defined. That will keep counting up, even when the source sensor resets.

2 Likes