Adjust sensor data (delete) in the database

I sometimes have outliers in some sensors. As these have the state_class: “measurement”, unfortunately they cannot be corrected using DEVTools.

There is only one way left: direct intervention in the database.

Please make a backup of your Home Assistant instance before you intervene in the database.

There are two tables in the database in which the values of the entities are stored. The “states” and the “statistics” contain the values and the corresponding statistics.

In order to access the database natively, the “SQLite Web” add-on must be installed. It does not need to be configured and displays the tables of the database and offers the possibility to manipulate the data via SQL query.

In order to find the values of an entity in the “states” table, the ID of the relevant entity must first be searched for in the “states_meta”.
With the query: “SELECT * FROM “states” where metadata_id = (SELECT metadata_id FROM “states_meta” where entity_id = ‘sensor.deye_pv1_power’)”, all values for the entity ‘sensor.deye_pv1_power’ are displayed.

Now either use the filter “where CAST(state as decimal) > 6800” to select all suitable values, or remember the state_id of the data set. The CAST is necessary because the state values are saved as varchar(255).

select * FROM “states” where metadata_id= (SELECT metadata_id FROM “states_meta” where entity_id = 'sensor.deye_pv1_power') and CAST(state as decimal) > 6000

Be careful, with the delete from, without the where clause, the whole table is quickly deleted! Then the only thing that helps is to import a backup.

Always delete the incorrect values in the “states” and “statistics” table, then the “outliers” are also corrected. The data correction in the frontend only takes effect after a restart, I assume the data is cached somewhere in the Home Assistant.

Leave a Comment