Statistics Sensor Question/Divide by Zero

I guess this is more of a question of how others feel this should work… I’m not sure if this should operate differently or I am just thinking about it the wrong way. I figure some of you guys with more programming/logic experience could better explain if it should work differently.

I use TP-Link HS-110s to monitor my washer and dryer, washer I’ve had setup for awhile , dryer I just recently finished setting up the sensors, etc. I use the statistics sensor and “average_watts_mean” to level out the watt fluctuations so I can monitor the progress… I can tell when it goes to “air dry” at the end of the cycle etc. (it is an old dumb dryer)…

- platform: statistics
  entity_id: sensor.dryer_watts
  name: Dryer Average Watts Mean
  sampling_size: 20 
  max_age:
    minutes: 5

The washer works fine and in an idle/done state reads 0.45 watts approximately… so the average_watts_mean for the washer works fine and levels out to about that after it’s done running…
The problem comes when the dryer is done… the watts on the dryer goes to a flat 0.0 and obviously to calculate the “average_watts_mean” it is then trying to divide by zero… which of course causes and issue and then the average watts mean sensor stays at the last actual reading which is usually about 250 watts… and gets stuck there.

Now I understand why dividing by zero doesn’t work… but my question is being a “statistics” sensor I guess I thought that even at zero… once the statistics sensor gets a couple 0.0 readings it would reflect that… I am by no means a math expert and was simply wondering if anyone else would expect that once multiple 0 readings were read… the sensor would reflect that… in my head statistics is the readings over some duration of time…

So ultimately, I see the potential for someone to think the statistics sensor is broken when in actuality, once its’ zero, the sensor is doing exactly what it should… I’m really just looking for clarification if my logic of what I think it should be doing is flawed…

watts on the dryer goes to a flat 0.0 and obviously to calculate the “average_watts_mean” it is then trying to divide by zero

(0+0+0+0…+0) / 20 is not dividing by zero, it’s dividing zero by 20 (your sample size) and should give a valid zero result.

So is something then broken with my sensor ? It has done this multiple times… (thanks for clarifying the math part of it… not my strong suit :grimacing:)… a homeassistant.update_entity immediately made it go to “unknown” for all attributes and state

Well it’s actually a windowed average so not as simple as my example. Still, I would not expect it to generate divide by zero errors just because the values are zero.

So calling update_entity on the regular watt sensor made it go to zero… I guess since it stops changing the actually entity stops updating so it stops taking samples? Maybe I’ll just have to add an update entity to the automation… thanks for helping clarify…