Unknown sensor calculation causing error

I have a value template that seems to be causing me some problems when one of the sensors is ‘unknown’.

value_template: '{{ ((states("sensor.torana_available_charging_current")|float + states("sensor.torana_tesla_watts")|float)/(states("sensor.grid_voltage")|float) - 2|float)|round(0) }}'

I know that sensor.torana_tesla_watts is going to be unknown when it isn’t powered on so how do effectively treat this? I expect somehow I need to tell the value_template that when unknown make it zero or another way. What is the best way to do this?

I notice this within home_assistant.log

2021-11-20 09:40:17 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '{{ ((states("sensor.torana_available_charging_current")|float + states("sensor.torana_tesla_watts")|float)/(states("sensor.grid_voltage")|float) - 2|float)|round(0) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

First, you need to supply each instance of float with a default value. I have chosen 0 but you can select whatever value you think is appropriate.

value_template: '{{ ((states("sensor.torana_available_charging_current")|float(0) + states("sensor.torana_tesla_watts")|float(0))/(states("sensor.grid_voltage")|float;0)) - 2|float;0))|round(0) }}'

Second, you should enhance your Template Sensor’s configuration with an availability_template. Minimally, I suggest this because you said “I know that sensor.torana_tesla_watts is going to be unknown when it isn’t powered on”.

availability_template: '{{ states("sensor.torana_tesla_watts") not in ["unavailable", "unknown"] }}'

If the state value of sensor.torana_tesla_watts is either unavailable or unknown, the template reports false. As a result, the value_template is not evaluated.