Need help with template error (trying to do math in template sensor but how to add "none")

Hello all,

Could someone help me with the proper way to give the result of some math into a template sensor. The issue is I believe one or more of my sensors that are being added have a value of none? Are zero and none the same thing??

I should also add, I don’t know what I am doing so I am going to provide what I know and hope you guys can help me determine how to get it done.

I would also like to divide the results of the below by 25.4 because the input sensors are in mm but I would like the end of the math results to be in inches.

Thank you all!

Below are the 3 sensors I am trying to make:

      previous_rainfall:
        friendly_name: "Past 48h Rainfall"
        unit_of_measurement: "in"
        value_template: "{{ state_attr('sensor.rainfactor2', 'day0rain') | float  + state_attr('sensor.rainfactor2', 'day1rain') | float  }}"
      previous_rainfall_five_day:
        friendly_name: "Past 5d Rainfall"
        unit_of_measurement: "in"
        value_template: "{{ state_attr('sensor.rainfactor2', 'day0rain') | float  + state_attr('sensor.rainfactor2', 'day1rain') | float + state_attr('sensor.rainfactor2', 'day2rain') | float  + state_attr('sensor.rainfactor2', 'day3rain') | float + state_attr('sensor.rainfactor2', 'day4rain') | float  }}"
      daily_rainfall:
        friendly_name: "Daily Rainfall"
        unit_of_measurement: "in"
        value_template: "{{ state_attr('sensor.rainfactor2', 'day0rain') | float  }}"

Below is the error I am getting in the logs:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:197
integration: Template (documentation, issues)
First occurred: June 8, 2024 at 10:00:46 PM (3 occurrences)
Last logged: June 8, 2024 at 10:00:46 PM

TemplateError('ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('sensor.rainfactor2', 'day0rain') | float + state_attr('sensor.rainfactor2', 'day1rain') | float }}' but no default was specified') while processing template 'Template<template=({{ state_attr('sensor.rainfactor2', 'day0rain') | float + state_attr('sensor.rainfactor2', 'day1rain') | float }}) renders=6>' for attribute '_attr_native_value' in entity 'sensor.previous_rainfall'
TemplateError('ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('sensor.rainfactor2', 'day0rain') | float + state_attr('sensor.rainfactor2', 'day1rain') | float + state_attr('sensor.rainfactor2', 'day2rain') | float + state_attr('sensor.rainfactor2', 'day3rain') | float + state_attr('sensor.rainfactor2', 'day4rain') | float }}' but no default was specified') while processing template 'Template<template=({{ state_attr('sensor.rainfactor2', 'day0rain') | float + state_attr('sensor.rainfactor2', 'day1rain') | float + state_attr('sensor.rainfactor2', 'day2rain') | float + state_attr('sensor.rainfactor2', 'day3rain') | float + state_attr('sensor.rainfactor2', 'day4rain') | float }}) renders=6>' for attribute '_attr_native_value' in entity 'sensor.previous_rainfall_five_day'
TemplateError('ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('sensor.rainfactor2', 'day0rain') | float }}' but no default was specified') while processing template 'Template<template=({{ state_attr('sensor.rainfactor2', 'day0rain') | float }}) renders=6>' for attribute '_attr_native_value' in entity 'sensor.daily_rainfall'

Below is the sensor attributes I am trying to add together:

None and 0 are not the same thing…

The first thing I would do would be to add availability templates for each sensor. This will prevent the integration from trying to render the template when the sensor it references doesn’t have a value.

The second thing would be to add default values to the float filters.

To perform division on the sum of multiple values you just need to use parentheses to surround the elements that are being added together:

{{ ( a + b + c ) / 25.4 }}

      previous_rainfall:
        friendly_name: "Past 48h Rainfall"
        unit_of_measurement: "in"
        value_template: "{{ state_attr('sensor.rainfactor2', 'day0rain') | float(0)  + state_attr('sensor.rainfactor2', 'day1rain') | float(0)  }}"
        availability_template: "{{ has_value('sensor.rainfactor2') }}"
      previous_rainfall_five_day:
        friendly_name: "Past 5d Rainfall"
        unit_of_measurement: "in"
        value_template: |
          {% set s = 'sensor.rainfactor2' %}
          {{ (state_attr(s, 'day0rain') | float(0)  + state_attr(s, 'day1rain') | float(0) + 
          state_attr(s, 'day2rain') | float(0)  + state_attr(s, 'day3rain') | float(0) + 
          state_attr(s, 'day4rain') | float) / 25.4  }}"
        availability_template: "{{ has_value('sensor.rainfactor2') }}"
      daily_rainfall:
        friendly_name: "Daily Rainfall"
        unit_of_measurement: "in"
        value_template: "{{ state_attr('sensor.rainfactor2', 'day0rain') }}"
        availability_template: "{{ has_value('sensor.rainfactor2') }}"

Based on the provided image, an alternative template for “Past 5d Rainfall” would be:

{{ (states.sensor.rainfactor2.attributes).items() 
| selectattr(0, 'match', 'day')| map( attribute=1) | sum / 25.4 }}

No

You could provide defaults to your | float like this: | float(0)
But at the end of the day, that’s still a wrong value.

Best is to implement availability in your templates, so that the sensor will be unavailable if state_attr('sensor.rainfactor2', 'day0rain') is not a proper numeral.

1 Like

Your templates should have had no trouble adding the values shown in your screenshot. day0rain and day1rain are simply 0 (not none which means no value at all but the numeric value zero).

image

The fact that there was an error suggest two possibilities:

  1. Sometimes the value of those attributes is in fact none.
  2. Sometimes those two attributes don’t exist because state_attr() will report none if given a non-existing attribute.

In either case, the attributes value is effectively “missing” (i.e. no value).

How to handle either possibility depends on what you want Home Assistant to report when it encounters them.

  • If you use float(0) in the template, it effectively masks the attributes problem and uses 0 in place of the missing attributes value to perform the addition.

  • If you use the availability_template option, the sensor’s value will be unavailable when it encounters a missing attributes value.