WT… Home Assistant integration, filtering and derivative

My single biggest annoyance of the last few weeks is somewhere between the filter, derivative and integration sensors and the inconsistency of the sampling they do. In general, as a user, I expect to see 2 things:

  1. to be able to specify an update frequency of the sensor, in other words: how often does HA update for instance a derivative of a source sensor?
  2. to be able to select how sampling is handled, so I would like to specify if a sample should be taken at every change, update or at a specific time interval for these 3 operations.

The current state is a bit of a mess in the sense that there is no possibility to specify these parameters, so as a user I have to live with what is hard-coded, and the 3 operations use different assumptions of what I want.

So a simple example:
I would like to take the derivative of a utility sensor which doesn’t change very often. The default behavior of this sensor is such that there is no update to the sensor if the current data coming in has not changed. This means the state of the derivative will not go to zero if my consumption does not change.

Created an intermediate time-pattern-triggered template sensor which mirrors the current state of the source sensor and (critically) added an attribute which changes every single sample (a timestamp in my case)

Another example:
Filter on the other hand looks at the last_changed attribute, so the fix I mentioned before does not work. What I ended up doing there was to again create a template, but take the sensor value (in this case a temperature) and ‘jitter’ the input sensor by adding 0.01 degrees every other minute to ensure ‘last changed’ actually recorded a value.

Sorry if this is a bit of a deep dive into the subject, but if it helps I am more than happy to clarify this further!

I cannot code myself (yet :smile: ) but if I were to take a clean sheet I would:

  1. Merge all these operations in one ‘maths’ integration
  2. Make the behavior consistent across all 3
  3. Allow the user to specify which sampling model is needed (legacy behavior being the default)