Struggling with incorrect min / max / distance_absolute values

I might be overlooking something obvious, but I just cannot understand this issue I am experiencing with the statistics integration.

I have a simple counter, which tracks the number of heating cycles of my heat pump.

I now want to derive the number of heating cycles over the past 24h. This should be a simple max - min calculation, ie 7424 - 7417 = 7. But HA gives me 6!?

I have tried statistics change, I have tried statistics distance_absolute, both give me 6. (See my sensor configuration below).

I then looked at min and max values, and the underlying issue seems to be that the min value returned is 7418, rather than 7417.

I also tried adding keep_last_sample: true (I have to admit, from the docs I still do not full understand what it does), but that made no difference.

Can anybody help me wrap my head around this?

- platform: statistics
  name: "WP Heizzyklen 24h"
  entity_id: sensor.cycles_waermepumpe
  unique_id: heizzyklen_waermepumpe_flamme
  state_characteristic: change
  max_age:
    hours: 24
  precision: 0
- platform: statistics
  name: "WP Heizzyklen 24h (Distance)"
  entity_id: sensor.cycles_waermepumpe
  unique_id: heizzyklen_waermepumpe_flamme_2
  state_characteristic: distance_absolute
  max_age:
    hours: 24
  precision: 0
- platform: statistics
  name: "WP Heizzyklen 24h (Max)"
  entity_id: sensor.cycles_waermepumpe
  unique_id: heizzyklen_waermepumpe_flamme_max
  state_characteristic: value_max
  keep_last_sample: true
  max_age:
    hours: 24
  precision: 0
- platform: statistics
  name: "WP Heizzyklen 24h (Min)"
  entity_id: sensor.cycles_waermepumpe
  unique_id: heizzyklen_waermepumpe_flamme_min
  state_characteristic: value_min
  keep_last_sample: true
  max_age:
    hours: 24
  precision: 0

The arrow in the graph below points to the first state change in the last 24 hours. This is important because the sensor only updates on state changes:

This is why you are seeing 7418 as the minimum. After the first state change in the 24 hour period the value was 7418.

Thank you for the quick response! That is what I had feared (and is really confusing behavior, I wonder what happens if there is no increase in the counter over 24h).

So is there any straightforward way to get what I am looking for? The numerical increase in my counter over 24h? That must be something users would require frequently. Right away, my rain counter would come to mind (which I now realize has also occassionally been giving me slightly off values, probably for the same reason).

As a last resort, I can create this via pyscript and running an SQL query against the short-term statistics, as I already did to measure counter increase for the current day. But I would think there must be a simpler solution?