Skip to content

Commit 82b63d2

Browse files
#106 Implemented supporttool to determine the sensor information
1 parent a812ad9 commit 82b63d2

35 files changed

+766
-634
lines changed

MariaDB/Import Energy data into Home Assistant.sql renamed to Database/MariaDB/Import Energy data into Home Assistant.sql

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ CUTOFF NEW METER:
4646
m³ : 25.0 m³
4747
CUTOFF INVALID VALUE:
4848
This value determines when a value is considered to be invalid (too large). The value depends on the unit of measurement of the target sensor!
49+
For instance, this can occur when the imported data is recalculated (since the import shows usage per interval rather than actual readings).
50+
This may produce a large spike at the point where the imported data transitions to the Home Assistant data.
4951
Change this in case a higher/lower diff cutoff is needed to mark a value invalid.
5052
Invalid value detection: (difference between two sensor states > cutoff invalid value)
5153
Wh : 1000000.0 Wh

MariaDB/README.md renamed to Database/MariaDB/README.md

+53-34
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,46 @@ Importing historical energy data into Home Assistant is not simple and requires
2929
### How-to
3030

3131
#### Source data preparation
32-
- See the [generic how-to](../README.md) on how to prepare the source data.
32+
- See the [generic how-to](../../README.md) on how to prepare the source data.
3333

3434
#### Tooling
3535
- Download and install: HeidiSQL https://www.heidisql.com/
36-
- MySQL python library ```pip install mysql-connector-python```
36+
- MySQL python library `pip install mysql-connector-python`
3737

3838
#### Home Assistant preparation
3939
- Create a backup of the Home Assistant database
40-
- Disable recorder while making the backup -> ```Developer tools/Actions/Action: Recorder:disable```
40+
- Disable recorder while making the backup -> `Developer tools/Actions/Action: Recorder:disable`
4141
- Stop the Home Assistant core
42-
- ```Developer tools/Actions/Action: Home Assistant Core Integration: Stop```
42+
- `Developer tools/Actions/Action: Home Assistant Core Integration: Stop`
4343

44-
#### Import the data
45-
- Start ```HeidiSQL```
44+
#### Import the data into a temporary table
45+
- Start `HeidiSQL`
4646
- On first run configure the database connection
4747
- Network type: MariaDB or MySQL (TCP/IP)
4848
- Hostname/IP: hostname/ip-number of your MariaDB machine
4949
- In case MariaDB runs on the same machine as Home Assistant this is the same hostname/ip-number of your Home Assistant instance
5050
- User/password: database user and password
51-
- The database user and password can be found in the connection string in the ```configuration.yaml```
52-
```
51+
- The database user and password can be found in the connection string in the `configuration.yaml`
52+
`
5353
recorder:
5454
db_url: mysql://<user>:<password>@core-mariadb/homeassistant?charset=utf8mb4
55-
```
55+
`
5656
- Port: 3306 (default - In case the Home Assistant add-on is used, verify that the port is exposed to the host)
5757
- <b>EXTRA SAFETY STEP</b>: Create a copy of your existing Home Assistant database so that it is possible to quickly move back to the original database.
58-
- Right click in the left most window and select ```Create new -> database```
59-
- Name: ```homeassistant backup```
60-
- Collation: ```utf8mb4_unicode_ci```
61-
- Right click the ```homeassistant``` database and select ```Export database as SQL```
62-
- Press ```Export``` to copy the ```homeassistant``` database to the ```homeassistant backup``` database
58+
- Right click in the left most window and select `Create new -> database`
59+
- Name: `homeassistant backup`
60+
- Collation: `utf8mb4_unicode_ci`
61+
- Right click the `homeassistant` database and select `Export database as SQL`
62+
- Press `Export` to copy the `homeassistant` database to the `homeassistant backup` database
6363
- Settings:
64-
- Database(s): not selected ```Drop```, selected ```Create```
65-
- Table(s): not selected ```Drop```, selected ```Create```
66-
- Data: ```Insert```
67-
- Output: ```Database```
68-
- Database: ```homeassistant backup```
69-
- In case something goes wrong you can remove the ```homeassistant``` database and rename the ```homeassistant backup``` database to the ```homeassistant``` database.
70-
- Right click the ```homeassistant``` database and select ```Drop```
71-
- Right click the ```homeassistant backup``` database and select ```Edit``` and change the name into ```homeassistant``` and press ```Ok```
64+
- Database(s): not selected `Drop`, selected `Create`
65+
- Table(s): not selected `Drop`, selected `Create`
66+
- Data: `Insert`
67+
- Output: `Database`
68+
- Database: `homeassistant backup`
69+
- In case something goes wrong you can remove the `homeassistant` database and rename the `homeassistant backup` database to the `homeassistant` database.
70+
- Right click the `homeassistant` database and select `Drop`
71+
- Right click the `homeassistant backup` database and select `Edit` and change the name into `homeassistant` and press `Ok`
7272
- Run the `ImportData.py` script from the Datasources directory to import the generated CSV files and use the following command-line parameters:
7373
- `--db-type mariadb`
7474
- `--host HOST` Host as defined in the HeidiSQL database connection
@@ -78,14 +78,31 @@ Importing historical energy data into Home Assistant is not simple and requires
7878
- `--csv-file CSV_FILE [CSV_FILE ...]` Location of the CSV files generated in the source data preparation step, wildcards are allowed
7979
- `--verbose`<br><br>
8080
Example:<br>
81-
```python ImportData.py --db-type mariadb --host localhost --user root --database homeassistant --csv-file "data\*.csv" --verbose```
81+
`python ImportData.py --db-type mariadb --host localhost --user root --database homeassistant --csv-file "data\*.csv" --verbose`
8282

83-
- Load SQL file ```Import Energy data into Home Assistant.sql``` from the MariaDB directory (File -> Load SQL file - Yes on auto-detect file encoding)
83+
#### Load import script
84+
- Load SQL file `Import Energy data into Home Assistant.sql` from the MariaDB directory (File -> Load SQL file - Yes on auto-detect file encoding)
8485
- Validate the schema version of the database (Select table: schema_changes and select the data tab on the right and scroll down to the bottom)
85-
- The script has been tested with schema version 48. With higher versions you should validate if the structure of the ```statistics``` and ```short_term_statistics``` tables have changed.
86-
- Used fields in table ```statistics```: ```metadata_id```, ```state```, ```sum```, ```start_ts```, ```created_ts```
87-
- Used fields in table ```short_term_statistics```: ```sum```
88-
- Lookup in the ```statistics_meta``` table the IDs of the sensors (Select table: ```statistics_meta``` and select the data tab on the right. You can use ```filter``` to find the ID of the sensor, For instance: ```statistic_id LIKE '%sensor.gas_meter%'```)
86+
- The script has been tested with schema version 48. With higher versions you should validate if the structure of the `statistics` and `short_term_statistics` tables have changed.
87+
- Used fields in table `statistics`: `metadata_id`, `state`, `sum`, `start_ts`, `created_ts`
88+
- Used fields in table `short_term_statistics`: `sum`
89+
90+
#### Determine the sensor configuration
91+
##### Option 1: Use the `Sensors.py` script (GUI)
92+
- Run the `Sensors.py` script from the Database directory to determine the sensor configuration and use the following command-line parameters:
93+
- `--db-type mariadb`
94+
- `--host HOST` Host as defined in the HeidiSQL database connection
95+
- `--user USER` Username as defined in the HeidiSQL database connection
96+
- `--password PASSWORD` Password as defined in the HeidiSQL database connection
97+
- `--database homeassistant`<br><br>
98+
Example:<br>
99+
`python Sensors.py --db-type mariadb --host localhost --user root --database homeassistant`
100+
- Select the target sensors from Home Assistant and the corresponding source import name identifiers.
101+
- Determine the unit of measurement of the source data (`Source unit`). The script will automatically determine the correct correction factor based on the unit of measurement of the source data.
102+
- Determine the `cutoff_new_meter` and `cutoff_invalid_value`. The script will automatically fill in the default values based on the unit of measurement of the target sensor. The different cutoffs are described in the script.
103+
- Press `Generate SQL` which will generate the SQL statements that need to be replaced in the `Import Energy data into Home Assistant.sql` SQL file.
104+
##### Option 2: Manually lookup the sensor information
105+
- Lookup in the `statistics_meta` table the IDs of the sensors (Select table: `statistics_meta` and select the data tab on the right. You can use `filter` to find the ID of the sensor, For instance: `statistic_id LIKE '%sensor.gas_meter%'`)
89106
- The names of the sensors can be looked up in the Home Assistant Energy dashboard (Settings -> Dashboards -> Energy).
90107
<br>Example:
91108
```
@@ -100,14 +117,16 @@ Importing historical energy data into Home Assistant is not simple and requires
100117
451 sensor.battery_energy_feed_out recorder kWh
101118
653 sensor.watermeter_quantity_m3 recorder m³
102119
```
103-
- Change the script and remove/comment out the lines of the sensors that are not needed. They can be found at the top of the script by looking up the lines where ```/* Change */``` has been added in the SQL statement.
104-
- Change the script and update the IDs according to the found IDs in the ```statistics_meta``` table.
105-
They can be found at the top of the script by looking up the lines where ```/* Change */``` has been added in the SQL statement.
106-
- Determine the ```correction``` value based on the ```unit_of_measurement``` of the sensor and the used datasource. The unit of measurement of the datasource can be found in the readme of the datasource.
120+
- Change the script and remove/comment out the lines of the sensors that are not needed. They can be found at the top of the script by looking up the lines where `/* Change */` has been added in the SQL statement.
121+
- Change the script and update the IDs according to the found IDs in the `statistics_meta` table.
122+
They can be found at the top of the script by looking up the lines where `/* Change */` has been added in the SQL statement.
123+
- Determine the `correction` value based on the `unit_of_measurement` of the sensor and the used datasource. The unit of measurement of the datasource can be found in the readme of the datasource.
107124
The different corrections are described in the script.
108-
- Determine the ```cutoff_new_meter``` and ```cutoff_invalid_value``` values based on the unit of measurement of the target sensor. The different cutoffs are described in the script.
125+
- Determine the `cutoff_new_meter` and `cutoff_invalid_value` values based on the unit of measurement of the target sensor. The different cutoffs are described in the script.
126+
127+
#### Execute import script
109128
- Execute the SQL and wait for it to complete. (Please be patient because this can take some time!)
110-
- For the first run the ```COMMIT``` statement at the end of the script can be commented out so that the changes are not written to the database.
129+
- For the first run the `COMMIT` statement at the end of the script can be commented out so that the changes are not written to the database.
111130
This makes it possible to test the script and see whether it completes without errors.
112131
- In case of an error correct the error and execute the script again. The script will automatically rollback any changes before trying again.
113132

0 commit comments

Comments
 (0)