I use 1-wire sensors DS2423 to count several things like water, energy, wind, etc.
These counters are polled by HA using SNMP from a OWserver. Works great!
My problem now is how to calculate the rate of the meter, using the counter values.
Ideally I would like to calculate the rate when a new sample is available as:
rate = (value - counter[last entry]) / (now - time[last_entry])
Note, this is completely untested and I don’t know if the states.input_text.rate.last_changed in above formula will return the last time it got updated, but I hope it does.
Then if you want a “true” sensor you can add a template_sensor that picks up the value of input_text.rate
It took a while, but now the statistics template is working for me. (The “_mean” suffix was not expected…)
I think the module assumes all samples are evenly spaced in time. The rate etc. is using a number of samples without looking at the sample times.
Please consider to improve this module so the time for each sample is used.
Here is an example where I stopped sampling a counter for a while. The rate should be approximately flat, but…
This is the immediate change_rate as I see it. len(self.states) is completely wrong to use as it assumes something about the sample rate.
After I apply a low-pass filter on this signal I get the curve I wanted.