Filter values average every 5 minutes

Hello all,
I have this sensor, that produces a large amount of data with peaks and lows (measuring PM2.5 air quality).

I would like to compute a sensor that returns a value every 5 minutes with the average of the original sensor in the last 5 minutes.
This is my code:

sensor:
  - platform: filter
    name: "PM25 average"
    entity_id: sensor.vindriktning_ikea_vindriktning_pm25
    filters:
      - filter: time_simple_moving_average
        window_size: "00:05"

But I still get more than one sample every 5 minutes:

My goal is to have a value that lasts for 5 minutes, so I can use it for some automations.
How can I do that?
Thank you very much,
Fabio

Use a trigger-based template sensor:

template:
  - trigger:
      - platform: time_pattern
        minutes: "/5"
    sensor:
      - name: "PM25 5 min"
        state: "{{ states('sensor.pm25_average') }}"

That will take the value of the filter sensor every 5 minutes starting on the hour.

Troon, is the _average suffix in the sensor name some jinja magic in combination with the time pattern sensor that will return the average of all values within this period? Because until now I thought the trigger based template sensor will only return the last value every X minutes.

Or are you suggesting creating two sensors, first calculate a floating average and then use the last value of this floating average every five minutes with the trigger based sensor?

No, sensor.pm25_average is what I’d expect your existing code to generate:

If the filter platform creates different entity IDs than the normal way of just slugify-ing the name, then use that entity ID instead.

Yes, that, as I said:

Troon, thanks for the quick reply, I hoped there was a way to prevent an additional sensor (that’s not used other than for a the final calculation).

I wonder why there isn’t a versatile statistics sensor available (or even as part of HA itself). The best I could find is GitHub - Limych/ha-average: Average Sensor for Home Assistant, but it’s not in active development, does not have a time pattern feature and does not provide other calculations (personally I find current deviation from a long term mean most useful). Besides the fact that you need HACS and there’s no UI for it.

EDIT: HA provides the Statistics - Home Assistant sensor, but when the source does not provide a single value in the time frame (e.g. 5 minutes), it changes to “unavailable” instead of using the last known value. This is an issue for sensors that only broadcast data when the value has changed. Also the HA statistics sensor does not allow merging multiple sensors to one (e.g. the average temperature of temperature in multiple rooms).

PS: I’m not the original poster. :slight_smile:

1 Like

Try this, it works for me:

sensor:
  - platform: filter
    name: "PM25 average"
    entity_id: sensor.vindriktning_ikea_vindriktning_pm25
    filters:
      - filter: time_simple_moving_average
        window_size: "00:05"
      - filter: time_throttle
        window_size: "00:05"

Just also add the time_throttle filter. Let me know if this works for you too.