Configuration Advice

I’m putting together an air quality sensor using an SDS011.
I have this configured and working on an ESP-12E NodeMCU in ESPHome.

sensor:
- platform: sds011
  update_interval: 10min
  pm_2_5:
    name: "PM <2.5µm"
    id: "PM25"
    filters:
    - filter_out: nan
    - sliding_window_moving_average:
        window_size: 6
        send_every: 1
        send_first_at: 1
  pm_10_0:
    name: "PM <10.0µm"
    id: PM100
    filters:
    - filter_out: nan
    - sliding_window_moving_average:
        window_size: 6
        send_every: 1
        send_first_at: 1

As I said this works. What I’d like to do is have each data point returned as raw data and also calculate the average over the last hour and the average over that last 24 hours.

What’s the best way to accomplish this? I’d like to have all three values (raw, 1 hour avg, 24 hour avg) available in Home Assistant. I’ve started to look into Template Sensors but haven’t completely figured them out yet but it seems like the route to go.

Thanks

Look at derivative sensors.
https://www.home-assistant.io/integrations/derivative/

Also look at the PurpleAir thread here on the HA forums. The basics are still the same, and you might find a nice way to display it.

Take a look at my display:
https://github.com/GlennGoddard/CanvasGaugeBackgrounds

The derivatives look interesting. I may look into integrating them into the config.

Looks like a nice display. I’m working my way towards something similar. Assembling things piece by piece.

I ended up adding a Template for each of the averaged values. It seems to be working but it’s only been running for a few minutes so the averages aren’t there yet. Does this seem reasonable or is there something that will cause issues?

The one thing I’d like to change is how the averaged values are triggered. Right now they’re just on a timed interval. Is it possible to trigger them when the source value is updated? So I’d like to trigger the 1 hour average when the Raw value is triggered and the 24 hour average when the 1 hour value is triggered. Seems this would ensure the averages would be using the latest values.

- platform: sds011
  update_interval: 10min
  pm_2_5:
    name: "PM <2.5µm"
    id: "PM25"
  pm_10_0:
    name: "PM <10.0µm"
    id: PM100

- platform: template
  name: "PM <2.5µm 1 Hour Avg"
  id: PM251Hour
  unit_of_measurement: 'µg/m³'
  update_interval: 10min
  state_class: measurement
  icon: mdi:chemical-weapon
  lambda: |-
    return (id(PM25).state);
  filters:
  - filter_out: nan
  - sliding_window_moving_average:
      window_size: 6
      send_every: 1
      send_first_at: 1

- platform: template
  name: "PM <2.5µm 24 Hour Avg"
  id: PM2524Hour
  unit_of_measurement: 'µg/m³'
  update_interval: 60min
  state_class: measurement
  icon: mdi:chemical-weapon
  lambda: |-
    return (id(PM251Hour).state);
  filters:
  - filter_out: nan
  - sliding_window_moving_average:
      window_size: 24
      send_every: 1
      send_first_at: 1

- platform: template
  name: "PM <10.0µm 1 Hour Avg"
  id: PM1001Hour
  unit_of_measurement: 'µg/m³'
  update_interval: 10min
  state_class: measurement
  icon: mdi:chemical-weapon
  lambda: |-
    return (id(PM100).state);
  filters:
  - filter_out: nan
  - sliding_window_moving_average:
      window_size: 6
      send_every: 1
      send_first_at: 1

- platform: template
  name: "PM <10.0µm 24 Hour Avg"
  id: PM10024Hour
  unit_of_measurement: 'µg/m³'
  update_interval: 60min
  state_class: measurement
  icon: mdi:chemical-weapon
  lambda: |-
    return (id(PM1001Hour).state);
  filters:
  - filter_out: nan
  - sliding_window_moving_average:
      window_size: 24
      send_every: 1
      send_first_at: 1

I don’t think so, the time is based off when it is run initially and then the periodicity after that. There could be a complicated way to do in in an automation.

Derivative sensors are not averages.

Also look into moving averages. You’ll be able to set up 1-hour and 24-hour moving averages.