Template: Conversation Celsius to Fahrenheit give me 32 when device is no available

Hi,
I setup a ESPHome DS18B20 sensor that provide me Celsius temp and as I need to display both temp unit (C/F), I did use template and it work fine until the device get not responding where the template provide me 32 instead of “Not Available” like ESPHome entity

Below is my config where you did try each of test 1 and test 2 without success.

sensor:
#----- Template -------
  - platform: template
    sensors:
      pool_water_temperature_f:
        friendly_name: "Pool Water Temperature F"
        unit_of_measurement: 'ºF'
#--- test1 ---
        value_template: "{{ (((states('sensor.pool_water_temperature_c') | float) * 9/5)+ 32)|round(1)}}"
#--- test 2 ---
        #value_template: "{{ (((states.sensor.pool_water_temperature_c.state | float) * 9/5)+ 32)|round(1)}}"

Do HA have some way when the device is not available to also provide this status to a template function? I though test 1 will provide this benefit but both react by providing me only 32 causing big spike in my graph and losing the resolution.
image
image

Any suggestion?

Assuming the actual state of sensor.pool_water_temperature_c is unavailable when it shows as “Not Available” in the frontend:

        value_template: >
          {% set tempc = states('sensor.pool_water_temperature_c') %}
          {% if tempc == 'unavailable' %}
            unavailable
          {% else %}
            {{ (((tempc | float) * 9/5) + 32)|round(1) }}
          {% endif %}

Awesome! Thanks Phil for the tip it work perfectly. :smiley:
Just learn something new today.

1 Like