Statistics an value_min odd behavior?

I have a mqtt configured house water flow sensor that gets updated in home assistant on a 10s interval. Most of the readings are, unsurprisingly, zero, as water doesn’t flow in the house more than occasionally.

The sensor is configured as follows (through mqtt):

    "name": "Main Water Flow",
    "state_topic": "homeassistant/sensor/water_flow_main/state",
    "unique_id": "water_flow_main_flow",
    "unit_of_measurement": "gal/m"

That works as expected. I then added a statistics sensor whose value should be the minimum value of the above sensor over the past 5 minutes:

  - platform: statistics
    unique_id: <...>
    entity_id: sensor.water_flow
    state_characteristic: value_min
    name: "Main Water Flow 5m min"
    sampling_size: 40
      minutes: 5

My expectation with this was that the value_min sensor would only ever go above zero if there’s a window in time longer than 5 minutes where the value is over zero. That is not what I’m seeing, per the below snapshot of a history view of the two sensors.


In the end, what I want to do is trigger an automation if 5 minutes goes by and the water flow sensor never hits zero. Is this a bug in home assistant, or am I misunderstanding something around the statistics usage or something else? If there’s a better way to do that, I’d love to hear it!

You don’t need a stats sensor for this.

 - platform: numeric_state
   entity_id: sensor.water_flow
   above:  0.01
     minutes: 5

This will only trigger if the sensor goes from below 0.01 (i.e. from zero) to above 0.01 and stays there for for 5 minutes.

You cut the Y-axis off your screen shot of the graph but 0.01 should do though it could be 0.1 if that is the minimum step your sensor takes. Or it could be 0.0001, or it could be 1. Hard to say without seeing the Y-axis.

The main thing is, do not use above: 0 as the sensor would have to go from below 0 (i.e. negative) to above 0 to trigger. And your sensor does not go below 0. Use the minimum step your sensor is capable of making (or less).

Thanks tom_l, you of course make a very good point, no need to over-complicate this as I was doing. And thanks for the detailed suggestion, that will work well in my case.

I’m still curious why the statistics module doesn’t appear to do what I expect, but I’ll leave that for some other time when it actually matters for what I want to do…


Difficult to say. You have provided a picture of a blue and a purple line with no context as to which sensor each one represents.

I’m going to assume the blue line is the statistics sensor as I see a spike very likely produced by a restart (which is a completely separate issue).

The main thing you have to remember is that the stats sensor only updates when the source sensor changes state. Once the age of that state change sample exceeds 5 minutes it will be discarded.

So after your restart at about 9:19 the stats sensor had no state as the source sensor did not change until after 9:21. At this point the minimum value was set to the changed state of the source sensor and followed it down to 0 over the next short interval.

Now if your next change in state is longer than 5 minutes away all the samples will have been discarded and the first change in your source sensor will be set as the minimum of the stats sensor.

So in short, it does not work the way you want.