Template sensor is unknown if the value is negative

I use this template

        {% if (states('sensor.p_ac') == 'unavailable') and (states('stromzaehler_total_power')|float >= 0.00) %}
        {{ 
          states('sensor.stromzaehler_total_power')|float
        }}
        {% elif (states('sensor.p_ac') == 'unavailable') and (states('stromzaehler_total_power')|float < 0.00) %}
        {{ 
          0.00 | float
        }}
        {% elif states('sensor.stromzaehler_einspeisung_aktuell')|float <= 0.00 %}
        {{
          states('sensor.stromzaehler_total_power')|float +
          states('sensor.p_ac')|float
        }}
        {% else %}
        {{
          states('sensor.p_ac')|float - 
          states('sensor.stromzaehler_einspeisung_aktuell')|float 
        }}
        {% endif %}

which brings up a error if the value of 'stromzaehler_total_power is negative.

or shortly {{ (states('stromzaehler_total_power')|float >= 0.00) }} gets an error if the value is negative

ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('stromzaehler_total_power')|float >= 0.00) }}' but no default was specified

if I use {{ (states('stromzaehler_total_power')|float(-1) >= 0.00) }} it is working. But this will also render when the sensor becomes unavailable.

So what to do that this template will work correclty with negative values?

Use has_value() to determine if an entity’s value is not unknown or unavailable.

In addition, it’s good practice to provide float() with a default value. If it cannot convert a supplied value to a floating point number, it will use the default value. If there’s no default value, it will generate an error message (like the one that you received).

has_value sounds good. will implement that.

And yes, the default value is important. The sensor has a value, but still uses the default value. But only if the value is negative. I want to use the default value only if there is really no value.

I expect the template to render -153 and not -1

That’s not a default value, that would be a previous value.

You mean the result of the template? If i change the default in float, the template immediately change to the new default.

if I render {{ states('stromzaehler_total_power') }} it renders unknown. So i think there is a problem with the conversion from string to float.
This is the sensor

- platform: template
  sensors:
    stromzaehler_total_power:
      friendly_name: 'Verbrauch aktuell total'
      unit_of_measurement: "W"
      device_class: energy
      value_template: >-
        {{ 
        (states('sensor.stromzaehler_channel_a_power')|float + 
        states('sensor.stromzaehler_channel_b_power')|float + 
        states('sensor.stromzaehler_channel_c_power')|float)|round(2)
         }}

sensor.stromzaehler_channel_a_power comes from a shelly 3EM.

No I mean, the words you’ve been using so far, you’ve been talking about a default value. Not the previous state.

A default value is a static fallback value if something doesn’t occur. It’s not a dynamic value.

In this case you’re using | float which attempts to convert a string to a number. If that conversion fails, you’ll get a default value if you provide a default value. You’re providing -1 as your default value. So when that fails, -1 will be returned.

Ok, maybe we have a problem with my english to explain the problem.

{{ states('stromzaehler_total_power')| float(-1) }}

This should render to the value the sensor shows me in the frontend. As long it can convert the string to float. If not, it will return -1.

But in reality is renders only if the real value is not negative. As soon as it gets negative the conversion from string to float fails with unknown and the sensor uses its default value.

So why is the conversion failing if the value is negative?

Sorry if this gets complicated because of the language.

that’s not a correct entity_id. So that’s your first problem. It most likely is states('sensor.stromzaehler_total_power') | float(-1)

2 Likes

spotted in the first post looking back at that now :wink:

why it would return anything but that default value is a mystery?

In the seconds you wrote this, I discovered it myself.

:scream:

Everything is working now.

I used this whole template for a few days and never get any problems. Until today double checked because of other reasons.
Crazy confused!

There are a few if queries. So the affected query wasn’t active until yesterday and therefore I did not recognized the problem.