Skip to main content

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 provides the post method add_watt_consumption with the value to be provided as a raw data string.

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

Configuration

Everything is configured in the respective .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"
ExecStart=/usr/bin/bash -lc "source /home/ubuntu/smart_meter_py_env/bin/activate && uvicorn --host 0.0.0.0 --port 8000 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 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 request 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:8000

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
ExecStart=/usr/bin/bash -lc "source /home/ubuntu/smart_meter_py_env/bin/activate && habapp -c $HABAPP_CONFIG_FOLDER"

[Install]
WantedBy=multi-user.target

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smartplug_energy_controller-0.0.10.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file smartplug_energy_controller-0.0.10.tar.gz.

File metadata

File hashes

Hashes for smartplug_energy_controller-0.0.10.tar.gz
Algorithm Hash digest
SHA256 c00f659fb815a2e615be2040528209c5b9f3391d4f1d77ce7c570454c7f90e6b
MD5 2e76ecbcf3fa043f5385633e6272058e
BLAKE2b-256 fa2f03fa2baa9c9d154b7a9a0cd96843915dc0895174cc61bd2a2534d3c5d8b1

See more details on using hashes here.

Provenance

File details

Details for the file smartplug_energy_controller-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for smartplug_energy_controller-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 49525daad268c77f02f537b1539130bc98184ef32e953b10a5c27c2f8ea852b1
MD5 5cfa80cd9dbc2e40f58d88a57ad31064
BLAKE2b-256 15edc20cf0845196e7aaa45785b0609d12ca95799ca362f3ed1e23b9ee2c99a7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page