Oil Sensor template

hello,
i have create an oil sensor with custom 3d print.

I am also using a template to calculate the oil which is left it tank.

  - platform: template      
    sensors:
      oil_in_tank_height:
        friendly_name: "Oil left in tank (cm)"
        unit_of_measurement: 'cm'
        value_template: "{{ (102.0 - float(states('sensor.oil_level_sensor'))) | round(2) }}"
        icon_template: mdi:arrow-expand-vertical

The whole project works smoothly and without problems.
It works great and it gives me what I want the most of the times.
The calculation is nearly always very accurate.

The only problem I face is when I restart HA I get the following error at log files:

Logger: homeassistant.helpers.event
Source: helpers/template.py:400
First occurred: 23:25:48 (2 occurrences)
Last logged: 23:25:48

Error while processing template: Template("{{ (102.0 - float(states('sensor.oil_level_sensor'))) | round(2) }}")

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'float' and 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'float' and 'str'

and then exactly at the next log file I get this

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:73
Integration: Template (documentation, issues)
First occurred: 23:25:48 (2 occurrences)
Last logged: 23:25:48

TemplateError('TypeError: unsupported operand type(s) for -: 'float' and 'str'') while processing template 'Template("{{ (102.0 - float(states('sensor.oil_level_sensor'))) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.oil_in_tank_height'

My question is if it is possible to prevent this error each time I reboot HA ???
Any help would be appreciated.

Try this:

 value_template: "{{ (102.0 - states('sensor.oil_level_sensor')|float(none) )| round(2, default = none) }}"

tried it
it gives the following:

Logger: homeassistant.helpers.event
Source: helpers/template.py:400
First occurred: 01:25:50 (1 occurrences)
Last logged: 01:25:50

Error while processing template: Template("{{ (102.0 - states('sensor.oil_level_sensor')|float(none) )| round(2, default = none) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

Ugh. Try this:

 value_template: >
  {% set oil_level = states('sensor.oil_level_sensor')|float(none) %}
  {% if oil_level != none %}
    {{ (102.0 - oli_level )|round(2) }}"
  {% else %}
    none
  {% endif %}

Or in a more compact form

 value_template: >
  {% set oil_level = states('sensor.oil_level_sensor')|float(none) %}
  {{ (102.0 - oli_level )|round(2) if oil_level != none else none }}

This did the job! Thank you