Get average of multiple temperature sensors

Hey, I’m hoping someone might be able to help me!

So I have 3 mqtt temp sensors at the minute as below which are working great, however I would like to get the average temp for all three to be able to use it in a thermostat control as well as display its value as another sensor : I’ve looked at templates and can only guess that’s where i need to be but am completely lost as to where to start?

sensor 1:
  platform: mqtt
  name: "Temperature Lounge"
  state_topic: "/lounge/sensor/temperature"
  qos: 0
  unit_of_measurement: "°C"

  
sensor 2:
  platform: mqtt
  name: "Temperature Bedroom 2"
  state_topic: "/bedroom2/sensor/temperature"
  qos: 0
  unit_of_measurement: "°C"

  
sensor 3:
  platform: mqtt
  name: "Temperature Bedroom 1"
  state_topic: "/bedroom1/sensor/temperature"
  qos: 0
  unit_of_measurement: "°C"

I was going somewhere along these lines but just trying to do some simple maths to start to prove concept but I can’t even get that working as I may be barking up the wrong tree!

sensor 4: 
  - platform: template 
    sensors:
      mqtt:
        friendly_name: 'house'
        value_template: >-
          {{sensor.temperature_lounge / 2}}
        entity_id:
          - sensor.temperature_lounge

Does anyone think they might be able to help?

Cheers!

2 Likes

Well after a bit of messing around with templates I managed to get it working as follows:

sensor 4: 
  platform: template 
  sensors:
    average_house_temp:
      friendly_name: 'Average House Temp'
      unit_of_measurement: "°C"
      value_template: >-
        {{ (float(states.sensor.temperature_lounge.state) + float(states.sensor.temperature_bedroom_1.state) + float(states.sensor.temperature_bedroom_2.state)) / 3 | round(2) }}

The round doesn’t seem to be working so if anyone can give any pointers on that I’d greatly appreciate it, ant it probably isn’t the best way to do it but it works and I’m quite pleased!

8 Likes

Thank you for posting this, I was looking for a bit more of a reliable way to get overall luminance across 3 multisensors and this looks perfect.

As far as the round, it just looks like you need another set of parentheses:

    average_house_luminance:
      unit_of_measurement: "%"
      value_template: >-
        {{ ((float(states.sensor.living_room_4in1_luminance_8.state) + float(states.sensor.master_bed_4in1_luminance_30.state) + float(states.sensor.office_4in1_luminance_32.state)) / 3) | round(1) }}
1 Like

This was super helpful thank you! I wanted to average some temperature sensors, and round one temperature. It took a little tweaking but wanted to add my final that is working great.

sensor 1:
  platform: template
  sensors:
    garage_avg_temp:
      friendly_name: "Garage Temperature"
      unit_of_measurement: "°F"
      value_template: >-
        {{ ((float(states.sensor.garage_temperature.state) + float(states.sensor.garage_temperature_2.state)) / 2) | round(2) }}

sensor 2: 
  platform: template
  sensors:
    upstairs_avg_temp:
      friendly_name: 'Upstairs Temperature'          
      unit_of_measurement: "°F"
      value_template: >-
        {{ ((float(states.sensor.laundry_temperature.state) + float(states.sensor.upstairs_temperature.state) + float(states.sensor.stairs_temperature.state)) / 3) | round(2) }}

sensor 3:
  platform: template
  sensors:
    bedroom_temperature:
      friendly_name: 'Bedroom Temperature' 
      unit_of_measurement: "%"
      value_template: >-
       {{ (float(states.sensor.closet_temperature.state)) | round(2) }}
5 Likes

This could be an alternative to a template.

6 Likes

Note to users:

Be careful when copying the example files here - the encoding of the unit of measurement in the code above:

unit_of_measurement: "°F" and unit_of_measurement: "°C"

May not copy and paste in proper UTF-8 format and will cause HA to fail to start without producing an error log depending on your editor. This messed me up for a sleepless night until I got some help figuring out what the problem was. You can read more about it here:

Not sure if this is dependent on your editor, but I am using the same editor I have been using since I started using HA and never ran into this issue before. I raised a bug on github for it.

3 Likes