Hello,
I’m struggling with a sensor could anyone help?
When HA boots it shows up this the logs below ( I know these sensors can take 3-4 minutes to show up so the sensors might show up as “unavailable” to start with.
What would I need to change to fix this?
TemplateError('TypeError: '>' not supported between instances of 'str' and 'int'') while processing template 'Template("{{ '-{}'.format(states('sensor.batttery_discharge')) if states('sensor.batttery_discharge')|round(1) > 0 else '+{}'.format(states('sensor.battery_charge'))| float(0) }}")' for attribute '_attr_native_value' in entity 'sensor.battery_flow'
Template warning: 'round' got invalid input 'unavailable' when rendering template '{{ '-{}'.format(states('sensor.batttery_discharge')) if states('sensor.batttery_discharge')|round(1) > 0 else '+{}'.format(states('sensor.battery_charge'))| float(0) }}' but no default was specified. Currently 'round' will return 'unavailable', however this template will fail to render in Home Assistant core 2022.1```
{{ '-{}'.format(states('sensor.batttery_discharge'))
if states('sensor.batttery_discharge')|round(1) > 0 else
'+{}'.format(states('sensor.battery_charge'))| float(0) }}"
Your template appears to be designed to display the batttery_discharge value with a negative sign (-) when the value is positive and with a positive sign (+) when it’s negative.
Hi yes, it was a typo. I just noticed that when I was checking!
The template should take 2 sensors, charge and discharge and show + or - depending on what it’s doing. I’m trying to use it to display the flow of my battery, it does work but it just shows in the logs as it has an issue and that what confuses me.
The template you posted references only one sensor (sensor.battery_discharge). What’s this second sensor you are referring to?
Because the original template can fail if sensor.battery_discharge doesn’t exist on startup. The failure point is the numeric comparison it performs here:
if states('sensor.battery_discharge')|round(1) > 0
If the sensor doesn’t exist, the result of states() will be unknown. The round function can’t do anything with the string unknown so the template proceeds to compare if unknown is greater than zero. That’s an illegal operation and that’s why the error message states:
‘>’ not supported between instances of ‘str’ and ‘int’
To avoid that error message, you can either use the template I posted or add another filter to your template like this:
if states('sensor.battery_discharge') | float(0) | round(1) > 0
If the value of sensor.battery_discharge is unknown then the result of float(0) will be zero. the comparison will not fail because it will be comparing if zero is greater than zero. The result will be false and your template will report -unknown.
If you don’t want it to display -unknown then you need to enhance the Template Sensor with an availability template. The idea is that if the value of sensor.battery_discharge is unknown the Template Sensor will report unavailable (instead of -unknown).
So I added in as you suggested and that fixed the issue:
battery_flow:
friendly_name: “Battery Flow (Live)”
unit_of_measurement: ‘W’
value_template: “{{ ‘-{}’.format(states(‘sensor.batttery_discharge’)) if states(‘sensor.batttery_discharge’)| float(0) | round(1) > 0 else ‘+{}’.format(states(‘sensor.battery_charge’)) }}”
I didn’t know about the availability template! I will do some reading up on that today!
Thanks so much for taking the time to explain so well!