An Energy Management System for Home Assistant
Project description
emhass
EMHASS: Energy Management for 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
... or the compiled whl file:
pip install emhass-X.X.X-py3-none-any.whl
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.yaml
and secrets.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).
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
For example, the following line command can be used to perform a day-ahead optimization task:
emhass --action 'dayahead-optim' --config '/home/user/emhass'
Before running any valuable command you need to modify the config.yaml and secrets.yaml files.
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'
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'
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 desider 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
action:
- service: homeassistant.turn_on
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 emhass using the provided setup.py file:
conda activate emhass-dev
python setup.py install
Add more packages if needed, this is optional if using spyder:
conda install spyder-kernels
Update the build package:
python3 -m pip install --upgrade build
And generate distribution archives with::
python3 -m build
In Spyder you can use CTRL+F6 and add the needed arguments in the "Command line options". The runfile command in the ipython console may look like this:
runfile('/home/user/emhass/src/emhass/command_line.py', args='--action "dayahead-optim" --config "/home/user/emhass"', wdir='/home/user/emhass/src/emhass')
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:
make clean
make html
sphinx-apidoc -o ./ ../src/emhass/
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 Distribution
Built Distributions
File details
Details for the file emhass-0.1.0.tar.gz
.
File metadata
- Download URL: emhass-0.1.0.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11aaa2cb56d6bcd86b545fc844934ecfbd6ed38330859f329ce07d8f3f04875a |
|
MD5 | 00bfe9352eb957a0a13df349752b7d18 |
|
BLAKE2b-256 | a5ed7281c5023f1a83f99650c765d21bef41c5076d49ed7898acb56199f8f1a2 |
File details
Details for the file emhass-0.1.0-py3.8.egg
.
File metadata
- Download URL: emhass-0.1.0-py3.8.egg
- Upload date:
- Size: 36.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a432116b03d17dd8c38e2b27e61a8ff813fb1f19286664588a358e90abedf5c |
|
MD5 | 97f6e356d56d143bf40ea01b6cb70728 |
|
BLAKE2b-256 | 0094a912325fd3a19ec3ee11bb53590edee4e51bce9bf1f3d0bc857c9fc2721f |
File details
Details for the file emhass-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: emhass-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3198d9524418a954fd4dca7d4ab3366e484de03a709b6ab885cace8b535e19e |
|
MD5 | a76027feb53fe308cfe3533c6f831153 |
|
BLAKE2b-256 | 59d686850e66bfc2c9ef9c4b5a68c1c16b42fcbc9c61bcd7a4b19de9d6016fa1 |