Sum differences sensor throwing wrong values

I have a problem with my statistics sensor. I tried few ways how to resolve it, but so far, I was unsuccessful. I have a sensor that holds total precipitation. I feed that number into input_number and then have a template sensor that adds attribute, otherwise status would go to unknown if not changed for some time.

template:
- sensor:
  - name: "Rain 24h Input Number"
    unique_id: "Rain 24h Input Number"
    state_class: "measurement"
    unit_of_measurement: "mm"
    state: >
      {{ states('input_number.rain_preserved_state') }}
    attributes:
      hour_counter: "{{ now().hour }}"

Lastly, I use sum_differences sensor to calculate amount of rain in last 24 hours:

- platform: statistics
  name: "Rain Over Last 24 Hours"
  unique_id: "Rain Over Last 24 Hours"
  entity_id: sensor.rain_24h_input_number
  state_characteristic: sum_differences
  max_age:
    hours: 24

My problem is if I restart HA (or something else) and statistics sensor goes unavailable. If rain is 0,0mm, it is OK. However if it rains 10mm, and then goes unavailable, after it comes back to 10 again, it counts it as 10+10 (because of difference 10–>0 + 0–>10). How can I avoid this from happening?
I checked, my rain stats and input_number do not go unavailable, it is only stats sensor.


image

Did you manage to solve the problem? I have exactly the same issue. When I just do a “quick reloan”, not “restart”, then the value is right.
To me it is something about initializing template during restart.

No, I did not. I use different approach where my weather station has “event rain” sensor and use this value as condition.