TemplateError ZeroDivisionError: float division by zero, what is wrong in my syntax?

Hi all (hi @tom_l :upside_down_face: )

I have this error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: 8:49:31 AM (2 occurrences)
Last logged: 8:49:31 AM

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (((states.sensor.prncyanlvl.state | float) / (states.sensor.prncyancap.state | float)) * 100) | round(1) }}")' for attribute '_state' in entity 'sensor.cyan_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (((states.sensor.prnyellowlvl.state | float) / (states.sensor.prnyellowcap.state | float)) * 100) | round(1) }}")' for attribute '_state' in entity 'sensor.yellow_toner_level'

Coming from these sensors I think:

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: '{{ (((states.sensor.prncyanlvl.state | float) / (states.sensor.prncyancap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prncyanlvl')|float / (states('sensor.prncyancap')|float * 100) )|round(1) }}"
      magenta_toner_level:
        friendly_name: 'Magenta Toner Level'
        unit_of_measurement: '%'
        value_template: '{{ (((states.sensor.prnmagentalvl.state | float) / (states.sensor.prnmagentacap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) )|round(1) }}"
        #value_template: "{{ '{:.1%}'.format( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) ) }}"
      yellow_toner_level:
        friendly_name: 'Yellow Toner Level'
        unit_of_measurement: '%'
        value_template: '{{ (((states.sensor.prnyellowlvl.state | float) / (states.sensor.prnyellowcap.state | float)) * 100) | round(1) }}'
      black_toner_level:
        friendly_name: 'Black Toner Level'
        unit_of_measurement: '%'
        value_template: '{{ (((states.sensor.prnblacklvl.state | float) / (states.sensor.prnblackcap.state | float)) * 100) | round(1) }}'

What can I do to prevent that logging errors?

When a sensor state is unavailable, |float returns 0 by default. You can tell it to return another number, like this |float(3) but thereā€™s a better way. For each of your template sensors define an availability template that checks if the denominator is greater than 0.

Also see the warning here about the format you use to retrieve a sensorā€™s state.

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: "{{ (100 * states('sensor.prncyanlvl') | float / states('sensor.prncyancap') | float ) | round(1) }}"
        availability_template:  "{{ states('sensor.prncyancap') | float > 0 }}"

Finally, please see point 16 here.

Thanks tom_i, I think that did the trick and the error is now gone from the logs!

Thank you, I am a noob when it comes to these type of template sensors languagesā€¦

Sorryā€¦ I did not realize it was offending. I never got to number 16 when reading those. And I am sorry to have tagged you. It was more meant as a ā€œcomplimentā€.

Oh noā€¦ itā€™s backā€¦ I have this error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: February 27, 2021, 9:22:06 PM (8 occurrences)
Last logged: 9:06:45 AM

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnyellowlvl') | float / states('sensor.prnyellowcap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.yellow_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prncyanlvl') | float / states('sensor.prncyancap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.cyan_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnblacklvl') | float / states('sensor.prnblackcap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.black_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnmagentalvl') | float / states('sensor.prnmagentacap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.magenta_toner_level'

And this yaml:

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: "{{ (100 * states('sensor.prncyanlvl') | float / states('sensor.prncyancap') | float ) | round(1) }}"
        availability_template:  "{{ states('sensor.prncyancap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prncyanlvl.state | float) / (states.sensor.prncyancap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prncyanlvl')|float / (states('sensor.prncyancap')|float * 100) )|round(1) }}"
      magenta_toner_level:
        friendly_name: 'Magenta Toner Level'
        unit_of_measurement: '%'
        value_template: "{{ (100 * states('sensor.prnmagentalvl') | float / states('sensor.prnmagentacap') | float ) | round(1) }}"
        availability_template:  "{{ states('sensor.prnmagentacap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnmagentalvl.state | float) / (states.sensor.prnmagentacap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) )|round(1) }}"
        #value_template: "{{ '{:.1%}'.format( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) ) }}"
      yellow_toner_level:
        friendly_name: 'Yellow Toner Level'
        unit_of_measurement: '%'
        value_template: "{{ (100 * states('sensor.prnyellowlvl') | float / states('sensor.prnyellowcap') | float ) | round(1) }}"
        availability_template:  "{{ states('sensor.prnyellowcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnyellowlvl.state | float) / (states.sensor.prnyellowcap.state | float)) * 100) | round(1) }}'
      black_toner_level:
        friendly_name: 'Black Toner Level'
        unit_of_measurement: '%'
        value_template: "{{ (100 * states('sensor.prnblacklvl') | float / states('sensor.prnblackcap') | float ) | round(1) }}"
        availability_template:  "{{ states('sensor.prnblackcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnblacklvl.state | float) / (states.sensor.prnblackcap.state | float)) * 100) | round(1) }}'

Try:

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

Likewise for the other three.

Hm think it still isnā€™t gone with thatā€¦

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: 1:35:20 PM (13 occurrences)
Last logged: 10:22:08 PM

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnmagentalvl') | float / states('sensor.prnmagentacap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.magenta_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnblacklvl') | float / states('sensor.prnblackcap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.black_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prnyellowlvl') | float / states('sensor.prnyellowcap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.yellow_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (100 * states('sensor.prncyanlvl') | float / states('sensor.prncyancap') | float ) | round(1) }}")' for attribute '_state' in entity 'sensor.cyan_toner_level'

You need to write an if statement in the value_template to verify that the denominator is not zero before dividing. The availability template will just change the state to unavailable but the calculation will still occur.

1 Like

I have been puzzling with something like this but I am stuckā€¦ am I in the right direction?

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
        	{% set level = 100 * states('sensor.prncyanlvl') | float %}
        	{set states = states('sensor.prncyancap') | float ) %}
        	{{ 'unknown' if level == 'unknown' or states == 'unknown' else {{ [ level / states |  round(1) }}
        availability_template:  "{{ states('sensor.prncyancap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
1 Like

Any help appreciatedā€¦ log errors drive me crazyā€¦

Youā€™re dividing by zeroā€¦ So what should you be checking for? 0 or unknown?

I am really havin a hard time decoding thisā€¦

Tried:

sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prncyanlvl') | float %}
          {% set states = states('sensor.prncyancap') | float ) %}
          {{ '0' if level == '0' or states == '0' else {{ [ level / states ] | round(1) }}
        availability_template:  "{{ states('sensor.prncyancap') not in ['0', 'unavailable', 'unknown', 'none'] }}"

gives:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['cyan_toner_level']['value_template']. Got "{% set level = 100 * states('sensor.prncyanlvl') | float %} {% set states = states('sensor.prncyancap') | float ) %} {{ '0' if level == '0' or states == '0' else {{ [ level / states ] | round(1) }}\n". (See ?, line ?).

And this:

sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prncyanlvl') | float %}
          {% set states = states('sensor.prncyancap') | float ) %}
          {{ '0' if level == '0' or states == '0' else {{ [ level / states ] | round(1) }}}}
        availability_template:  "{{ states('sensor.prncyancap') not in ['0', 'unavailable', 'unknown', 'none'] }}"

gives:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['cyan_toner_level']['value_template']. Got "{% set level = 100 * states('sensor.prncyanlvl') | float %} {% set states = states('sensor.prncyancap') | float ) %} {{ '0' if level == '0' or states == '0' else {{ [ level / states ] | round(1) }}}}\n". (See ?, line ?).

Bottom lineā€¦ I donā€™t know what I am doingā€¦

You have an errant )

sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prncyanlvl') | float %}
          {% set states = states('sensor.prncyancap') | float ) %}
                                                              ^ ### Delete this

Also thereā€™s no need to test if level is zero. You are allowed to divide 0 by things (answer = 0). Just not divide things by 0.

1 Like

I think youā€™re spending too much time thinking you canā€™t do this, when in actuality this is highschool level math. Dividing any number by zero results in a imaginary number. Imaginary numbers are not real and will result in a divide by zero error.

True! but the math is not the problem here, it is the yaml coding

{ [ " ā€™ tab space and all that :slight_smile:

the rule for those are simple, and text editors help. When you have a {, you need to close it with a }. the same goes for every other character you listed.

Still issueā€¦

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: 3:39:15 PM (20 occurrences)
Last logged: 3:47:46 PM

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prncyanlvl') | float %} {% set states = states('sensor.prncyancap') | float %} {{ level / states | round(1) }}")' for attribute '_state' in entity 'sensor.cyan_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnmagentalvl') | float %} {% set states = states('sensor.prnmagentacap') | float %} {{ level / states | round(1) }}")' for attribute '_state' in entity 'sensor.magenta_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnyellowlvl') | float %} {% set states = states('sensor.prnyellowcap') | float %} {{ level / states | round(1) }}")' for attribute '_state' in entity 'sensor.yellow_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnblacklvl') | float %} {% set states = states('sensor.prnblackcap') | float %} {{ level / states | round(1) }}")' for attribute '_state' in entity 'sensor.black_toner_level'

this is in now:

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prncyanlvl') | float %}
          {% set states = states('sensor.prncyancap') | float %}
          {{ level / states | round(1) }}
        availability_template:  "{{ states('sensor.prncyancap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prncyancap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prncyanlvl.state | float) / (states.sensor.prncyancap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prncyanlvl')|float / (states('sensor.prncyancap')|float * 100) )|round(1) }}"
      magenta_toner_level:
        friendly_name: 'Magenta Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnmagentalvl') | float %}
          {% set states = states('sensor.prnmagentacap') | float %}
          {{ level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnmagentacap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnmagentacap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnmagentalvl.state | float) / (states.sensor.prnmagentacap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) )|round(1) }}"
        #value_template: "{{ '{:.1%}'.format( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) ) }}"
      yellow_toner_level:
        friendly_name: 'Yellow Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnyellowlvl') | float %}
          {% set states = states('sensor.prnyellowcap') | float %}
          {{ level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnyellowcap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnyellowcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnyellowlvl.state | float) / (states.sensor.prnyellowcap.state | float)) * 100) | round(1) }}'
      black_toner_level:
        friendly_name: 'Black Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnblacklvl') | float %}
          {% set states = states('sensor.prnblackcap') | float %}
          {{ level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnblackcap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnblackcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnblacklvl.state | float) / (states.sensor.prnblackcap.state | float)) * 100) | round(1) }}'

Sorry I do not know ā€œhow to do the not dividing by 0 in yamlā€ā€¦

EDIT:
and this is the error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:71
Integration: template (documentation, issues)
First occurred: 4:19:19 PM (13 occurrences)
Last logged: 4:22:39 PM

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnyellowlvl') | float %} {% set states = states('sensor.prnyellowcap') | float %} {{ '0' if level == '0' or states == '0' else level / states | round(1) }}")' for attribute '_state' in entity 'sensor.yellow_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prncyanlvl') | float %} {% set states = states('sensor.prncyancap') | float %} {{ '0' if level == '0' or states == '0' else level / states | round(1) }}")' for attribute '_state' in entity 'sensor.cyan_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnmagentalvl') | float %} {% set states = states('sensor.prnmagentacap') | float %} {{ '0' if level == '0' or states == '0' else level / states | round(1) }}")' for attribute '_state' in entity 'sensor.magenta_toner_level'
TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{% set level = 100 * states('sensor.prnblacklvl') | float %} {% set states = states('sensor.prnblackcap') | float %} {{ '0' if level == '0' or states == '0' else level / states | round(1) }}")' for attribute '_state' in entity 'sensor.black_toner_level'

when I use this:

  - platform: template
    sensors:
      cyan_toner_level:
        friendly_name: 'Cyan Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prncyanlvl') | float %}
          {% set states = states('sensor.prncyancap') | float %}
          {{ '0' if level == '0' or states == '0' else level / states | round(1) }}
        availability_template:  "{{ states('sensor.prncyancap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prncyancap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prncyanlvl.state | float) / (states.sensor.prncyancap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prncyanlvl')|float / (states('sensor.prncyancap')|float * 100) )|round(1) }}"
      magenta_toner_level:
        friendly_name: 'Magenta Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnmagentalvl') | float %}
          {% set states = states('sensor.prnmagentacap') | float %}
          {{ '0' if level == '0' or states == '0' else level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnmagentacap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnmagentacap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnmagentalvl.state | float) / (states.sensor.prnmagentacap.state | float)) * 100) | round(1) }}'
        #value_template: "{{ ( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) )|round(1) }}"
        #value_template: "{{ '{:.1%}'.format( states('sensor.prnmagentalvl')|float / (states('sensor.prnmagentacap')|float * 100) ) }}"
      yellow_toner_level:
        friendly_name: 'Yellow Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnyellowlvl') | float %}
          {% set states = states('sensor.prnyellowcap') | float %}
          {{ '0' if level == '0' or states == '0' else level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnyellowcap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnyellowcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnyellowlvl.state | float) / (states.sensor.prnyellowcap.state | float)) * 100) | round(1) }}'
      black_toner_level:
        friendly_name: 'Black Toner Level'
        unit_of_measurement: '%'
        value_template: >
          {% set level = 100 * states('sensor.prnblacklvl') | float %}
          {% set states = states('sensor.prnblackcap') | float %}
          {{ '0' if level == '0' or states == '0' else level / states | round(1) }}
        availability_template:  "{{ states('sensor.prnblackcap') not in ['0', 'unavailable', 'unknown', 'none'] }}"
        #availability_template:  "{{ states('sensor.prnblackcap') | float > 0 }}"
        #value_template: '{{ (((states.sensor.prnblacklvl.state | float) / (states.sensor.prnblackcap.state | float)) * 100) | round(1) }}'

level is the numerator. states is the denominator.

numerator
---------
denominator

When dividing by zero, the denominator is zero.

Also, quotes around something make a string. So, please tell me, what are you doing wrong with this after what I just said:

{{ '0' if level == '0' or states == '0' else level / states | round(1) }}

Sorryā€¦ I have absolutely no clueā€¦

should I use qoutes around the formula :see_no_evil:?

Like:

{{ '0' if level == '0' or states == '0' else 'level / states' | round(1) }}

Or should it be this?
{{ 0 if level == 0 or states == 0 else level / states | round(1) }}

Seeā€¦ I do not know how to ā€œbuild upā€ the code structure/contextā€¦