home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.14k stars 29.82k forks source link

Unit conversion causes self-referencing sensors to break #115051

Open mekaneck opened 5 months ago

mekaneck commented 5 months ago

The problem

Unintended unit conversion behavior when units are changed on any sensor with a device_class that supports unit conversion and that contains a template that self-references (uses this.state).

Here is a simplified example that can be used to replicate the issue. This sensor will update whenever a custom event unit_conversion_test is fired on the event bus. One would expect the sensor to be set to 1 after that event is first fired, and then the sensor would remain at 1 on subsequent triggers. If the units are later changed in the UI, the expectation should be that the value is converted to the new units and then the trigger continues to set the state of the sensor to it's current state, so it should essentially do nothing.

template:
  - trigger:
      - platform: event
        event_type: unit_conversion_test
    sensor:
      - name: Unit Conversion Test
        unit_of_measurement: "km"
        device_class: distance
        state: "{{ this.state if is_number(this.state) else 1 }}"

If we change the units to m in the UI, the sensor will change to 1000 as expected. However now when we fire the event, the value is multiplied by the conversion factor each time (so it goes from 1000 to 1000000 to 1E9, 1E12, etc. on each subsequent trigger).

While this example is clearly contrived, this is a real issue for every sensor that uses self-referencing and has its units changed.

What version of Home Assistant Core has the issue?

core-2024.4.1

What was the last working version of Home Assistant Core?

This has been an issue since unit auto-conversion was introduced in 2022 (ref dev blog link)

What type of installation are you running?

Home Assistant Container

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

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

mekaneck commented 1 month ago

Still an issue in 2027.7

Not sure if this is really a "bug" or more of an architecture problem.