Availability Template Help - TemplateError ('ZeroDivisionError: float division by zero')

I have a template:

## Percentages in %
      solar_percent:
        unit_of_measurement: '%'
        friendly_name: Current Solar Percentage Realtime
        #availability_template: "{{ ((states('sensor.power_consumption_w')|float(0)) + (states('power_production_solar_total')|float(0))) != 0}}"
        value_template: >
            {% set solar_total_w = states('sensor.power_production_solar_total') | float(0) %}
            {% set production_w = states('sensor.power_production_w') | float(0) %}
            {% set consumption_w = states('sensor.power_consumption_w') | float(0) %}
            {% set consumption_total = states('sensor.power_consumption_total') | float(0) %}
            {{ [ (solar_total_w * 100) / (consumption_w + solar_total_w), 0 ] | max | round(0) }}

I get this error:

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:329
First occurred: 08:26:23 (1 occurrences)
Last logged: 08:26:23

TemplateError('ZeroDivisionError: float division by zero') while processing template 'Template<template=({% set solar_total_w = states('sensor.power_production_solar_total') | float(0) %} {% set production_w = states('sensor.power_production_w') | float(0) %} {% set consumption_w = states('sensor.power_consumption_w') | float(0) %} {% set consumption_total = states('sensor.power_consumption_total') | float(0) %} {{ [ (solar_total_w * 100) / (consumption_w + solar_total_w), 0 ] | max | round(0) }}) renders=26>' for attribute '_attr_native_value' in entity 'sensor.solar_percent'

I am trying (with the help of this topic):
https://community.home-assistant.io/t/templateerror-zerodivisionerror-float-division-by-zero-what-is-wrong-in-my-syntax

to decode what the availability template should look like.
But whatever I try I have no success…

I would like to see an example to extract the logic from it. maybe even a “working” line of YAML if possible… Thanks in advance.

## Percentages in %
      solar_percent:
        unit_of_measurement: '%'
        friendly_name: Current Solar Percentage Realtime
        value_template: >
            {% set solar_total_w = states('sensor.power_production_solar_total') | float(0) %}
            {% set production_w = states('sensor.power_production_w') | float(0) %}
            {% set consumption_w = states('sensor.power_consumption_w') | float(0) %}
            {% set consumption_total = states('sensor.power_consumption_total') | float(0) %}
            {{ [ (solar_total_w * 100) / (consumption_w + solar_total_w), 0 ] | max | round(0) }}
        availability_template: >
            {{ states('sensor.power_production_solar_total') | is_number and
               states('sensor.power_production_w') | is_number and
               states('sensor.power_consumption_w') | is_number and
               states('sensor.power_consumption_total') is_number and
               states('sensor.power_consumption_total') | float(0) + states('sensor.power_consumption_w') | float(0) > 0 }}

Hi Tom,

it’s logically clear this way. Although I get an error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'is_number') for dictionary value @ data['sensors']['solar_percent']['availability_template']. Got "{{ states('sensor.power_production_solar_total') | is_number and states('sensor.power_production_w') | is_number and states('sensor.power_consumption_w') | is_number and states('sensor.power_consumption_total') is_number and states('sensor.power_consumption_total') | float(0) + states('sensor.power_consumption_w') | float(0) > 0 }}\n". (See ?, line ?).

^ that one is missing the filter | symbol berore is_number.

1 Like

Thank you Tom! This will be the basis for a lot of others (for me)

1 Like