Need help with a template error, which occurs rarely on some template sensors

Hello,

I have multiple template sensors in action. The onse I have issues with are still written in the “old format”. As I do calcluations, I tried to check, if the sensor state is “unknown” before doing the calculations. My testing in the template showed, that this works at least, if the required state is unavailable.
Nevertheless, the following error still shows up from time to time in which the “unknown” state is still not captured as it seems.
If someone has an idea how to check better for state/sensor availablility - I would be happy to receive them :-):

Log extract:

2023-09-10 00:01:32.284 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_daily_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = states('sensor.electricity_price_base') | float * 12 / 365 | round(2) %}
  {% set consumption_cost =  states('sensor.energy_daily_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}' but no default was specified') while processing template 'Template<template=({% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_daily_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = states('sensor.electricity_price_base') | float * 12 / 365 | round(2) %}
  {% set consumption_cost =  states('sensor.energy_daily_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}) renders=3592>' for attribute '_attr_native_value' in entity 'sensor.electricity_costs_daily'
2023-09-10 00:01:32.306 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_monthly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = states('sensor.electricity_price_base') | float | round(2) %}
  {% set consumption_cost =  states('sensor.energy_monthly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}' but no default was specified') while processing template 'Template<template=({% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_monthly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = states('sensor.electricity_price_base') | float | round(2) %}
  {% set consumption_cost =  states('sensor.energy_monthly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}) renders=3588>' for attribute '_attr_native_value' in entity 'sensor.electricity_costs_monthly'
2023-09-10 00:01:32.316 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_yearly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = now().month * states('sensor.electricity_price_base') | float | round(2) %}
  {% set consumption_cost =  states('sensor.energy_yearly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}' but no default was specified') while processing template 'Template<template=({% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_yearly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
  {% set base_cost = now().month * states('sensor.electricity_price_base') | float | round(2) %}
  {% set consumption_cost =  states('sensor.energy_yearly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
  {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
{% else %}
  {{ None }}
{% endif %}) renders=7334>' for attribute '_attr_native_value' in entity 'sensor.electricity_costs_yearly'

Template YAML:

platform: template
sensors:
  electricity_price_kwh:
    device_class: monetary
    friendly_name: Strompreis (kWh)
    unit_of_measurement: "€/kWh"
    unique_id: electricity_price_kwh
    value_template: >
      {% if states('input_number.electricity_price_kwh') != 'unknown' %}
        {% set price = states('input_number.electricity_price_kwh') %}
        {{ price }}
      {% else %}
        unknown
      {% endif %}
  electricity_price_base:
    device_class: monetary
    friendly_name: Stromgrundpreis (Monat)
    unit_of_measurement: "EUR"
    unique_id: electricity_price_base
    value_template: >
      {% if states('input_number.electricity_price_base') != 'unknown' %}
        {% set price = states('input_number.electricity_price_base') %}
        {{ price }}
      {% else %}
        unknown
      {% endif %}
  electricity_costs_daily:
    device_class: monetary
    friendly_name: Stromkosten (Tag)
    unit_of_measurement: "EUR"
    unique_id: electricity_costs_daily
    value_template: >
      {% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_daily_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
        {% set base_cost = states('sensor.electricity_price_base') | float * 12 / 365 | round(2) %}
        {% set consumption_cost =  states('sensor.energy_daily_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
        {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
      {% else %}
        {{ None }}
      {% endif %}
  electricity_costs_monthly:
    device_class: monetary
    friendly_name: Stromkosten (Monat)
    unit_of_measurement: "EUR"
    unique_id: electricity_costs_monthly
    value_template: >
      {% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_monthly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
        {% set base_cost = states('sensor.electricity_price_base') | float | round(2) %}
        {% set consumption_cost =  states('sensor.energy_monthly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
        {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
      {% else %}
        {{ None }}
      {% endif %}
  electricity_costs_yearly:
    device_class: monetary
    friendly_name: Stromkosten (Jahr)
    unit_of_measurement: "EUR"
    unique_id: electricity_costs_yearly
    value_template: >
      {% if states('sensor.electricity_price_base') != 'unknown' and states('sensor.energy_yearly_standard') != 'unknown' and states('sensor.electricity_price_kwh') != 'unknown'%}
        {% set base_cost = now().month * states('sensor.electricity_price_base') | float | round(2) %}
        {% set consumption_cost =  states('sensor.energy_yearly_standard') | float * states('sensor.electricity_price_kwh') | float | round(2) %}
        {{ '%.2f' | format(base_cost + consumption_cost | round(2) ) }}
      {% else %}
        {{ None }}
      {% endif %}

Background: Our electricity tariff is covered by a monthly base fee and costs per kwh. I want to calculate the costs per day, month and year. I can set the base fee and cost per kwh in a dashboard via "input_number"s…
Probably I dont need the first two sensors and can use the value from the input_numbers directly in the calculations below. Calculations worked. Just want to get rid of the error.

Default values were introduced in 2021.10. Petro had written a recommandable post here.

Try to give the |float and |round filters a default value that fits to your needs, e.g.


|round(0,default=0)

and

|float(100)
|float('desired value', default='fallback value')

https://www.home-assistant.io/docs/configuration/templating#numeric-functions-and-filters

1 Like

Thanks for pointing me towards this. I added this already now!!!
Still dont know, why the error occurs as I tried to catch “unknown” values prior…

I’m not using any energy or price sensors, but I could imagine that an integer instead of {{ None }} would fit better.

Also see the possibility of an availability template

The reason was, that I either wanted the correct price to be shown or nothing. In case there is an issue due to whatever :slight_smile: - and the calculation fails, I found it better fitting to show „unavailable“ or set the sensor to „None“.

I understand your proposal. For me this would be a fallback if I cannot make the other way work priperly :-).

Thanks again for your feedback!!!