Sensor template yields "unknown" in configuration, but works in dev tool / template

I am trying to calculate the efficiency of my air exchanger. I have 3 temperature sensor:

  - platform: template
    sensors:
      airexchangerefficiency:
        friendly_name: "Éch Air Eff"
        value_template: "{{ ((states('sensor.acurite_tower_14889_t')|float-states('sensor.outsidetempnotfiltered')|float)/(states('sensor.insidetempnotfiltered')|float-states('sensor.outsidetempnotfiltered')|float)*100)|round(1) }}"
        unit_of_measurement: "%"

The above works in dev tools / template:

But not after restarting HA:

I have tried keeping only the uppermost portion of the divide; it works. I also kept only the denominator; it works. Something is wrong with the divide function, but I can’t figure it out.

Can you help spot the problem?

Anything in logs? After restarting Home Assistant there is a better than good chance that the state of one or more sensors when this template is being rendered was ‘unknown’ or ‘unavailable’ and as such we started being warned multiple releases ago, that the behaviour of things like float and int, were changing, instead of silently failing to convert text to a number, as of November, Home Assistant will now not render the template if you haven’t included the default.

        value_template: "{{ ((states('sensor.acurite_tower_14889_t')|float(0)-states('sensor.outsidetempnotfiltered')|float(0))/(states('sensor.insidetempnotfiltered')|float(0)-states('sensor.outsidetempnotfiltered')|float(0))*100)|round(1) }}"

Now any value that can’t be converted to a number, will be default to 0

1 Like

Changing to |float(0) did not change the “unknown” status, however I found this in the logs:

2021-12-21 00:25:34 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 261, in _async_template_startup
    result_info = async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 933, in async_track_template_result
    tracker.async_setup(raise_on_template_error)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 740, in async_setup
    self._info[template] = template.async_render_to_info(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 401, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 332, 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 "<template>", line 1, in top-level template code
ZeroDivisionError: float division by zero

So I assume one of the sensors is “unknown” at startup and the division permanently breaks this sensor, without recovering … That doesn’t sound like a logic behavior …

Tried with |float(1) instead to avoid dividing by zero if the sensor is unavailable, but the division by zero still shows in the log. I don’t get it?

Removing the sensor altogether and the error disappears from the logs. so the division by zero is coming from that sensor.

Found the solution:

value_template: "{{ ((states('sensor.acurite_tower_14889_t')|float(1)-states('sensor.temp_now')|float(2))/(states('sensor.insidetempnotfiltered')|float(3)-states('sensor.temp_now')|float(4))*100)|round(1) }}"

Thanks for the help ! The division by zero is breaking the sensor if it occurs once. Good to know.