Description
The problem
If time_window is smaller than event updates there will be spikes in the output signal.
Setup:
My thermometer reports temperature every 10 sec and has a resolution of 0.1 C, and is giving steady values.
unit_time: h
time_window: "00:10:00"
Result:
Most output are 0 or small values < 1 C/h. But sometimes, ~40 minute interval, I get a value of 36 C/h, +/- 0.5.
Suggested cause:
36 C/h corresponds to 0.1 C/10s, i.e. it is calculated on the last two value updates only.
Looking at the code https://github.com/home-assistant/core/blob/dev/homeassistant/components/derivative/sensor.py
I assume that the section after the comment:
# Filter out the tuples that are older than (and outside of the) time_window
clears out all old data and then add the latest one which is only 10s old. Thus we get a temperature increase/decrease of 0.1 C/10s = 36 C/h.
HOWEVER, I can't explain why the spikes are positive, when the temperature is decreasing (are they added in the wrong order? I am not a Python dev).
Suggested solution:
If there has been no change in temperature during time_window, then use the time of the last temperature change, which is older than time_window.
What is version of Home Assistant Core has the issue?
2021.1.5
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
sensor.derivative
Link to integration documentation on our website
https://www.home-assistant.io/integrations/derivative/
Example YAML snippet
# Put your YAML below this line
sensor:
- platform: derivative
source: sensor.pool_temperature
name: Pool Temperature change per hour
unit_time: h # the resulting "unit_of_measurement" will be °C/h if the sensor.temperate has set °C as it's unit
time_window: "00:10:00" # we look at the change over the last 10 minutes
Anything in the logs that might be useful for us?
# Put your logs below this line
Please ignore the first hour of the graph with negative spikes, it was a different setup.
The last hour of the graph use a longer time window of 30 min which seems to work, but is too slow for my use.