Configure total increasing template to avoid "float got invalid input" template error

Hi

I have a total increasing template sensor to calculate the total electrical energy consumed in my house, just adding the energy values from different sensors I have installed. This is the code

{% set alumbrado = states('sensor.energia_circuito_alumbrado') | float %}
{% set cocina = states('sensor.energia_circuito_cocina') | float %}
{% set otros = states('sensor.energia_circuito_otros') | float %}
{% set lavadora = states('sensor.energia_circuito_lavadora') | float %}
{% set secadora = states('sensor.energia_circuito_secadora') | float %}
{% set lavavajillas = states('sensor.energia_circuito_lavavajillas') | float %}

{{ alumbrado + cocina + otros + lavadora + secadora + lavavajillas| round(2)}}

As I have not configured the default value for the float, I’m getting the “template error: Float got invalid input” but my question is

Can a set the default value to 0? what happen to the template value when any of the variables is getting the invalid value?

That will likely make your total energy decrease which will stuff up the energy dashboard. A better option is to use an availability template:

state: >
    {% set alumbrado = states('sensor.energia_circuito_alumbrado') | float %}
    {% set cocina = states('sensor.energia_circuito_cocina') | float %}
    {% set otros = states('sensor.energia_circuito_otros') | float %}
    {% set lavadora = states('sensor.energia_circuito_lavadora') | float %}
    {% set secadora = states('sensor.energia_circuito_secadora') | float %}
    {% set lavavajillas = states('sensor.energia_circuito_lavavajillas') | float %}
    
    {{ alumbrado + cocina + otros + lavadora + secadora + lavavajillas| round(2)}}
availability: >
    {{ has_value('sensor.energia_circuito_alumbrado') and
       has_value('sensor.energia_circuito_cocina') and
       has_value('sensor.energia_circuito_otros') and
       has_value('sensor.energia_circuito_lavadora') and
       has_value('sensor.energia_circuito_secadora') and
       has_value('sensor.energia_circuito_lavavajillas') }}

See: Why an availability template is important for energy template sensors

Hi

Using your code I’m getting this error:

Sensor None has device class ‘energy’, state class ‘total_increasing’ unit ‘kWh’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘1982.8999999999999 availability: > True’ (<class ‘str’>)

Availability cannot be configured in the template sensor editor and has to be configured directly in the yaml?

Please share the full yaml of your sensor.

Edit: are you using the GUI?

You can not put an availability template inside the state template. It is a separate option and you will have to use YAML.

Yes, I’m using the GUI

Ok, so now another question. I have checked my configuration.yaml and the templates configured in the GUI don’t appear there as code. So, Do I need to delete the GUI sensor and replicate it in the YAML or can I create the sensor with the same name and configure it in YAML?
If a delelte the GUI template, what happens with the historic data?

Yes.

Sorry, there is still a pending question.

I think is relevent, even more for a energy measurement

Nothing happens to it. It stays in the database until it is purged.

1 Like