Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chocomega committed Apr 3, 2023
0 parents commit 63af372
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 chocomega

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# MyElectricalData Statistics Importer for Home Assistant

This Python script retrieves statistics from [MyElectricalData](https://github.com/m4dm4rtig4n/myelectricaldata/)'s cache database and imports them into Home Assistant via the [WebSocket API](https://developers.home-assistant.io/docs/api/websocket/).

Long Term Statistics will be created in Home Assistant and usable in the Energy Dashboard.

## Pre-requisites
- Python 3.4 or higher
- A running instance of Home Assistant 2022.10.0 or higher
- A running instance of MyElectricalData 0.8.13-11 or higher
- MyElectricalData must be configured with the cache and hourly details enabled, cf. [wiki](https://github.com/m4dm4rtig4n/myelectricaldata/wiki/03.-Configuration)
- A Long-lived access token created in Home Assistant

## Installation

1. Clone this repository or download the source code
2. The folder containing the source code should be preferrably located in Home Assistant's folder `config`
2. If you don't intend to execute the script from Home Assistant environment, install the required packages by running the following command:
```console
pip install -r requirements.txt
```

## Configuration

Rename the `script_config.example.yaml` file to `script_config.yaml` and edit it to suit your needs. All the following keys are mandatory:

- `ha_url`: URL of your Home Assistant instance
- `ha_access_token`: Long-lived access token for your Home Assistant instance
- `med_cache_db_path`: Path to the cache database of MyElectricalData
- `med_config_path`: Path to the configuration file of MyElectricalData

## Usage

```console
python statistics_importer.py [-h] [-d] [-f]
```

The script accepts the following options:

- `-h, --help`: show the help message and exit
- `-d, --delete-all`: delete all the statistics imported by this tool in Home Assistant, no import is done
- `-f, --force-all`: force the import of all statistics regardless of the last one already in Home Assistant

## Automation with Home Assistant
The script can be accessed as a service in Home Assistant with the [Shell Command integration](https://www.home-assistant.io/integrations/shell_command/).

Example of `configuration.yaml` entry in Home Assistant, assuming the folder `statistics_importer` is located in the folder `config`:
```yaml
shell_command:
statistics_import: python statistics_importer/statistics_importer.py
statistics_delete_all: python statistics_importer/statistics_importer.py -d
```
You can then create an automation to call the service `shell_command.statistics_import` periodically or when MyElectricalData cache is updated.

## Warning
- Home Assistant may take some time to display the newly created statistics. Please wait, they will eventually show up in the UI.
- In case things go wrong you can still delete the long term statistics created by this script. To do so, it is advised to remove them from the Energy Dashboard first then execute the script with the option `-d`.

## License

This script is released under the [MIT License](https://opensource.org/licenses/MIT).
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aiohttp==3.8.4
pytz==2023.2
PyYAML==6.0
13 changes: 13 additions & 0 deletions script_config.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Configuration for Statistics Importer script

# URL of Home Assistant
ha_url: "localhost:8123"

# Long-Lived access token from Home Assistant
ha_access_token: "xxxxxx"

# File path the to cache database of MyElectricalData
med_cache_db_path: "../myelectricaldata/cache.db"

# Path to the configuration file of MyElectricalData
med_config_path: "../myelectricaldata/config.yaml"
Loading

0 comments on commit 63af372

Please sign in to comment.