Template error - unsupported operand type(s) for +

Hi,
Can someone help me with the following error? it looks like + is not supported but the sensor works?

TemplateError('TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'') while processing template 'Template<template=({{ state_attr('sensor.solax_rest_cloud', 'powerdc1') + state_attr('sensor.solax_rest_cloud', 'powerdc2') }}) renders=72>' for attribute '_attr_native_value' in entity 'sensor.solax_cloud_solar_panel'

I already added the availability, but that didn’t solve the issue.

sensor:
    - name: "Solax Cloud Solar Panel"
      state: >-
        {{ state_attr('sensor.solax_rest_cloud', 'powerdc1') + state_attr('sensor.solax_rest_cloud', 'powerdc2') }}
      unit_of_measurement: "Wh"
      availability: >-
        {{ states('sensor.solax_rest_cloud') not in ['unavailable', 'none', 'unknown'] }}

when I test the state in the template tester, the same as the sensor uses but also seperate, the result is:

Can powerdc2 be the cause? which is almost 0 all the time?

{{ state_attr('sensor.solax_rest_cloud', 'powerdc1')|float(0) + state_attr('sensor.solax_rest_cloud', 'powerdc2')|float(0) }}

States and attributes are strings by default, you need to cast them to float or int to do math, and you need to provide a default value in case it’s not available or contains a value that can’t be converted. So we use float(0) which means if it can’t be converted it will just be 0.

Actually, just states. Attributes can be any object type.

‘NoneType’ and ‘NoneType’’ implies the attributes were unknown at some point.

A better availability template would be:

      availability: >
        {{ state_attr('sensor.solax_rest_cloud', 'powerdc1')|is_number and
           state_attr('sensor.solax_rest_cloud', 'powerdc2')|is_number }}
1 Like

The new ‘add’ might be useful here

Filter add(arg) will convert the input to a number and add it to arg. Useful in list operations in conjunction with map.

Templating - Home Assistant.

Nah. This is a simple addition of two sensors. No selecting and mapping.

1 Like