Problem with unknown sensor values

HA 2021.10:
In a template sensor, I have the following template value:
{{ states(‘sensor.abcd’)|round }}
Now I got the error message:

Template warning: ‘round’ got invalid input ‘unknown’ when rendering template but no default was specified. Currently ‘round’ will return ‘unknown’, however this template will fail to render in Home Assistant core 2021.12

{{ states(‘sensor.abcd’)|default(0)|round }} will not solve it.

How can I avoid this error?

1 Like

You can also add the default into the round:

{{ states('abcd')|round(default=0) }}
1 Like

I have a similar error on several template sensors and using round(default)=o does not work.
one of them is a simple conversion.

platform: template
sensors:

  total_distance_mtr_miles:
    friendly_name: "miles_walked"
    unit_of_measurement: 'miles'
    value_template: "{{ states('sensor.total_distance_mtr') | multiply (1/1609.3444) | float(2) | round(2)}}"

changing to round(default=0) (2) makes the sensor unavailable.
Solved by jms3000. round(2, default=0)

As I understand it states returns a string. So - convert it to float FIRST

{{ states('sensor.total_distance_mtr') | float(0) | multiply (1/1609.3444) | round(2) }}

round(2,default=0)

1 Like

Won’t round(2,default=0) result in 0’s in the data?
Can I set default=unknown?
And why must a default be set if availability is already checking if the input is a number with is_number in the first place?

Test it:
{{ “3x1”|round(default=“unknown”) }}

What do you do when it is just an unit conversion? You usually don’t want to convert “0” if the source is unknown but get unknown output instead? Is there any simple way to do that or do you need some if-else condition every time?

See the post directly above yours and set the default to whatever string you want.

How? If my formula is like that: (states('sensor.airrohr_bme280_pressure')|float / 133.322)|round(0) ? You can’t set the missing sensor value to string and then use it like a number for further calculations and still get “unknown” result.

yes you can.

{{ states(‘sensor.abc’)|float(default=‘unknown’) }}
{{ states(‘sensor.abc’)|round(default=“unknown”) }}

I don’t understand why the default value addition does not work in my case.
I tried multiple ways but still the warning:

Template warning: ‘float’ got invalid input ‘unavailable’ when rendering template ‘:host { --card-mod-icon-color: {% if states(‘sensor.paradijsvogelplant_vochtigheid’)| float <= 15 %} red {% else %} {% if states(‘sensor.paradijsvogelplant_vochtigheid’)| float >= 60 %} red {% else %} green {% endif %} {% endif %} }’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1

pops up in my log.

code in dashboard:

   - type: button
        icon: mdi:flower
        entity: sensor.paradijsvogelplant_vochtigheid
        name: pv plant
        show_name: false
        card_mod:
          style: |
            :host {
                --card-mod-icon-color:
                  {% if states('sensor.paradijsvogelplant_vochtigheid') != "unknown"  and states('sensor.paradijsvogelplant_vochtigheid') != "unavailable" %}
                    {% if states('sensor.paradijsvogelplant_vochtigheid')| float(default=0) <= 15  %}
                      red
                    {% else %}
                      {% if states('sensor.paradijsvogelplant_vochtigheid')| float(default=0) >= 60  %}
                        red
                      {% else %}
                        green
                      {% endif %}
                    {% endif %}
                  {% endif %}
            }

Well for a start you don’t have an else case for you first if statement. What does the template do if the sensor is unknown or unavailable?

1 Like

Good one… It can be grey for my part.
It actually solved the issue!!
I could not imagine that that would solve this warning. I expected it not to matter and that the function would resolve in no change without the ‘else’.

Could you elaborate on that if you know?

   - type: button
        icon: mdi:flower
        entity: sensor.paradijsvogelplant_vochtigheid
        name: pv plant
        show_name: false
        card_mod:
          style: |
            :host {
                --card-mod-icon-color:
                  {% if states('sensor.paradijsvogelplant_vochtigheid') != "unknown"  and states('sensor.paradijsvogelplant_vochtigheid') != "unavailable" %}
                    {% if states('sensor.paradijsvogelplant_vochtigheid')| float(default=0) <= 15  %}
                      red
                    {% else %}
                      {% if states('sensor.paradijsvogelplant_vochtigheid')| float(default=0) >= 60  %}
                        red
                      {% else %}
                        green
                      {% endif %}
                    {% endif %}
                  {% else %}
                    grey
                  {% endif %}
            }