There was a bug introduced with HomeAssistant 2023.05 which resulted in negative values in the energy dashboard if Riemann Sum was used for power calculation. This script fixed the values in the HomeAssistant database and recalculate all following entries
Tested with
- HomeAssistant Docker 2023.6.2
You are not interested in the details and just want to have your database fixed? Read this paragraph
- This script wipes statistics_short_term in the database and replaces it with one single entry with the last valid value.
It is required to have the last value updated for the next calculation of Riemann Sum Integral - You do NOT lose any long-term statistics. (Read below to understand the reason)
This table holds the Riemann Sum Integral with a resolution of 5 minutes but not the long-term statistics. - HA starts to fill this table again
- Clone this repository
- Change following variable to your HomeAssistant config folder
HA_CONFIG_ROOT = "/HomeAssistant/config"
- (Optional): Required for MySQL server connection
Change following variables to your server details:DB_SERVER = { "DB_HOST": "", "DB_USER": "", "DB_PASSWORD": "", "DB_NAME": "" }
- Create a entities.list file which includes entities that can be fixed by this script by executing:
(This does NOT touch your database yet)python HA_FixNegativeStatistics.py --list
- Modify entities.list to your needs (remove unwanted entities)
- Execute
python HA_FixNegativeStatistics.py
NOTE:
- This script makes a backup of your existing database first.
- This script exits if a backup file exists already to prevent an ovewrite.
In order to ensure a valid calculation, we need to fix multiple tables in the database and also update the last valid value of the Riemann Sum Integral in the .storage of HomeAssistant.
Otherwise, the next calculation of Riemann Sum Integral will result in an invalid value.
We need to fix:
- In table statistics: Key "state"
- In table statistics: Key "sum"
- In table statistics_shor_term: We only need one entry with the last valid value of key "state" and "sum"
- In table "states": Key "state"
- In file .storage/core.restore_state: Last valid state of entity
This table holds the long-term statistics of all the entities which support statistics (including Riemann Sum Entities).
This table stores values with an interval of 1 hour and holds the entire history of your entities.
For every full hour, HA copies the last value from short_term_statistics into statistics
- We are looping through the table from top to bottom for both keys state and sum.
- We start updating if a value is lower than the previous value. It is required to update ALL following entries in this case.
- The script always tries to use the difference between the previous entries. We don't want to lose the actual history!
This table holds the short_term statistics of the last ~ 10 days. This table is used to show a more precise graph to the user.
This table stores values with an interval of 5 minutes and is not mandatory. But we need to make sure that the last value is set to the last valid value
- We delete the entire history for the entity
- We insert one single entry with the last valid value from table statistics
- HomeAssistant starts to fill this table automatically
This table holds all states for every single entity in your HomeAssistant.
- We are looping through the table from top to bottom
- We start updating if a value is lower than the previous value. It is required to update ALL following entries in this case.
- The script always tries to use the difference between the previous entries. We don't want to lose the actual history!
- In addition: The script fixes the attributes_id to the correct one. Sometimes HA creates a new attributes_id in case of broken calculation
- In addition: The script fixes the key "old_state_id" if the value is NULL
This file holds all the last valid values of your entities.
The Riemann Sum Integration uses this value to calculate the next state for the table states
This script also tries to fix entities that are not Riemann Sum Entities and skips the steps which are not required
This is an untested feature!