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
73.71k stars 30.84k forks source link

Templated sensor entity warning if state_class= 'total_increasing' and device_class= 'monetary' after 2023.2.1 #87320

Closed BobC76 closed 1 year ago

BobC76 commented 1 year ago

The problem

Following upgrade to core-2023.2.1 I received the following warning in my log:

Entity sensor.octopus_energy_saving_session_earnings (<class 'homeassistant.components.template.sensor.TriggerSensorEntity'>) is using state class 'total_increasing' which is impossible considering device class ('monetary') it is using; The sensor is as follows:

  sensor:
    # Keep track of Octopus Energy Savings Sessions Earnings
    - name: "Octopus Energy Saving Session Earnings"
      state_class: total_increasing
      device_class: monetary
      icon: mdi:currency-gbp
      state: "{{ (states('sensor.octopus_energy_saving_session_points') | float * 0.00125) | round(2, 'floor') }}"
      unit_of_measurement: GBP

Its providing a conversion of accumulating saving session points into GBP. So I don't see why total_increasing is incompatible with "monetary" device types, and I can't find the documentation that explains why they are if so.

What version of Home Assistant Core has the issue?

core-2023.2.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

see above

Example YAML snippet

see above

Anything in the logs that might be useful for us?

see above

Additional information

No response

frenck commented 1 year ago

total_increasing should be used for resettable meters (e.g., energy meters, that are always counting upwards, unless completely replaced).

Otherwise, the total class should be used.

../Frenck

BobC76 commented 1 year ago

Thanks. It is always counting upwards, and then from time to time when the points are turned into a voucher it will reset to zero. It meets the definition of the text in the documentation at https://developers.home-assistant.io/docs/core/entity/sensor/

Similar to total, with the restriction that the state represents a monotonically increasing positive total which periodically restarts counting from 0, e.g. a daily amount of consumed gas, weekly water consumption or lifetime energy consumption. Statistics of the accumulated growth of the sensor's value since it was first added is updated every 5 minutes. A decreasing value is interpreted as the start of a new meter cycle or the replacement of the meter.

And later https://developers.home-assistant.io/docs/core/entity/sensor/#entities-representing-a-total-amount this example also fits:

The sensor's value may reset to 0, and its value can only increase: state class total_increasing. Examples: energy consumption aligned with a billing cycle, e.g. monthly, an energy meter resetting to 0 every time it's disconnected

Doesn't explain why state_class=total_increasing and device_class=monetary are "impossible" combinations.

ChristophCaina commented 1 year ago

Probably because this 'is being reset when turned into a Voucher" is a really rare Edge-case.

I would consider to create a template that does Not use the monetary device_class.

BobC76 commented 1 year ago

Er. It's just like a meter that resets to 0 every month. Just like in the docs. I don't understand the suggestion of not using the monetary device class when the thing it's measuring is money. So 'edge case' or not, nothing these comments have provided explains why it's deemed 'impossible' and the warning is issued.

frenck commented 1 year ago

Doesn't explain why state_class=total_increasing and device_class=monetary are "impossible" combinations.

In general total_increasing is designed for things like electricity meters. Other cases, should use total (with an reset parameter to indicate reset time if applicable).

Er. It's just like a meter that resets to 0 every month.

Exactly! So it needs total, with a reset timestamp.

BobC76 commented 1 year ago

Why?

The documentation for total increasing says "the state represents a monotonically increasing positive total which periodically restarts counting from 0"

Sorry but you aren't explaining the 'why' here, only repeating the statement that it should use total not total_increasing.

frenck commented 1 year ago

See above answer.

BobC76 commented 1 year ago

This doesn't explain why just reasserts the statement in the warning. If there is a disadvantage or consequence - somebody bothered to put the code in to issue a warning, why what is it warning for and why isn't it in the docs?

frenck commented 1 year ago

Monetary balances are a total, not monotonically increasing.

BobC76 commented 1 year ago

This one is derived from the monotonically increasing points total. Until the point are reset to zero. It's not a 'balance'.