Skip to main content

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/)

  1. Navigate to the folder where the virtual environment shall be created (e.g. your home dir):
cd ~
  1. Create virtual environment (this will create a new folder smart_meter_py_env):
python3 -m venv smart_meter_py_env
  1. Activate the virtual environment
source smart_meter_py_env/bin/activate
  1. Upgrade pip and setuptools
python3 -m pip install --upgrade pip setuptools
  1. Install smartplug-energy-controller
pip install smartplug-energy-controller
  1. 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 "/home/ubuntu/smart_meter_py_env/bin/smartplug_energy_controller"

[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:

This project includes a service that performs this requests by using HABApp (https://github.com/spacemanspiff2007/HABApp)

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/heiko/smart_meter_py_env/bin/activate && /home/ubuntu/smart_meter_py_env/bin/oh_to_smartplug_energy_controller"

[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 smart_meter_py_env/lib/python3.xx/site-packages/oh_to_smartplug_energy_controller/log/HABApp.log.

Development

Development is done in wsl2 on ubuntu 22.04. Setting up the development environment on Windows is not supported. But in principal it could be setup as well since no OS specific functionalities are used.

Setup

The project is using poetry for managing packaging and resolve dependencies. To install poetry call install-poetry.sh. This will install poetry itself as well as python and the required packages as a virtual environment in .venv. Example settings for development in VS Code are provided in vscode-settings. (Copy them to .vscode folder) Follow these instructions to enable proper linting and type checking.

Metadata

Release files for smartplug-energy-controller 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smartplug-energy-controller 0.1.7
File Size Uploaded Provenance Actions
smartplug_energy_controller-0.1.7.tar.gz 16.2 kB Without provenance Details for smartplug_energy_controller-0.1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for smartplug-energy-controller 0.1.7
File Interpreter ABI Platform Actions
smartplug_energy_controller-0.1.7-py3-none-any.whl Python 3 none any Details for smartplug_energy_controller-0.1.7-py3-none-any.whl

Total release size: 35.4 kB

Release files / smartplug_energy_controller-0.1.7.tar.gz

Download URL smartplug_energy_controller-0.1.7.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
04971ba1c70dfdeb65822907ae7da431dd79629a3d6fb3e311992905caf69ff0
BLAKE2b-256 checksum
How to use checksums
a7e6e4c934949df63a58e5cc3729327eed57b3f9a82f2ff6df24c563dfb6f81d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / smartplug_energy_controller-0.1.7-py3-none-any.whl

Download URL smartplug_energy_controller-0.1.7-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58ab14acabce9d598bd2ab42b0ce528ac89c5e015771e56d156a45945073c983
BLAKE2b-256 checksum
How to use checksums
22489a6ee40e4dc5f8c987393ec355c284cdaa70d671376f6d77411b7ce8cb82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for smartplug-energy-controller 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page