Log error on simple template. How to handle unavailable/unknown states?

First attempt at templating for me/ I’m trying to do a simple value adjustment for a temperature sensor. It works OK except there is always an error in the log when the entity is unavailable (flaky bluetooth connection!)

My template is:

{% if states('sensor.ibs_th_0373_temperature') | is_number %}
	{{ states('sensor.ibs_th_0373_temperature') | float - 3.5 }}
{% else %}
	{{ states('sensor.ibs_th_0373_temperature') }}
{% endif %}

and the log error is
ValueError: Sensor sensor.calibrated_pool_temperature has device class 'temperature', state class 'measurement' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unavailable' (<class 'str'>)

So I’m unclear how to handle the unavailable state. I don’t do any math if the state isn’t a number but apparently that’s not good enough!

I must have missed something obvious in the docs but I can’t find it :thinking:

If the temperature sensor’s value is not a number, this version reports the Template sensor’s existing value.

{% if states('sensor.ibs_th_0373_temperature') | is_number %}
	{{ states('sensor.ibs_th_0373_temperature') | float - 3.5 }}
{% else %}
	{{ this.state }}
{% endif %}

That would work I guess, but it would still be good to know that the sensor is unavailable.

I assume that template is used in a Template Sensor. Are you using its availability option?

It is in a template sensor. I created it through the UI (settings > devices&services > helpers)

If it’s not in the UI, I didn’t do it! This is all I did:

I wasn’t aware of the availability option.

If I’m understanding you correctly, I’d need to add another template for Availability? If so, where would I do that? I can’t find the YAML that the UI created.

  • You can create a Template Sensor via the UI (referred to as a Template Sensor helper).

  • You can create a Template Sensor via YAML.

A Template Sensor helper only offers a subset of the features available in its more capable YAML-based version.

In order to take advantage of a Template Sensor’s additional features, like availability, you will have to create it in YAML (i.e. replace the Template Sensor helper you currently have).

OK. I’ll give that a try. Appreciate the help.

Sucks that the UI gets you so far and then abandons you without even getting the starting YAML. Inconsistent with other parts of the UI.

I’m hoping that a Template Sensor helper will eventually support all the features of a Template Sensor. Currently it’s an MVP implementation (minimum viable product).

Just a thought:

Keep the Template Sensor helper (with the change I suggested) and create a new one in YAML (it will need a different entity_id).

Observe how they operate when encountering availability issues and choose which you prefer.

You could use none when the state of the source sensor is not a number. That is accepted (will not generate errors in your log) and the template sensor will become unknown

{% if states('sensor.ibs_th_0373_temperature') | is_number %}
	{{ states('sensor.ibs_th_0373_temperature') | float - 3.5 }}
{% else %}
	{{ states('sensor.ibs_th_0373_temperature') }}
{% endif %}

Or like this

{% set source = states('sensor.ibs_th_0373_temperature') | float(none) %}
{{ source - 3.5 if source is not none else source }}

@TheFes
I’m still learning Jinja so your second example is interesting.

The thing is
{{ states('sensor.ibs_th_0373_temperature') }}

is what craps out in the log. I think because I defined the sensor as a measurement class which is a number whereas {{ states('sensor.yadayada...}} returns a string if the state is unknown. It makes sense but this is my first template sensor so there is a lot to learn :grinning:

This is what I ended up with which works quite well:

template:
    - sensor:
        name: "Pool Temp (Calibrated)"
        unit_of_measurement: "°C"
        state_class: "measurement"
        device_class: "temperature"
        state: >
            {{ states('sensor.ibs_th_0373_temperature') | float - 3.5 }}
        availability: >
            {{ is_number(states('sensor.ibs_th_0373_temperature')) }}

Thanks everyone for the help. I’ve learned a lot!

This seems like a heck of a lot of work to subtract 3.5 from an existing value though?! Is there a simpler way?