Sensor Template | use backup sensor if main not available

I have a BLE sensor attached to a remote HA instance which sometimes times out. When this happens, I want to use the main HA instance (provided the BLE sensor is in range) to show the BLE value.

I have seen many contradicting examples over this forum and reddit but none seem to work for me… I have tried == none None unknown Unknown unavailable Unavailable and nothing works for me. In HA Lovelace is shows as Unknown

What is the correct syntax?

configuration.yaml

sensor:
  temperature_kitchen:
        friendly_name: 'Kitchen Temperature'
        unit_of_measurement: °C
        value_template: >
          {% if states('sensor.pz_ble_temperature_kitchen') == "Unknown" %}
            {{states('sensor.ble_temperature_kitchen')}}
          {% else %}
          {{states('sensor.pz_ble_temperature_kitchen')}}
          {% endif %}

If it indicates Unknown in the Lovelace UI then in Developer Tools > States it will be displayed as unknown (all lowercase) which is its actual state value.

sensor:
  - platform: template
    sensors:
      temperature_kitchen:
        friendly_name: Kitchen Temperature
        unit_of_measurement: "°C"
        value_template: >
          {% set t1 = states('sensor.pz_ble_temperature_kitchen') %}
          {% set t2 = states('sensor.ble_temperature_kitchen') %}
          {{ t2 if t1 == 'unknown' else t1 }}

Legacy Template Sensor Configuration Format

1 Like

Oh, that worked! Thanks Taras!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This makes it easier for users to find answers to similar questions. For more information refer to guideline 21 in the FAQ.

If I wanted to set an attribute to show with the state was coming from Sensor A or B… how would I incorporate that? Thanks in advance!

sensor:
  - platform: template
    sensors:
      temperature_kitchen:
        friendly_name: Kitchen Temperature
        unit_of_measurement: "°C"
        value_template: >
          {% set t1 = states('sensor.pz_ble_temperature_kitchen') %}
          {% set t2 = states('sensor.ble_temperature_kitchen') %}
          {{ t2 if t1 == 'unknown' else t1 }}
        attribute_templates:
          source: >-
            {% set t1 = states('sensor.pz_ble_temperature_kitchen') %}
            {% set t2 = states('sensor.ble_temperature_kitchen') %}
            {{ 'B' if t1 == 'unknown' else 'A' }}
1 Like

Legend, thank you.

1 Like

Say Taras… you’re kinda smart… is it possible to set up sensors something like this? Or not possible with sensors…?

ble_sensors = {‘kitchen’ : ‘pz_ble_temperature_kitchen’, ‘ble_temperature_kitchen’, ‘ben’ : ‘sensor.pz_ble_temperature_ben’, ‘sensor.ble_temperature_a4c13825e91f’ }
for name in ble_sensor.name():

and set up the friendly name and sensor 1 and 2?

Use friendly_name_template (instead of friendly_name) with the same template that was used for attribute_templates in the previous example.

1 Like