Template Sensor and Attribute issues in Home Assistant

I came across an interesting issue today and thought it worth documenting.

I have setup a template sensor which is triggered by the state change of an entity. This template sensor saves a generic state (a string saying OK), storing a maximum or minimum value of the referenced entity as an attribute. Its a daily maximum, so I reset it at midnight.

Screenshot 2025-09-04 at 13.40.11

Template Sensor Setup

In order to calculate if for example the “new” state of the entity was higher than the previous maximum value stored in the attribute, I have this logic in the template sensor YAML:

attributes:
    max_rain: >
        {% set r_new = states('sensor.tempest_precipitation_intensity') | float(-1) %}
        {% set prev = this.attributes.max_rain | float(-1) %}
        {{ [r_new, prev] | max if trigger.platform != 'time' else r_new }} 

So, create a variable r_new of the current state of the precipitation intensity entity, otherwise return -1.

Next, set a variable prev to get the previous value of the current template sensor attribute (max_rain), otherwise return -1.

Finally set the max_rain attribute to the largest of the two variables (r_new or prev), or reset to the current state (r_new) at midnight.

The Problem

If you test out this YAML logic in the developer tools, it works without error. E.g.

{% set r_new = states('sensor.tempest_precipitation_intensity') | float(-1) %}
{% set prev = state_attr('sensor.today_weather_statistics', 'max_rain') | float(-1) %}
{{ [r_new, prev] | max }}
Screenshot 2025-09-04 at 13.27.27
Screenshot 2025-09-04 at 13.27.35

The result here is 0.0, which reflects the current state from sensor.tempest_precipitation_intensity. In this case prev is returning -1 because the attribute max_rain does not exist on sensor.today_weather_statistics. All expected so far.

The REAL problem

The issue comes in when we’re trying to create this template sensor for the first time and establish the max_rain attribute. Because the logic to create and update the max_rain attribute references itself, something internally seems to fail. This all happens despite the YAML being safe in the developer tools. E.g.

{% set prev = state_attr('sensor.today_weather_statistics', 'max_rain') | float(-1) %}

Returns -1 safely in the developer tools. But it seems this kind of reference to itself cannot work on the first initialisation of the attribute itself. What you’ll find is the attribute never appears in your template sensor.

Solution

So instead of creating the self referencing logic in the first place, the easiest thing to do is something like this, where you create the attribute and assign a default value:

- trigger:
  - platform: state
    entity_id: 
        - sensor.tempest_precipitation_intensity
  - platform: time
    at: "00:00:00"
  sensor:
    - name: 'Today Weather Statistics'
      unique_id: today_weather_statistics
      state: "OK"
      attributes:
        max_rain: >
            {{ float(0.0) }}

Restart Home Assistant, go check out your new sensor, and confirm the max_rain attribute appears, with a value of 0.0.

Now you’re ready for the self-referencing logic to work and not fail. Go back to your template sensor YAML, and update the max_rain logic:

- trigger:
  - platform: state
    entity_id: 
        - sensor.tempest_precipitation_intensity
  - platform: time
    at: "00:00:00"
  sensor:
    - name: 'Today Weather Statistics'
      unique_id: today_weather_statistics
      state: "OK"
      attributes:
        max_rain: >
                    {% set r_new = states('sensor.tempest_precipitation_intensity') | float(-1) %}
                    {% set prev = this.attributes.max_rain | float(-1) %}
                    {{ [r_new, prev] | max if trigger.platform != 'time' else r_new }} 

Restart Home Assistant – this time you should still see your attribute, but most importantly, see the value increase from 0.0, should the state of your reference entity be higher.