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.51k stars 30.71k forks source link

Cannot change “state_class” for a sensor with LTS #120784

Open ildar170975 opened 4 months ago

ildar170975 commented 4 months ago

The problem

There is some sensor created by some integration. When the integration entry was added & the sensor was created, I specified a "state_class":

homeassistant:
  customize:
    sensor.xxx:
      state_class: measurement

Then some LTS data have been stored. Later I realized that "total_increasing" is more suitable for the sensor - and changed it:

homeassistant:
  customize:
    sensor.xxx:
      state_class: total_increasing

But the "state_class" has not been changed then - it is still "measurement": image

No messages in Log. No "issues" in Dev tools -> Statistics.

Earlier in similar cases I had to:

  1. Exclude a sensor from a config.
  2. Dev tools -> Statistics -> removed stored LTS (since the sensor does not exist).
  3. Re-added the sensor into config.
  4. Only then the sensor got a new "state_class".

Probably I cannot just change "state_class" from "measurement" -> "total(-increasing)" and keep having a previously stored LTS. In DB data are stored in a "statistics" table which contains columns for "mean", "max", "min", "state", "sum". But old "mean/max/min" data become meaningless with a new "state_class". Based on this speculation, a more logical way after changing "state_class" would be:

  1. Change "state_class" as defined by a user.
  2. Start storing new LTS data accordingly to the new "state_class".
  3. Ignore an old LTS while processing LTS in cards like "statistics-graph".
  4. Show an "issue" in Dev tools -> Statistics for this sensor with a proposal to remove old LTS.

Otherwise currently there is no any notification of a user that "state_class" is not set as it was defined by the user.

What version of Home Assistant Core has the issue?

2024.6.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

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

parautenbach commented 1 month ago

@ildar170975 are you still seeing this problem?

I think I might be suffering from the same or a similar bug.

I saw that a number of sensors are going to LTS, which I didn't configure, so the state class must be set by the integration. To use one example, I'm using OpenWeatherMap, and basically all the sensors go to LTS. While LTS doesn't take that much space, I still don't want this.

I've set my overrides in my customize section, but it will not respect it.

    customize_glob:
      "sensor.openweathermap_*":
        state_class: null

In the above, I have it set as null, but I have None before that seemed to work (no quotes, so not a string).

List of sensors, for reference:

sensor.openweathermap_cloud_coverage: measurement 
sensor.openweathermap_daily_cloud_coverage: measurement 
sensor.openweathermap_daily_dew_point: measurement 
sensor.openweathermap_daily_feels_like_temperature: measurement 
sensor.openweathermap_daily_humidity: measurement 
sensor.openweathermap_daily_pressure: measurement 
sensor.openweathermap_daily_rain: measurement 
sensor.openweathermap_daily_snow: measurement 
sensor.openweathermap_daily_temperature: measurement 
sensor.openweathermap_daily_uv_index: measurement 
sensor.openweathermap_daily_visibility: measurement 
sensor.openweathermap_daily_wind_bearing: measurement 
sensor.openweathermap_daily_wind_speed: measurement 
sensor.openweathermap_dew_point: measurement 
sensor.openweathermap_feels_like_temperature: measurement 
sensor.openweathermap_humidity: measurement 
sensor.openweathermap_pressure: measurement 
sensor.openweathermap_rain: measurement 
sensor.openweathermap_snow: measurement 
sensor.openweathermap_temperature: measurement 
sensor.openweathermap_uv_index: measurement 
sensor.openweathermap_visibility: measurement 
sensor.openweathermap_wind_bearing: measurement 
sensor.openweathermap_wind_speed: measurement 
ildar170975 commented 1 month ago

Pieter, I will be able to check in ~10 days! Keep in touch.

ildar170975 commented 1 month ago

@parautenbach My test:

Initial state: image

Stat graph is available: image

Then the state_class was set to none:

homeassistant:
  customize:
    sensor.weather_home_openweathermap_pressure:
      state_class: none

After HA reboot (just in case) we see a changed state: image

And the issue with a useless message "state class not supported": image

And it is possible to delete LTS data: image

I have not pressed "Delete" and have no idea about if this data are actually deleted from DB...