How to find the average of multiple Temperature Sensors

Hi,
I am super new to Home Assistant, and I was hoping to be able to find the average of 5 or so temperature sensors I have scattered around my house and depending on the value I would then like to change the settings of my thermostat.
Can anyone help guide me on how to do this?

Welcome!

I’ve been very happy with using this tool for temperature, humidity and air quality averaging. Have been using for over a year. Good hunting!

Why not use the Max/Min/Average/Median helper sensor?
I use it for multiple outdoor and indoor temp sensors.
configuration → helpers

4 Likes

sorry if this is a silly question but how do you use Helpers?

I guess you have an automation to set the the thermostat? Then you could use the average value from the helper sensor to set it.

1 Like

I am not sure about earlier responses but what I read is that you want to have
(temp1 + temp2 + temp3 + temp4 + temp5) / 5
From what I know… you need a template sensor…try this out with your own sensors, replacing sensor.temp1/2
In developer tools > templates you can see the result

{{ (states('sensor.temp1')|float + states('sensor.temp2')|float ) / 2 }}
1 Like

Yes, this is one way of doing it, and I did it. But why complicate it if you are a beginner? The tools are already there in the UI.

1 Like

I am trying to respond to the OP, your vidoe discusses something else…might help too but at present … my 2 cents. I am not aware of a helper that can take into account the average value of multiple sensors

2 Likes

Ultimately yes, it is a template sensor.

It is a little more complicated as some of the sensors in the population might be unavailable or provide some outrageous, out of bounds, values or did not change for a large amount of time.

Some of the question that might be raised: what value do we use for the unavailable sensors or the ones that don’t provide relevant info? Should we approximate to the values of the available sensors? Should we use some default values (like 25 degrees (Celsius :smiley: ))?

I have this in my configuration.yml

sensor:
  - platform: min_max
    name: Average Woonkamer Temperature
    type: mean
    round_digits: 1
    entity_ids:
      - sensor.tstat_6db6f3_current_temperature
      - sensor.netatmo_woonkamer_temperature
      - sensor.airquality_1_temperature
      - sensor.airco_woonkamer_temperature
6 Likes

Sorry, I was not trying to correct you. I was just (in my opinion) trying to help the OP in the tools that are already available in the UI. The Min/Max helper sensor also has mean from multiple sensors

I’m not sure if it’s the same or not but I had a ā€œmisconceptionā€ about what HA is currently defining as ā€œhelpersā€.

I thought it was only input_xx entities that were helpers until someone pointed out that now other integrations are being called helpers too. I had no idea since I never use the UI to create them.

It makes no sense to me what the standards are for something to be called a ā€œhelperā€ but you can see which integrations are helpers by going to the ā€œhelpersā€ tab under ā€œdevices & servicesā€ menu. The min/max sensor (and a bunch of others) is in the list of available helpers to create.

The min/max helper has a setting called something like Mathematical Average or something like that. Thats what I use. Im in the midst of a power outage or I would look.

One question for the helpers:

When I have a helper that gives me the median of for example 3 temperature values and one of the 3 entities is off (e.g. sensor signal missing). Will it then calculate the median of the 2 entities, that are still available or will it calculate something that does not fit ?

If one or more source sensors are unavailable, the state of the helper will be derived from the states of those sensors that are available.

In case someone needs something more robust, I came up with this template sensor that could be adapted to any values in a similar situation. I have 2 outdoor zigbee sensors, but if one or more stop reporting properly I need to make sure that I have good data… and so I only average those that are reporting, unless they are both offline. Then I fall back on a cloud sensor.

{# INPUTS #}
{%- set sensor_entities_to_avg = ["sensor.outside_front_climate_temperature", 
                                 "sensor.outside_rear_climate_temperature"] -%}
{%- set cloud_fallback_entity_if_locals_dead = "sensor.nws_temperature" -%}
{# LOGIC #}
{%- set ns = namespace(temperatures = [], 
                      sensor_entities_to_avg=sensor_entities_to_avg, 
                      cloud_fallback_entity_if_locals_dead=cloud_fallback_entity_if_locals_dead,
                      value_to_return=0) -%}
{%- for ent in sensor_entities_to_avg -%}
  {%- if states(ent) | is_number -%}
    {# Add sensors that are reporting properly #}
    {%- set ns.temperatures = ns.temperatures + [states(ent) | float] -%}
  {% endif %}
{%- endfor -%}
{%- if ns.temperatures | count > 0 -%}
  {# Average sensors that are reporting properly #}
  {%- set ns.value_to_return = ns.temperatures | average(0) -%}
{%- else -%}
  {# No sensors working, fall back here. #}
  {%- set ns.value_to_return = states(cloud_fallback_entity_if_locals_dead) | float -%}
{%- endif -%}

{{- ns.value_to_return -}}

If you’re interested, here’s a shorter way to achieve the same result.

{% set a = ['sensor.outside_front_climate_temperature', 'sensor.outside_rear_climate_temperature'] 
  | select('has_value') | map('states')
  | map('float', 0) | average(none) %}
{{ a if a is not none else states('sensor.nws_temperature') }}
  • has_value('sensor.my_sensor') will test if the given entity is not unknown or unavailable. Can be used as a filter or a test.

Reference

Templating - States

3 Likes

Oh nice. That’s some next level templating kung fu. :slight_smile: :ninja:

also, I didn’t know you could use (none) as a fallback value. good to know.

This is the most neat solution. I am wondering why this is not natively in the HA helpers, where one could list the entities to make an average from and have a fallback option to ā€˜none’ or ā€˜unknown’ when one of them fails.

Edit, in fact the Min/Max option exists: