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.