Average temperature based on multiple sensors

Hi guys,
i have an ESP with a dth22 connected

  - platform: dht
    model: DHT22
    pin: GPIO23
    temperature:
      name: ${name} "Temperature"
      id: dht22_t1
    humidity:
      name: ${name} "Humidity"
      id: dht22_h1

I also defined a home assistant based sensor

platform: homeassistant
name: "HA Temp"
id: "ha_t"
entity_id: sensor.ha_temp

I would like to set a thermostat component that is based on the Home assistant sensor (if available) but if it is not available it is based on the dht22 sensor

it’s possible?
how?

climate:
  - platform: thermostat
    id: "winter"
    name: ${name} "winter"
    sensor: IF AVAILABLE ha_t ELSE dht22_t1
    default_target_temperature_low: 20.5 °C
    heat_action:
      - switch.turn_on: OU8
    idle_action:
      - switch.turn_off: OU8
   

I think you’ll need a tempoate sensor https://next.esphome.io/components/sensor/template.html which contains the “if sensor a is available use its value, otherwise use that value”

Then use the template sensor in the climate component.

@blademckain , not sure if you found a solution but here is the yaml I am using to average 2 temperature sensors together. In your example where you want to use this as the input into a climate thermostat you would define your sensor as “demo_temperature_average”

sensor:
  - platform: dht
    pin: D1
    model: DHT22
    temperature:
      id: "demo_temperature_1"
      name: "Demo Temperature 1"
    humidity:
      id: "demo_humidity_1"
      name: "Demo Humidity 1"
    update_interval: 5s
  
  - platform: dht
    pin: D2
    model: DHT22
    temperature:
      id: "demo_temperature_2"
      name: "Demo Temperature 2"
    humidity:
      id: "demo_humidity_2"
      name: "Demo Humidity 2"
    update_interval: 5s
  
  - platform: template
      id: "demo_temperature_average"    
      name: "Demo Temperature Average"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    lambda: |-
      return (id(demo_temperature_1).state + id(demo_temperature_2).state)/2;
    update_interval: 5s
1 Like

With protecttion against unavailable sensors:

  lambda: |-
      float sum = 0;
      uint16_t cnt = 0;
      if (!std::isnan(id(demo_temperature_1).state)) {
        sum += id(demo_temperature_1).state;
        cnt += 1;
      }
      if (!std::isnan(id(demo_temperature_2).state)) {
        sum += id(demo_temperature_2).state;
        cnt += 1;
      }
      if (cnt == 0) {
        return NAN;
      } else {
        return (sum / cnt);
      }
4 Likes

Just wondering if I wanted to use 3 temps would it be like this:

  lambda: |-
      float sum = 0;
      uint16_t cnt = 0;
      if (!std::isnan(id(demo_temperature_1).state)) {
        sum += id(demo_temperature_1).state;
        cnt += 1;
      }
      if (!std::isnan(id(demo_temperature_2).state)) {
        sum += id(demo_temperature_2).state;
        cnt += 1;
      }
     if (!std::isnan(id(demo_temperature_3).state)) {
        sum += id(demo_temperature_3).state;
        cnt += 1;
      }
      if (cnt == 0) {
        return NAN;
      } else {
        return (sum / cnt);
      }

Yes, indeed