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).
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.
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
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.
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.