Template sensor showing device attribute not auto-updating – Why?

I created some template sensors to show the installed version of ESPHome devices on the HA GUI.
As far as I know the current sw version is not directly accessible, so I am obtaining the sw_version attribute from the ESP devices.
This is the current code:

template:
  - sensor:
    - name: D1PV2-01 FW
      unique_id: uid_fw_d1pv2_01
      state: "{{ device_attr( device_id( 'sensor.d1pv2_01_ip_address' ),'sw_version' ) }}"

These sensors correctly show the currently installed ESPHome version like so:

2026.1.5 (2026-02-11 08:33:22 +0100)

But to my surprise the values are not automatically updated when a new version of the software is installed. I expected these sensors to update every minute.
Apparently a changed attribute of a device is not seen as a state change?
Why doesn’t this work, and what could be a solution for this?
Should I add a time based trigger?

That would only update when sensor.d1pv2_01_ip_address updates state. And the sensors state would only update when the sw_version changes.

I doubt the IP address is changing frequently, so your sensor state is likely static.

Use a different source entity, or attach a trigger to the sensor.

Thank you Petro.

OK, I do have another entity for all my ESP devices that is updated frequently: an uptime sensor.
So I changed the code like so:

template:
  - sensor:
    - name: D1PV2-01 FW
      unique_id: uid_fw_d1pv2_01
      state: "{{ device_attr( device_id( 'sensor.d1pv2_01_uptime' ),'sw_version' ) }}"

But unfortunately it still doesn’t auto-update after installing a new sw version.
When I enter the code in the Template editor the result is correct, but the template sensor still keeps showing the old value, also after more then a minute.
Only after going through Settings → Developer tools → YAML → Reload Template entities I get the correct results.

So next step is to try adding a trigger?

That’s an uptime entity, it also does not change frequently. The state will be a datetime set when the source was rebooted/restarted, the UI makes it look like it updates frequently because the time is “counting up” but it’s purely a UI calculation.

To add a trigger, follow the documentation.

Interesting!
I will try adding the trigger later, but just out of curiosity I now tested it with another entity: the human readable uptime sensor, but apparently this also does no really change state (its an uptime entity as well).
Then I changed it into a WiFi Strength entity , that does change often (with an update interval of 60 sec), but still the same effect: the template sensor is not updated. Is this also as expected?

Looking at the Template editor again, I now see that the Result is shown as:

Result
2026.2.2 (2026-02-26 16:00:06 +0100)
Result type: string
This template does not listen for any events and will not update automatically.

So it does not work at all like this?

it’s possible that device_attr or device_id do not register the entity, meaning you’d have to use a trigger regardless.

Yes, after adding the trigger it is working fine, also with the IP address entities :slight_smile:

This is the final code:

template:
  - trigger:
    - platform: time_pattern
      minutes: /10
    sensor:
      - name: D1PV2-01 FW
        unique_id: uid_fw_d1pv2_01
        state: "{{ device_attr( device_id( 'sensor.d1pv2_01_ip_address' ),'sw_version' ) }}"
      - name: D1V3-01 FW
        unique_id: uid_fw_d1v3_01
        state: "{{ device_attr( device_id( 'sensor.d1v3_01_ip_address' ),'sw_version' ) }}"