Division by zero error - when sensor is probably not yet available

Hi all,

at the moment I am facing an issue with a division by zero in my logs.
The sensor which is causing the division by zero is “maxdiskspace” - and the value will be received from a NodeRed flow.

I do know that the sensor template itself is working, because I do get a correct value displayed - I asume, that the division by zero just happens after a restart of HomeAssistant - when the NodeRed flow was not yet triggered.

I’ve tried to prevent the division by zero with an IF / ELSE statement - but it seems not working as expected - and I can’t see why… ?!

Can you please have a look at my template and let me know, where I might have done a mistake?

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:72
Integration: Template (documentation, issues)
First occurred: 14:20:12 (5 occurrences)
Last logged: 14:20:12

    TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% if is_state('sensor.lxc_01_maxdisksize', '0') or is_state('sensor.lxc_01_diskusage', '0') %} 0.0 {% else %} {% set maxdiskspace = states('sensor.lxc_01_maxdisksize') | float %} {% set useddiskspace = states('sensor.lxc_01_diskusage') | float %} {{ (( useddiskspace / maxdiskspace) | float * 100) | round(2) }} {% endif %}")' for attribute '_attr_native_value' in entity 'sensor.samba_used_disk_space_percentage'

and this is the template, causing the error:

# Proxmox Monitoring Calc-Free Disk-Space
    samba_used_disk_space_percentage:
      friendly_name: 'LXC-01 used Disk space %'
      value_template: >
        {% if is_state('sensor.lxc_01_maxdisksize', '0') or is_state('sensor.lxc_01_diskusage', '0') %}
          0.0
        {% else %}
          {% set maxdiskspace = states('sensor.lxc_01_maxdisksize') | float %}
          {% set useddiskspace = states('sensor.lxc_01_diskusage') | float %}
          {{ (( useddiskspace / maxdiskspace) | float * 100) | round(2) }}
        {% endif %}
      unit_of_measurement: '%'
      availability_template:  "{{ states('sensor.lxc_01_maxdisksize') not in ['0', 'unavailable', 'unknown', 'none'] }}"
value_template: >
  {% set maxdiskspace = states('sensor.lxc_01_maxdisksize') | float %}
  {% set useddiskspace = states('sensor.lxc_01_diskusage') | float %}
  {% if ((maxdiskspace == 0) or (useddiskspace == 0)) %}
    0.0
  {% else %}
    {{ (( useddiskspace / maxdiskspace) | float * 100) | round(2) }}
  {% endif %}

It’s worth a try?

Thanks, it seems to work with this code :slight_smile:

Just did a quick test in dev tools:

{% set t = 'test' %}
{{ t | float }}

returns 0, so in theory - a state that is ‘unknown’ should resolve to 0 :slight_smile:

With the update to Home Assistant - you will need to ensure you add the default option to the float filters to prevent log errors when the sensor is unavailable:

{% set maxdiskspace = states('sensor.lxc_01_maxdisksize') | float(default=0) %}
{% set useddiskspace = states('sensor.lxc_01_diskusage') | float(default=0) %}
1 Like

thanks :slight_smile:
Without your comment, I would not have seen the update yet :smiley:

1 Like