An Energy Management System for Home Assistant
Project description
Energy Management for Home Assistant
EHMASS is a Python module designed to optimize your home energy interfacing with Home Assistant.
Context
This module was conceived as an energy management optimization tool for residential electric power consumption and production systems. The goal is to optimize the energy use in order to maximize autoconsumption. The main study case is a household where we have solar panels, a grid connection and one or more controllable (deferrable) electrical loads. Including an energy storage system using batteries is also possible in the code. The package is highly configurable with an object oriented modular approach and a main configuration file defined by the user. EMHASS was designed to be integrated with Home Assistant, hence it's name. Installation instructions and example Home Assistant automation configurations are given below.
The main dependencies of this project are PVLib to model power from a PV residential installation and the PuLP Python package to perform the actual optimizations using the Linear Programming approach.
The complete documentation for this package is available here.
Installation
It is recommended to install on a virtual environment.
For this you will need virtualenv
, install it using:
sudo apt install python3-virtualenv
Then create and activate the virtual environment:
virtualenv -p /usr/bin/python3 emhassenv
cd emhassenv
source bin/activate
Install using the distribution files:
python3 -m pip install emhass
Clone this repository to obtain the example configuration files. We will suppose that this repository is cloned to:
/home/user/emhass
This will be the root path containing the yaml configuration files (config_emhass.yaml
and secrets_emhass.yaml
) and the different needed folders (a data
folder to store the optimizations results and a scripts
folder containing the bash scripts described further below).
To upgrade the installation in the future just use:
python3 -m pip install --upgrade emhass
Usage
To run a command simply use the emhass
command followed by the needed arguments.
The available arguments are:
--action
: That is used to set the desired action, options are:perfect-optim
,dayahead-optim
andpublish-data
--config
: Define path to the config.yaml file (including the yaml file itself)--costfun
: Define the type of cost function, this is optional and the options are:profit
(default),cost
,self-consumption
--log2file
: Define if we should log to a file or not, this is optional and the options are:True
orFalse
(default)
For example, the following line command can be used to perform a day-ahead optimization task:
emhass --action 'dayahead-optim' --config '/home/user/emhass/config_emhass.yaml' --costfun 'profit'
Before running any valuable command you need to modify the config_emhass.yaml
and secrets_emhass.yaml
files. These files should contain the information adapted to your own system. To do this take a look at the special section for this in the documentation.
Home Assistant integration
To integrate with home assistant we will need to define some shell commands in the configuration.yaml
file and some basic automations in the automations.yaml
file.
In configuration.yaml
:
shell_command:
dayahead_optim: /home/user/emhass/scripts/dayahead_optim.sh
publish_data: /home/user/emhass/scripts/publish_data.sh
And in automations.yaml
:
- alias: EMHASS day-ahead optimization
trigger:
platform: time
at: '05:30:00'
action:
- service: shell_command.dayahead_optim
- alias: EMHASS publish data
trigger:
- minutes: /5
platform: time_pattern
action:
- service: shell_command.publish_data
In these automations the optimization is performed everyday at 5:30am and the data is published every 5 minutes.
Create the file dayahead_optim.sh
with the following content:
#!/bin/bash
. /home/user/emhassenv/bin/activate
emhass --action 'dayahead-optim' --config '/home/user/emhass/config_emhass.yaml'
And the file publish_data.sh
with the following content:
#!/bin/bash
. /home/user/emhassenv/bin/activate
emhass --action 'publish-data' --config '/home/user/emhass/config_emhass.yaml'
Then specify user rights and make the files executables:
sudo chmod -R 755 /home/user/emhass/scripts/dayahead_optim.sh
sudo chmod -R 755 /home/user/emhass/scripts/publish_data.sh
sudo chmod +x /home/user/emhass/scripts/dayahead_optim.sh
sudo chmod +x /home/user/emhass/scripts/publish_data.sh
The final action will be to link a sensor value in Home Assistant to control the switch of a desired controllable load. For example imagine that I want to control my water heater and that the publish-data
action is publishing the optimized value of a deferrable load that I have linked to my water heater desired behavior. In this case we could use an automation like this one below to control the desired real switch:
automation:
trigger:
- platform: numeric_state
entity_id:
- sensor.p_deferrable1
above: 0.1
action:
- service: homeassistant.turn_on
entity_id: switch.water_heater
A second automation should be used to turn off the switch:
automation:
trigger:
- platform: numeric_state
entity_id:
- sensor.p_deferrable1
below: 0.1
action:
- service: homeassistant.turn_off
entity_id: switch.water_heater
Development
To develop using Anaconda:
conda create --name emhass-dev python=3.8 pip=21.0.1
Then activate environment and install the required packages using:
pip install -r docs/requirements.txt
Add emhass
to the Python path using the path to src
, for example:
/home/user/emhass/src
Update the build package:
python3 -m pip install --upgrade build
And generate distribution archives with:
python3 -m build
Create a new tag version:
git tag vX.X.X
git push origin --tags
Upload to pypi:
twine upload dist/*
To generate de documentation we will use Sphynx, the following packages are needed:
pip install sphinx==3.5.4 sphinx-rtd-theme==0.5.2 myst-parser==0.14.0
The actual documentation is generated using:
sphinx-apidoc -o ./ ../src/emhass/
make clean
make html
TODO
New functionalities
- Create a plotting script to visualize the optimization results.
- Propose multiple types of cost functions: profit maximization, self-consumption maximization, etc.
- Integrate the possibility of variable tariffs, for purshasing and selling energy to the grid.
- Implement an energy management with a Model Predictive Control approach. Consider implementing the receiding horizon approach.
- Introduce the modeling of constraints during optimization for a thermal energy storage.
- Add elasticity to LP formulation in case on infeasible solution.
Related to forecasting improvement
- Define the type of forecast that should be used from the configuration file.
- Move get_load_unit_cost from optimization to forecast class: define forecast methods for load and PV production prices.
- Improve load forecasting using a time series forecast algorithm. Some tests were made with fbprophet but results are not completly satisfactory. The model needs some regressors for more accuracy.
- Test with LTSM with or without Autoencoders.
Packaging, HA integration, testing
- EMHASS hass been tested in Home Assistan Core. It need to be tested on Home Assistant Operating System and Home Assistant Container.
- Create an EMHASS add-on for even easier installation on Home Assistant Operating System and Home Assistant Supervised.
- Package everything in a docker container.
- Improve testing to be used with no running hass instance.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file emhass-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: emhass-0.2.2-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec40ae35974ea32e8fccc7f0c071be7603e8672f9d40c3fa7193cb678b3566f6 |
|
MD5 | a7e8076752afe231133e90d355227ca7 |
|
BLAKE2b-256 | e97af68dce44d23ddbaf1e2605e4eae688f31bde92942b1bfd6ac0535dafea5c |