Get total rainfall in a defined time interval

I’m trying to define sensors to show total rainfall in a certain interval, specifically

  • in the last 24h (not the last day but the last 24h from now)
  • in the interval between 48 and 24 hours ago
  • in the interval between 72 and 48 hours ago
  • etc
    I have a weather station that gives me instant, hourly, daily, weekly, yearly rain rate.

this works ok for the rainfall in the last 24h

  - platform: statistics
    name: "Rainfall 24 to 0 hours ago"
    entity_id: sensor.yearly_rain_rate
    state_characteristic: change
    max_age:
      hours: 24

problem was for the rainfall in the interval between 48 and 24h from now as i couldn’t find a way to have the statistics consider an interval :confused:

so i ended up defining another rainfall sensor for the last 48h

  - platform: statistics
    name: "Rainfall 48 to 0 hours ago"
    entity_id: sensor.yearly_rain_rate
    state_characteristic: change
    max_age:
      hours: 48

and then using a helper template to subtract the “Rainfall 24 to 0 hours ago” from the “Rainfall 48 to 0 hours ago”

{{ ( states('sensor.rainfall_48_to_0_hours_ago') | float
- states('sensor.rainfall_24_to_0_hours_ago') | float) | round(2) }}

this also works ok.

I was wondering if there’s a better way to do this as it is a double step and seems bulky if i have to do it for many other intervals