Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template sensors not working since 2025.03.2 #140447

Open
JonLaliberte opened this issue Mar 12, 2025 · 9 comments
Open

Template sensors not working since 2025.03.2 #140447

JonLaliberte opened this issue Mar 12, 2025 · 9 comments

Comments

@JonLaliberte
Copy link

The problem

I'm seeing that a few of my template/history sensors are not working after moving from 2025.03.1 to 2025.03.2.
This is one example.

What version of Home Assistant Core has the issue?

2025.3.2

What was the last working version of Home Assistant Core?

2025.3.1

What type of installation are you running?

Home Assistant OS

Integration causing the issue

template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

- unique_id: "pool_water_temperature_1_hour_avg_template"
    unit_of_measurement: "°F"
    state: >
      {% set last_reported = states.sensor.aw_pool_temp_float.last_reported %}
      {% set stats_sensor = states('sensor.pool_water_temperature_1_hour_avg') %}
      {% set pool_temp = states('sensor.aw_pool_temp_float') %}
      {% if last_reported == none %}
        {{ stats_sensor }}
      {% else %}
        {% set last_reported_time = as_timestamp(last_reported) %}
        {% set current_time = as_timestamp(now()) %}
        {% if stats_sensor != 'unavailable' and stats_sensor != 'unknown' and (current_time - last_reported_time) <= 3600 %}
          {{ stats_sensor }}
        {% else %}
          {{ pool_temp }}
        {% endif %}
      {% endif %}

Anything in the logs that might be useful for us?

Nothing in logs for this sensor.
However, this other sensor did report an error in the logs:

Error rendering state template for sensor.pool_temperature_overnight_loss: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set max_temp = states('sensor.pool_temp_eod') %} {% set min_temp = states('sensor.pool_temp_sod') %} {% if max_temp != 'unavailable' and min_temp != 'unavailable' %} {% set delta = ((max_temp | float) - (min_temp | float)) | round(1) %} {% if delta >= 0 %} {{ '+' ~ delta }} {% else %} {{ delta }} {% endif %} {% else %} None {% endif %}' but no default was specified

Here I traced it back and see that `sensor.pool_temp_sod` is unknown, and this is caused by `pool_water_temperature_1_hour_avg_template` (the sensor I'm detailing in this issue) being unknown.

Additional information

I debugged using the dev tools template editor using:

{% set last_reported = states.sensor.aw_pool_temp_float.last_reported %}
{% set stats_sensor = states('sensor.pool_water_temperature_1_hour_avg') %}
{% set pool_temp = states('sensor.aw_pool_temp_float') %}

{# Debugging Outputs #}
Last Reported: {{ last_reported }}
Stats Sensor: {{ stats_sensor }}
Pool Temp: {{ pool_temp }}

{% if last_reported == none %}
  {# If last_reported is none, use the current statistics sensor value #}
  Output Last Reported None: {{ stats_sensor }}
{% else %}
  {% set last_reported_time = as_timestamp(last_reported) %}
  {% set current_time = as_timestamp(now()) %}
  
  {# More Debugging Outputs #}
  Last Reported Time (timestamp): {{ last_reported_time }}
  Current Time (timestamp): {{ current_time }}
  Time Difference: {{ current_time - last_reported_time }}

  {% if stats_sensor not in ['unavailable', 'unknown'] and (current_time - last_reported_time) <= 3600 %}
    {# If stats_sensor is available and the last report was within an hour, use stats_sensor #}
    Output Template: {{ stats_sensor }}
  {% else %}
    {# Otherwise, fall back to the current pool temperature #}
    Output Pool Temp: {{ pool_temp }}
  {% endif %}
{% endif %}

This reported:

Last Reported: 2025-03-12 12:42:45.560789+00:00
Stats Sensor: 41.3
Pool Temp: 41.36

  Last Reported Time (timestamp): 1741783365.560789
  Current Time (timestamp): 1741783367.263896
  Time Difference: 1.7031068801879883

    Output Template: 41.3
@home-assistant
Copy link

Hey there @Petro31, @PhracturedBlue, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign template Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation
template source
(message by IssueLinks)

@JonLaliberte
Copy link
Author

Here is another one.

Sensor: sensor.template_pool_roof_temperature_last_15_minutes

Image

The template is configured like this:

  - unique_id: "pool_solar_temperature_roof_15min_avg_template"
    unit_of_measurement: "°F"
    state: >
      {% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %}
      {% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %}
      {% set roof_temp = states('sensor.pool_solar_temperature_roof') %}
      {% if last_reported == none %}
        {{ stats_sensor }}
      {% else %}
        {% set last_reported_time = as_timestamp(last_reported) %}
        {% set current_time = as_timestamp(now()) %}
        {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
          {{ stats_sensor }}
        {% else %}
          {{ roof_temp }}
        {% endif %}
      {% endif %}

Testing this in dev tools with:

{% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %}
{% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %}
{% set roof_temp = states('sensor.pool_solar_temperature_roof') %}

{# Debugging Outputs #}
Last Reported: {{ last_reported }}
Stats Sensor: {{ stats_sensor }}
Roof Temp: {{ roof_temp }}

{% if last_reported == none %}
  {# If last_reported is none, use the current stats sensor value #}
  Output: {{ stats_sensor }}
{% else %}
  {% set last_reported_time = as_timestamp(last_reported) %}
  {% set current_time = as_timestamp(now()) %}
  
  {# More Debugging Outputs #}
  Last Reported Time (timestamp): {{ last_reported_time }}
  Current Time (timestamp): {{ current_time }}
  Time Difference: {{ current_time - last_reported_time }}

  {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
    {# If stats_sensor is not 'unknown' and the last report was within 15 minutes (900 seconds) #}
    Output: {{ stats_sensor }}
  {% else %}
    {# If it's been more than 15 minutes since last report or stats_sensor is 'unknown', fall back to roof_temp #}
    Output: {{ roof_temp }}
  {% endif %}
{% endif %}

Yields:

Last Reported: 2025-03-12 14:53:33.451648+00:00
Stats Sensor: 78.7
Roof Temp: 80.4


  
  
  
  
  Last Reported Time (timestamp): 1741791213.451648
  Current Time (timestamp): 1741791213.460102
  Time Difference: 0.008454084396362305

  
    
    Output: 78.7

@Petro31
Copy link
Contributor

Petro31 commented Mar 12, 2025

Hi this is not a bug, you have issues in your templates.

Your first error is telling you that float got an invalid input. You are using float on max_temp and min_temp. That means one of those entities is unknown causing the template to fail.

float got invalid input 'unknown' when rendering template '{% set max_temp = states('sensor.pool_temp_eod') %} {% set min_temp = states('sensor.pool_temp_sod') %} {% if max_temp != 'unavailable' and min_temp != 'unavailable' %} {% set delta = ((max_temp | float) - (min_temp | float)) | round(1) %} {% if delta >= 0 %} {{ '+' ~ delta }} {% else %} {{ delta }} {% endif %} {% else %} None {% endif %}' but no default was specified

@JonLaliberte
Copy link
Author

Thanks @Petro31

I probably should not have included that error log as I think it can cause confusion here.
And in general, my first template is a bit complicated as it makes use of HACS module for storing variables and it has some recursive nature to it that can make it difficult to make sense of quickly.

Let's ignore that one for now and concentrate on my second example.

This one is pretty simple. It grabs the value of a stats sensor and of a temperature sensor and figures out the best value to return.

In the example from the dev tools template tester, the template should be outputting 78.7, but since installing 2025.03.2 it isn't working:

Image

The last data point here is when I installed the update last night.

@Petro31
Copy link
Contributor

Petro31 commented Mar 12, 2025

I'd like to clarify that nothing in templates was changed between these 2 versions. Your issue is likely elsewhere. Otherwise, please provide the following:

  1. A template entity and it's configuration that does not error.
  2. A screenshot of the current state for the template entity.
  3. Screenshots of every entity (and their state) the template entity uses to calculate the state provided in the screenshot for bullet number 2.

@JonLaliberte
Copy link
Author

JonLaliberte commented Mar 12, 2025

@Petro31 Yeah, I noticed that too. I'm not sure what could be causing it though.

When you say "A template entity and it's configuration that does not error.", you want me to provide some other template entity/configuration?

Because the one mentioned in my second post (sensor.template_pool_roof_temperature_last_15_minutes) doesn't error, it just gives me "unknown".

Right now I have a bunch of sensors failing, but many of them have a root cause of the pool_water_temperature_1_hour_avg_template template failing. But, concentrating on that one is just going to be confusing.

The template for sensor.template_pool_roof_temperature_last_15_minutes is much more straightforward.

Just to provide the info mentioned for this template:

Template:

# Entity ID: `sensor.template_pool_roof_temperature_last_15_minutes`
  - unique_id: "pool_solar_temperature_roof_15min_avg_template"
    unit_of_measurement: "°F"
    state: >
      {% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %}
      {% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %}
      {% set roof_temp = states('sensor.pool_solar_temperature_roof') %}
      {% if last_reported == none %}
        {{ stats_sensor }}
      {% else %}
        {% set last_reported_time = as_timestamp(last_reported) %}
        {% set current_time = as_timestamp(now()) %}
        {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
          {{ stats_sensor }}
        {% else %}
          {{ roof_temp }}
        {% endif %}
      {% endif %}

So this template uses just 2 other sensors:

  • sensor.pool_solar_temperature_roof
  • sensor.pool_roof_temperature_last_15_minutes - not to be confused with the template sensor
Image Image

At the bottom here is the template sensor.

And for good measure, I copied/pasted the entire template into dev tools and it returns a valid value.

Image

@Petro31
Copy link
Contributor

Petro31 commented Mar 12, 2025

I notice your template has a unit of measurement of degree F but you don't have device_class temperature on it. Are you sure you don't have errors in your logs? This technically isn't a valid configuration because of that.

@JonLaliberte
Copy link
Author

I'm sorry, I expected an error here to be reported often (at each restart at a minimum, but maybe even each time one of the constituent sensors was updated). So I went back further in the log and found an error logged last night, but none since then (even though I have restarted HA a few times since then). This seems to have been logged right after the update installed, but before HA restarted, but never again since then.

2025-03-11 21:35:04.839 ERROR (MainThread) [homeassistant.helpers.event] Error while dispatching event for sensor.pool_solar_temperature_roof to <Job track state_changed event {'sensor.pool_roof_temperature_last_15_minutes', 'sensor.pool_solar_temperature_roof'} HassJobType.Callback <bound method TrackTemplateResultInfo._refresh of <TrackTemplateResultInfo {Template<template=({% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %} {% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %} {% set roof_temp = states('sensor.pool_solar_temperature_roof') %} {% if last_reported == none %}
  {{ stats_sensor }}
{% else %}
  {% set last_reported_time = as_timestamp(last_reported) %}
  {% set current_time = as_timestamp(now()) %}
  {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
    {{ stats_sensor }}
  {% else %}
    {{ roof_temp }}
  {% endif %}
{% endif %}) renders=41718>: <RenderInfo Template<template=({% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %} {% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %} {% set roof_temp = states('sensor.pool_solar_temperature_roof') %} {% if last_reported == none %}
  {{ stats_sensor }}
{% else %}
  {% set last_reported_time = as_timestamp(last_reported) %}
  {% set current_time = as_timestamp(now()) %}
  {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
    {{ stats_sensor }}
  {% else %}
    {{ roof_temp }}
  {% endif %}
{% endif %}) renders=41718> all_states=False all_states_lifecycle=False domains=frozenset() domains_lifecycle=frozenset() entities=frozenset({'sensor.pool_roof_temperature_last_15_minutes', 'sensor.pool_solar_temperature_roof'}) rate_limit=None has_time=True exception=None is_static=False>}>>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 640, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'unavailable'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 355, in _async_dispatch_entity_id_event
    hass.async_run_hass_job(job, event)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1312, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 940, in async_run_hass_job
    hassjob.target(*args)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 463, in _handle_results
    self.async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1023, in async_write_ha_state
    self._async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1148, in _async_write_ha_state
    self.__async_calculate_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1085, in __async_calculate_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1029, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 642, in state
    raise ValueError(
    ...<5 lines>...
    ) from err
ValueError: Sensor sensor.template_pool_roof_temperature_last_15_minutes has device class 'None', state class 'None' unit '°F' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unavailable' (<class 'str'>)
[01:35:08] INFO: Home Assistant Core finish process exit code 0
[01:35:08] INFO: Home Assistant Core service shutdown

I just updated the template to add the device_class, just to be sure that wasn't causing a problem (this sensor has been active for at least idk, 8-12 months without it though):

  - unique_id: "pool_solar_temperature_roof_15min_avg_template"
    unit_of_measurement: "°F"
    device_class: temperature
    state: >
      {% set last_reported = states.sensor.pool_solar_temperature_roof.last_reported %}
      {% set stats_sensor = states('sensor.pool_roof_temperature_last_15_minutes') %}
      {% set roof_temp = states('sensor.pool_solar_temperature_roof') %}
      {% if last_reported == none %}
        {{ stats_sensor }}
      {% else %}
        {% set last_reported_time = as_timestamp(last_reported) %}
        {% set current_time = as_timestamp(now()) %}
        {% if stats_sensor != 'unknown' and (current_time - last_reported_time) <= 900 %}
          {{ stats_sensor }}
        {% else %}
          {{ roof_temp }}
        {% endif %}
      {% endif %}

I did a full restart, but the template sensor is still reporting "unknown" as the state. But in dev tools it reports "53.3" and the result type is "number".

@Petro31
Copy link
Contributor

Petro31 commented Mar 12, 2025

Sensor sensor.template_pool_roof_temperature_last_15_minutes has device class 'None', state class 'None' unit '°F' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unavailable' (<class 'str'>)

I'm not sure what to tell you. Please review your configuration and logs please.

I suggest you add state_class: measurement as well. You likely have an error still in your logs about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants