Getting the average value from a group of sensors

Hi there,

i am currently evaluating a migration from OpenHab2 to Home Assistant and figuring out how to achieve various things i have done in OH2.

For example:
I have a set of Homematic motion and brightness sensors (HM-Sen-MDIR-O-2), each reporting the brightness as a value from 0-255. I want to compute the average brightness (in percent) from all the sensors. They are entities of a group with ID “group.brightness_sensors”.

{
  "order": 4,
  "entity_id": [
    "sensor.meqXXXXXX_brightness",
    "sensor.meqYYYYYY_brightness"
  ]
}

Currently, I use a template sensor with a value template to compute and return the average:

platform: template
sensors:
  avg_brightness:
    value_template: '{{ "%.0f" | format((states.sensor.meqXXXXXX_brightness.state | int + states.sensor.meqYYYYYY_brightness.state | int) / 5.12) }}'
    unit_of_measurement: '%'

That way it works currently, but it has two drawbacks:

  • it gets messy, if more sensors are installed
  • it fails on HA restart, as the sensor states may not be available. HA reports

Could not render template avg_brightness, the state is unknown

Is there a more reliable way to compute the something based on the states of grouped entities?

Regards

@m0wlheld

This may interest you…

1 Like

It definitely does! Thank you.

For the record:

sensor:
  platform: min_max
  name: brightness
  round_digits: 0
  type: mean
  entity_ids:
    - sensor.meqXXXXXX_brightness
    - sensor.meqYYYYYY_brightness

and

sensor:
  platform: template
  sensors:
    avg_brightness:
      value_template: "{{ '%.0f' | format(states('sensor.brightness_mean') | int / 2.55) }}"
      unit_of_measurement: '%'
2 Likes

No problem! Hope you enjoy HA as much as I do!

Use the new Statistics Sensor in HA.