Backup sensor if unavailable?

I use this displaying my outside temp on a nextion display

  - platform: homeassistant   # outside temperature
    id: temperature_outside
    entity_id: sensor.ute_temperature
    accuracy_decimals: 0
    internal: true
    on_value:
       then:
          - lambda: 'id(nextiondisplay).set_component_text_printf("outtemp","%3.0f",id(temperature_outside).state);'

This is a Zigbee temp sensor connected to my HA server.
Sometimes this sensor is unavailable.
Is it possible with a if unavailable, get values from another sensor in home assistant?

  - platform: homeassistant   # outside temperature
    id: temperature_outside_1
    if temperature_outside_1 is unavailable
    use temperature_outside_2
    entity_id: sensor.ute_temperature
    accuracy_decimals: 0
    internal: true

Is this possible?

Add 2 homeassistant sensors in your esphome, one for temperature_outside_1 and another one for temperature_outside_2. Create a template sensor, use the lambda to select the sensor value based on its state.

Do you have an example, how the template should look like?

Something like this should work, though depends how your sensor behaves when it’s in unavailable state.

- platform: template
  name: Consolidated temperature Outside
  lambda: |-
    return !isnan(id(temperature_outside_1).state) 
      ? id(temperature_outside_1).state
      : id(temperature_outside_2).state;

It reads the state of first sensor, when it’s NaN (Not A Number) it’ll will use the reading from the second sensor.

Thank you.
It works like I hoped.
Is it easy to do the same if the value is older than 3 hours? Sometimes it takes a while before it is discovered af unavailable.

Believe that you can use a combination of sensors and sensor filters to achieve that. Personally I prefer a customer lambda expression which could be something like this:

- platform: template
  name: Consolidated temperature Outside
  lambda: |-
    const unsigned long threeHoursInMs = 3 * 60 * 60 * 1000;
    static unsigned long lastTempUpdate = millis();
    static float lastTempValue = NAN;
    if (isnan(id(temperature_outside_1).state)) {
      return id(temperature_outside_2).state;
    } else if (id(temperature_outside_1).state != lastTempValue) {
      lastTempUpdate = millis();
      lastTempValue = id(temperature_outside_1).state;        
    } else if (millis() - lastTempUpdate > threeHoursInMs) {
      return id(temperature_outside_2).state;
    }
    return lastTempValue;

What it does is: it stores the current tick count in a static var. Upon updating the sensor it checks if the state of temperature 1 is NAN, if so will immediately return temperature 2 value. Otherwise, when value is different than the stored value (initially set to NAN) then it stores the new value and updates the tick count. When last change was more than 3 hours ago, will also return temperature 2 value but when less than 3 hours, will return temperature 1 (= last known value).

Disclaimer: haven’t tried/tested this, it should work in theory… Let us know if this works.

This seems to work fine. Thank you again. :smiley: