Template platform issue or bug in 0.115.2?

I have a template sensor that tells me what the share of electricity generated by the PV panels is that is used by my electric heater. Last winter it worked fine but now I’m all of a sudden getting errors (0.115.2)

Here’s the error msgs and the yaml for the sensor. Weird thing is in the Dev > template section the value is correctly computed!

Logger: homeassistant.helpers.event
Source: helpers/template.py:285
First occurred: 20:36:46 (2 occurrences)
Last logged: 20:36:46

Error while processing state changed for sensor.verwarming_electricity_kwh_month
Error while processing state changed for sensor.solaredge_energy_this_month_kwh

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/event.py”, line 228, in _async_state_change_dispatcher
hass.async_run_job(action, event)
File “/usr/src/homeassistant/homeassistant/core.py”, line 384, in async_run_job
target(*args)
File “/usr/src/homeassistant/homeassistant/helpers/event.py”, line 695, in _refresh
self._info[template] = template.async_render_to_info(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 306, in async_render_to_info
render_info._result = self.async_render(variables, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render
return compiled.render(kwargs).strip()
File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/usr/local/lib/python3.8/site-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
ZeroDivisionError: float division by zero

Logger: homeassistant.components.sensor
Source: helpers/template.py:285
Integration: Sensor (documentation, issues)
First occurred: 20:36:47 (1 occurrences)
Last logged: 20:36:47

Error while setting up template platform for sensor

Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 201, in _async_setup_platform await asyncio.gather(*pending) File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 310, in async_add_entities await asyncio.gather(*tasks) File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 481, in _async_add_entity await entity.add_to_platform_finish() File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 521, in add_to_platform_finish await self.async_added_to_hass() File “/usr/src/homeassistant/homeassistant/components/template/sensor.py”, line 151, in async_added_to_hass await super().async_added_to_hass() File “/usr/src/homeassistant/homeassistant/components/template/template_entity.py”, line 290, in async_added_to_hass await self._async_template_startup() File “/usr/src/homeassistant/homeassistant/components/template/template_entity.py”, line 264, in _async_template_startup result_info = async_track_template_result( File “/usr/src/homeassistant/homeassistant/helpers/event.py”, line 792, in async_track_template_result tracker.async_setup() File “/usr/src/homeassistant/homeassistant/helpers/event.py”, line 518, in async_setup self._info[template] = template.async_render_to_info(variables) File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 306, in async_render_to_info render_info._result = self.async_render(variables, **kwargs) File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render return compiled.render(kwargs).strip() File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 1090, in render self.environment.handle_exception() File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 832, in handle_exception reraise(*rewrite_traceback_stack(source=source)) File “/usr/local/lib/python3.8/site-packages/jinja2/_compat.py”, line 28, in reraise raise value.with_traceback(tb) File “”, line 1, in top-level template code ZeroDivisionError: float division by zero

        verwarming_share_month:
            friendly_name: 'Verwarming Electricity Share'
            unit_of_measurement: '%'
            value_template: "{{ (states('sensor.verwarming_electricity_kwh_month') | float / states('sensor.solaredge_energy_this_month_kwh') | float * 100 ) | round(2) }}"

It’s not a bug. When the system starts up states('sensor.solaredge_energy_this_month_kwh') is ‘unknown’. You then turn the result into a float, which makes it zero. Then you go and divide by zero, which is a no-no in math.

So, build an if statement that checks to see if that result is zero or unknown, pick your poison. Zero is probably easier.

As an FYI, when you’re reading a template error that is a bunch of code, read the last line:

Thanks petro, I know it was a division by zero. The question was how to resolve the effect at startup :slight_smile:

I also have a very similar issue that just started recently ( version >0.115.x). It appears something has changed in Home Assistant affecting templates on startup if a template is trying to calculate with a sensor that temporarily shows as unknown.