Error with custom sensors

Hi All,
I previously (years ago) posted about creating a custom sensor which included lots of sensors. This was to have a combined temperature and humidity sensor, based on multiple individual sensors (thread).

Ultimately this has been giving me errors and i have just been too busy to look at it till now. I have found through reading the doco, that i had formatting issues and other issues with the sensors from that post.

Ultimately i have updated my configuration.yaml to this:

template:
  - sensor:
    -  name: "Upstairs Temperature"
       unique_id: Upstairs_Temperature
       unit_of_measurement: "C"
       state: >
          {% set ensuite_temp = states('sensor.ensuite_sensor_temperature') | float(0) %}
          {% set harper_temp = states('sensor.harper_s_thermostat_temperature') | float(0) %}
          {% set mia_temp = states('sensor.mia_s_thermostat_temperature') | float(0) %}
          {% set landing_temp = states('sensor.landing_thermostat_temperature') | float(0) %}
          {% set main_temp = states('sensor.main_bedroom_thermostat_temperature') | float(0) %}
          {% set study_temp = states('sensor.study_thermostat_temperature') | float(0) %}
          {{average([ensuite_temp,harper_temp,mia_temp,landing_temp,main_temp,study_temp]) | round(2, default=0)}}

    -  name: "Upstairs Humidity"
       unique_id: Upstairs_Humidity
       unit_of_measurement: "%"
       state: >
         {% set mia_humidity = states('sensor.mia_s_thermostat_humidity') | float(0) %}
         {% set harper_humidity = states('sensor.harper_s_thermostat_humidity') | float(0) %}
         {% set landing_humidity = states('sensor.landing_thermostat_humidity') | float(0) %}
         {% set mainbrm_humidity = states('sensor.main_bedroom_thermostat_humidity')| float(0) %}
         {% set study_humidity = states('sensor.study_thermostat_humidity') | float(0) %}
         {{average([mia_humidity,harper_humidity,landing_humidity,mainbrm_humidity,study_humidity]) | round(2, default=0)}}

    -  name: "Downstairs Temperature"
       unique_id: downstairs_temperature
       unit_of_measurement: "C"
       state: >
         {% set kitchen_temp = states('sensor.kitchen_thermostat_temperature') | float(0) %}
         {% set living_temp = states('sensor.living_room_thermostat_temperature') | float(0) %}
         {{average([kitchen_temp, living_temp]) | round(2, default=0)}}

    -  name: "Downstairs Humidity"
       unique_id: downstairs_humidity
       unit_of_measurement: "%"
       state: >
         {% set kitchen_humidity = states('sensor.kitchen_thermostat_humidity') | float(0) %}
         {% set living_humidity = states('sensor.living_room_thermostat_humidity') | float(0) %}
         {{average([kitchen_humidity, living_humidity]) | round(2, default=0)}}

However even with this i am still getting these errors:

homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set upstairs_humidity = states('sensor.upstairs_humidity') | float %}
{{ upstairs_humidity }} %' but no default was specified

2024-12-11 07:50:19.432 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set downstairs_temp = states('sensor.downstairs_temperature') | float %}
{{ downstairs_temp }} °C) renders=6>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2352, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 631, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2733, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2355, in forgiving_float_filter
    raise_no_default("float", value)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1909, in raise_no_default
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set downstairs_temp = states('sensor.downstairs_temperature') | float %}

{{ downstairs_temp }} °C' but no default was specified
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 748, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        variables, strict=strict, log_fn=log_fn, **kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 633, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set downstairs_temp = states('sensor.downstairs_temperature') | float %}
{{ downstairs_temp }} °C' but no default was specified
2024-12-11 07:50:19.438 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set downstairs_humidity = states('sensor.downstairs_humidity') | float %}
{{ downstairs_humidity }} %) renders=6>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2352, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 631, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2733, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2355, in forgiving_float_filter
    raise_no_default("float", value)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1909, in raise_no_default
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set downstairs_humidity = states('sensor.downstairs_humidity') | float %}
{{ downstairs_humidity }} %' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 748, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        variables, strict=strict, log_fn=log_fn, **kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 633, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set downstairs_humidity = states('sensor.downstairs_humidity') | float %}
{{ downstairs_humidity }} %' but no default was specified

I am not overly complex with HA but i have tried looking at the error and finding template.py as it looked like it may have some of the old formatting in it, but i cant find it (probably a me issue).

The error seems to point to some of the formatting issues i found, but even after updating the configuration.yaml these changes seem to be persistent from the old configuration.

Hoping you can point me in the right direction to finally clean up my last error i have.

There’s a helper for finding the average of a list of sensors that is a lot easier to set up:

Thanks for that, i will definately take a look.

I guess that having this is may still have the same issue of cached/legacy settings staying persistent though?

Might help provide me a simpler way to get the same functionality i have now, but not sure how this addresses the error I’m getting :frowning:

Did you restart or reload?

The other issue you would have with those sensors is that if any of the source sensors were unavailable they’d be set to zero, stuffing up your calculation. The min/max sensor won’t have this issue.

I wonder if those errors are arising from a template in Developer Tools / Template?

In particular:

ValueError: Template error: float got invalid input 'unknown'
when rendering template
'{% set downstairs_temp = states('sensor.downstairs_temperature') | float %}
 {{ downstairs_temp }} °C' but no default was specified

yeah i have restarted both the HA container and also the whole host. Appreciate that using min/max sensor wouldn’t have those issues as such, but I’m not sure if they will also remove the existing references which have been edited away.

thats kinda my issue.
im not a strong HA dev/user, so the only place i have edited things is in the main configuration.yaml file (which i pasted above). Those lines definately were there before i edited them (hence updating the formatting/syntax), but even after editing them, they are somehow remaining persistent.

But did you test them first in Developer Tools → Templates?

That area generates errors in the log too. Check it.