Hi Ward0. I donāt know if this will be of any use to you? When I created a āHome Average Temperature Sensorā I placed all potential sensors into a group. Whenever the sensor is updated (e.g. using the time pattern automation trigger), anomalous readings from malfunctioning devices are filtered out, the contributing devices are counted, and an average applied. E.g.
{% set temperatureSensors = [
states('sensor.temp_1') | float(default=0),
states('sensor.temp_2') | float(default=0),
etc. ] %}
or, set temperatureSensors = states.sensor | selectattr(āattributes.device_classā, āeqā, ātemperatureā).
{% set valid_temperatureSensors = temperatureSensors | select('gt', 0) | list %}
{% set averageTemperature = (valid_temperatureSensors | sum / valid_temperatureSensors | length) %}
{{ averageTemperature | round(1) }}
In my version, āunavailableā and āunknownā will default to 0, then only states greater than (āgtā) 0 are selected. You could also use filters and tests like āis_numberā or āhas_valueā or ārejectattr(āstateā, āeqā, [āunavailableā, āunknownā])ā.
SUM adds all the states together. LENGTH is the number of states added together, which is used to produce the average.