Turning smartplugs on/off depending on current electricity consumption
Project description
smartplug-energy-controller
A microservice to turn a smartplug on/off depending on current electricity consumption. The intention of this service is to use all energy you produced, e.g with a balcony power plant, by e.g. loading a portable battery. This can be achieved by plug in your battery into a smartplug. The smartplug is turned on/off dynamically, depending on your current electricity consumption.
The service is especially useful when your electricity meter supports OBIS 1.8.x only (no OBIS 2.8.x). In such a scenario you can give this service your current electricity meter value (obtained watt from provider) and your current watt production (e.g. from a balcony power plant) and it calculates a reasonable point when to turn your smartplugs on/off.
Installation
The python package can be installed from PyPi (https://pypi.org/project/smartplug-energy-controller/)
- Navigate to the folder where the virtual environment shall be created (e.g. your home dir):
cd ~
- Create virtual environment (this will create a new folder smart_meter_py_env):
python3 -m venv smart_meter_py_env
- Activate the virtual environment
source smart_meter_py_env/bin/activate
- Upgrade pip and setuptools
python3 -m pip install --upgrade pip setuptools
- Install smartplug-energy-controller
pip install smartplug-energy-controller
- Provide environment variables (e.g. in your ~/.profile).
CONFIG_PATH=full/path/to/config.yml
SMARTPLUG_ENERGY_CONTROLLER_PORT=8000
Configuration
Everything is configured in the respective config.yml file. See https://github.com/die-bauerei/smartplug-energy-controller/blob/main/tests/data/config.example.yml
Autostart after reboot and on failure
Create a systemd service by opening the file /etc/systemd/system/smartplug_energy_controller.service and copy paste the following contents. Replace User/Group/ExecStart accordingly.
[Unit]
Description=smartplug_energy_controller
Documentation=https://github.com/die-bauerei/smartplug-energy-controller
After=network-online.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
UMask=002
Restart=on-failure
RestartSec=5s
Environment="CONFIG_PATH=full/path/to/config.yml"
Environment="SMARTPLUG_ENERGY_CONTROLLER_PORT=8000"
ExecStart=/usr/bin/bash -lc "source /home/ubuntu/smart_meter_py_env/bin/activate && uvicorn --host 0.0.0.0 --port $SMARTPLUG_ENERGY_CONTROLLER_PORT smartplug_energy_controller.app:app > /dev/null"
[Install]
WantedBy=multi-user.target
Now execute the following commands to enable autostart:
sudo systemctl --system daemon-reload
sudo systemctl enable smartplug_energy_controller.service
It is now possible to start, stop, restart and check the status of smartplug-energy-controller with:
sudo systemctl start smartplug_energy_controller.service
sudo systemctl stop smartplug_energy_controller.service
sudo systemctl restart smartplug_energy_controller.service
sudo systemctl status smartplug_energy_controller.service
Usage of Tapo Smart Plugs
The service can control Tapo Smart Plugs via the plugp100 library (https://pypi.org/project/plugp100/). Have a look at the example config at https://github.com/die-bauerei/smartplug-energy-controller/blob/main/tests/data/config.example
Usage in conjunction with openHAB
To use this service you need to get the consumption values from your smart-meter. There are of course lots of different ways to achieve this. A possible setup could include:
- Read data from your smart-meter and push them to openHAB:
- Let openHAB send the requests to this service.
This project includes a service that performs this requests by using HABApp (https://github.com/spacemanspiff2007/HABApp) You can start habapp by e.g.
HABAPP_CONFIG_FOLDER=$(source /home/ubuntu/smart_meter_py_env/bin/activate && pip show smartplug_energy_controller | grep Location | sed "s/Location: //")/oh_to_smartplug_energy_controller
source /home/ubuntu/smart_meter_py_env/bin/activate && habapp --config $HABAPP_CONFIG_FOLDER
NOTE: Make sure to first start smartplug_energy_controller as this will setup the configuration for HABApp.
The service expects the smartplug_energy_controller API on http://localhost:$SMARTPLUG_ENERGY_CONTROLLER_PORT
Create a systemd service /etc/systemd/system/oh_to_smartplug_energy_controller.service to setup autostart for this service as well:
[Unit]
Description=Post smart meter values from openHAB to smartplug-energy-controller
Documentation=https://github.com/die-bauerei/smartplug-energy-controller
After=smartplug_energy_controller.service
[Service]
Type=simple
User=ubuntu
Group=ubuntu
UMask=002
Restart=on-failure
RestartSec=5s
#Provide environment variable (e.g. in your ~/.profile). Assignment example see above
Environment="SMARTPLUG_ENERGY_CONTROLLER_PORT=8000"
ExecStart=/usr/bin/bash -lc "source /home/ubuntu/smart_meter_py_env/bin/activate && habapp -c $HABAPP_CONFIG_FOLDER"
[Install]
WantedBy=multi-user.target
By setting up a connection to your openHAB instance you can additionally use any Smart Plug you have configured inside your openHAB instance. Have a look at the example config at https://github.com/die-bauerei/smartplug-energy-controller/blob/main/tests/data/config.example
Troubleshooting
- Have a look at the log-file you have given in your config.yml
- Have a look at the HABApp log-file located in $HABAPP_CONFIG_FOLDER/log/HABApp.log
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 Distribution
File details
Details for the file smartplug_energy_controller-0.1.4.tar.gz
.
File metadata
- Download URL: smartplug_energy_controller-0.1.4.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15ba9438ce791f5ff94fb05942a509e2626d31b2005bc579ee370510b21d8a65 |
|
MD5 | a55e93c9f9bf38a66144f61dc2d0cb4c |
|
BLAKE2b-256 | 3cae5286754a8e78e59f62f751a6874d0cb20354b454f09209ba093b291dda40 |
Provenance
File details
Details for the file smartplug_energy_controller-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: smartplug_energy_controller-0.1.4-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a54487db55c25e1727291b2d40675e14623800a8a7bfb96f0f958a61a37f040 |
|
MD5 | 8d68b7d1eb948a580e01eac3c7d8a454 |
|
BLAKE2b-256 | b9e5fdbb559a263c5737b1a37830947c5dff72f62f6cc6390a071f748ba04dca |