Divide by zero despite availabilty template

Hello,

I am trying to get rid of a divide by Zero error, but it’s still there.
Basically I want to calculate the ratio of consumed solar energy. At night production is 0, so that’s a given.

My code is:

        value_template: "{{ (((states('sensor.daily_solar_production')|float(0) -states('sensor.daily_electricity_export_total')|float(0)) / states('sensor.daily_solar_production')|float(1))*100) | round(2) }}"
        availability_template:  "{{ 0 not in [states('sensor.daily_solar_production')|int]}}"

In the modelling tool, it returns “False” as availability so that should work.
Yet I still get error messages in the log

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template("{{ (((states('sensor.daily_solar_production')|float(0) -states('sensor.daily_electricity_export_total')|float(0)) / states('sensor.daily_solar_production')|float(1))*100) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.daily_auto_conso_solar'

What can I do to solve that?
Using a If…Then could work but isn’t that clean is it?

Yes testing for 0 is the way to do it.

        value_template: "{{ none if states('sensor.daily_solar_production')|float(0) == 0 else (((states('sensor.daily_solar_production')|float(0) - states('sensor.daily_electricity_export_total')|float(0)) / states('sensor.daily_solar_production')|float(1))*100) | round(2) }}"
        availability_template:  "{{ states('sensor.daily_solar_production')|float(0) != 0}}"

Now, Frédéric has a point that the template shouldn’t even be rendered if its availability is False, functionaly-wise…

Whatever is using the template sensor should check that before rendering the sensor.

Frédéric, what is “using” the sensor? Is it only for display or is something else using it?

Try this without the changes to the value template:

availability_template: "{{ not is_state('sensor.daily_solar_production', '0') }}"

Hi, thanks for your feedback,

@koying,
Today this sensor is indeed only used for display, but I don’t exclude that I might use it for some automation (notification or whatever else) in the future.

@tom_l
I don’t think my availability_sensor is the issue, I have tried various versions and still have the same issue: even though the sensor shouldn’t be available, HA calculates it and thereby divides by 0.

And does it properly display as “unavailable”?

The availability template does not block rendering of other templates, it just means the entity’s state will be set to unavailable if the availability template returns False .

2 Likes

So, to avoid “divide by zero” errors I have to do that?

        value_template: >-
            {% if is_state('sensor.hourly_solar_production', '0.0') %}
              {{ ('0' | float) }}
            {% else %}
              {{ (((states('sensor.hourly_solar_production')|float(0) -states('sensor.hourly_electricity_export_total')|float(0)) / states('sensor.hourly_solar_production')|float(1))*100) | round(2) }}
            {% endif %}

If you wish, you can reduce it by using a variable and an inline if statement:

        value_template: >-
          {% set hsp = states('sensor.hourly_solar_production')|float(0) %}
          {{ 0 if hsp == 0 else (((hsp - states('sensor.hourly_electricity_export_total')|float(0)) / hsp) * 100)|round(2) }}
1 Like

FYI

I thought you might be interested in this very recent PR:

If I understand it correctly, it intends to implement new behavior where if the availability_template evaluates to false, it will block rendering of other templates in the entity (i.e. its state/value_template).

2 Likes