RESOLVED - Issue with my sensor.yaml and value_template

Hello,
I struggling with the following in my sensors.yaml file - It works fine right now but it’s showing errors in the log file at boot.


lux_battery_flow:
      friendly_name: "Lux - Battery Flow (Live)"
      unit_of_measurement: 'W'
      value_template: "{{ '-{}'.format(states('sensor.lux_batttery_discharge')) if states('sensor.lux_batttery_discharge')|round > 0 else '+{}'.format(states('sensor.lux_battery_charge')) }}"

In the logs it shows:

TemplateError('TypeError: '>' not supported between instances of 'str' and 'int'') while processing template 'Template("{{ '-{}'.format(states('sensor.lux_batttery_discharge')) if states('sensor.lux_batttery_discharge')|round > 0 else '+{}'.format(states('sensor.lux_battery_charge')) }}")' for attribute '_attr_native_value' in entity 'sensor.lux_battery_flow'

2:08:13 PM – (ERROR) Template

Template warning: 'round' got invalid input '' when rendering template '{{ '-{}'.format(states('sensor.lux_batttery_discharge')) if states('sensor.lux_batttery_discharge')|round > 0 else '+{}'.format(states('sensor.lux_battery_charge')) }}' but no default was specified. Currently 'round' will return '', **however this template will fail to render in Home Assistant core 2022.1**

2:08:13 PM – (WARNING) helpers/template.py - message first occurred at 2:08:12 PM and shows up 2 times

Error while processing template: Template("{{ '-{}'.format(states('sensor.lux_batttery_discharge')) if states('sensor.lux_batttery_discharge')|round > 0 else '+{}'.format(states('sensor.lux_battery_charge')) }}")

Could anyone help me correct this? I’m stuck :tired_face:

I guess your states('sensor.lux_batttery_discharge') returns a string.

TemplateError('TypeError: '>' not supported between instances of 'str' and 'int'')

I’d suggest trying: states('sensor.lux_batttery_discharge')|float(default=0) > 0

1 Like

A better alternative is to define the availability of the template sensor. If it’s unavailable, the value template won’t be evaluated

lux_battery_flow:
      friendly_name: "Lux - Battery Flow (Live)"
      unit_of_measurement: 'W'
      value_template: "{{ '-{}'.format(states('sensor.lux_batttery_discharge')) if states('sensor.lux_batttery_discharge')|round > 0 else '+{}'.format(states('sensor.lux_battery_charge')) }}"
      availability_template: "{{ states('sensor.lux_batttery_discharge') | is_number }}"
1 Like

Chris - That is absolutely awesome, that fixed the issue. Thanks!

That makes sense, thanks!