Two temperature sensors with average and error

Hi, I have two temperature sensors in the greenhouse and the average of the same is calculated, which gives a result on the template sensor. The average works great in case there are readings from both sensors but if one falls then there is no average template sensor.

How to configure the sensor template to calculate the average and if there are no readings of one sensor, use the value of another sensor that is available?

sensor:
  - platform: dht
    pin: GPIO23
    temperature:
      id: "master_temp_up"
      name: "Greenhous Temperature up"
      accuracy_decimals: 2
    humidity:
      id: "master_hum_up"
      name: "Greenhous Humidity up"
      accuracy_decimals: 2
    update_interval: 30s  
    
  - platform: dht
    pin: GPIO22
    temperature:
      id: "master_temp_down"
      name: "Greenhous Temperature down"
      accuracy_decimals: 2
    humidity:
      id: "master_hum_down"
      name: "Greenhous Humidity down"
      accuracy_decimals: 2
    update_interval: 30s 
    
  - platform: template
    name: "Greenhouse Temperature Average"
    id: "temp_avr"   
    unit_of_measurement: "°C"
    accuracy_decimals: 2
    lambda: |-
      return (id(master_temp_up).state + id(master_temp_down).state)/2;
    update_interval: 15s

You might try this add on average sensor. It has a number of useful options, including ways to deal with missing/unavailable values. I have had good success with it for about a year in my setup: