Filter sensors and the zero value

EDIT: (original question is below)

Based on what I have learned I will rephrase the question:

How do I create a sensor that averages another (real) sensor over a time period, not just an average of the individual real sensor readings?

The real sensor has long periods at 0 which should count as many individual readings (and be averaged). In actuality the long periods at 0 simply count as a single 0 reading so the computed average (using a filter time_simple_moving_average ) is wrong.

I’m really stuck here so would very much appreciate any help.


Original question:
I have created a filter sensor to iron out spikes in the value of an electricity import sensor:

  - platform: filter
    name: Import Power filtered 
    entity_id: sensor.givtcp_sa2236g166_import_power
    unique_id: import_power_filtered
    filters:
      - filter: outlier
        window_size: 4
        radius: 2.0
      - filter: lowpass
        time_constant: 10
      - filter: time_simple_moving_average
        window_size: "00:03"
        precision: 0

The problem I have is that if the input value (in this case sensor.givtcp_sa2236g166_import_power) to the filters has been positive for a while and suddenly drops to zero and stays there, the zero value is not included in the internal calculations so the output value of the sensor (Import Power filtered) stays with whatever is was. I would expect that as more and more zero input values arrive the output value of the filter would decay away to zero.

What is going wrong?

All your zero values are further than your specified outlier radius so they are being rejected and the last known good value that occurred is being kept. This is how the outlier filter works.

Thanks for the reply Tom. Unfortunately, this doesn’t appear to be my problem. I have removed the outlier filter altogether and the result is the same.

For example. a while ago the import figure was around 500 watts and the output of this filter sensor became very close to 500 (480 to be exact). The input import figure then dropped to 0 and has remained that value for around 15 minues. The filter sensor is still outputting 480.

for reference the code is now:

 - platform: filter
    name: Import Power filtered 
    entity_id: sensor.givtcp_sa2236g166_import_power
    unique_id: import_power_filtered
    filters:
      - filter: lowpass
        time_constant: 10
      - filter: time_simple_moving_average
        window_size: "00:03"
        precision: 0

The filter sensor only updates when the source sensor updates/changes.

Okay. So…How do I get round this please?

I have tried using a time_pattern trigger but I can’t work out how to incorporate filters into the code:

template:
  - trigger:
      - platform: time_pattern
        seconds: /20
    sensor:
      - name: Import Power Filtered
        unique_id: import_power_filtered
        unit_of_measurement: "W"
        state_class: measurement
        state: "{{ states('sensor.givtcp_sa2236g166_import_power')|float(0) }}"

That is as far as I can get but I want to do an average. Do I have to code one myself in the template?

Solution:

Use the Average integration from Limych - available in HACS

I’m having the same issue Has anybody figured this out?

- platform: filter
  name: "Fridge Power Filtered"
  unique_id: 111860b5-932c-477d-9eca-32ae4113e53f
  entity_id: sensor.fridge_energy_power
  filters:
    - filter: lowpass
      time_constant: 5
    - filter: throttle
      window_size: 20
      precision: 1