Skip to content

Templates and automations based on states() break when unit of measurement is changed #145100

@mariusz-schimke

Description

@mariusz-schimke

The problem

After changing the unit of measurement on several time-based sensors (e.g., from seconds to minutes), I started encountering inconsistent behavior across automations and templates. One example is a Jinja template used in a push notification message:

{{ (states('sensor.ups_battery_runtime') | int) | timestamp_custom('%H:%M:%S', local = false) }}

Originally, this returned 00:54:00 (seconds). After the unit was changed to minutes, it returned 00:00:54, causing a misleading notification (54 seconds instead of 54 minutes). This silent behavioral change led to other issues as well, such as with utility meters (see my other report here).

I now feel completely lost, as I have no easy way to check which of my automations and templates (among hundreds) were affected by my seemingly harmless unit changes. I thought I'd just make the times presented in the UI more human readable (e.g. 1,5 hour rather than 5400 seconds ;)), but in fact I caused all dependencies like automations and templates to change their behavior silently, and I don't even know which ones are broken now. I'm just noticing now and then some strange behaviors like the one I'm describing now.

To improve robustness and prevent such breakages, please consider allowing templates to read sensor state in a fixed unit, e.g.:

{{ (states('sensor.ups_battery_runtime', unit='s') | int) | timestamp_custom('%H:%M:%S', local = false) }}.

This would make template logic predictable and resilient even if an entity's unit is later changed in the UI.

Please, let me know if this suggestion makes sense or if this should just be approached differently. I appreciate it, thanks!

Image

What version of Home Assistant Core has the issue?

2025.5.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

{{ (states('sensor.ups_battery_runtime') | int) | timestamp_custom('%H:%M:%S', local = false) }}

Anything in the logs that might be useful for us?

Additional information

An argument that would allow specifying the unit would make your logic based on the sensor's value reliable: states('sensor.ups_battery_runtime', unit='s')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions