This is a small docker image to poll TP-Link Kasa and Tapo smart-plugs to retrieve energy usage readings and send them into InfluxDB.
In effect, this is an amalgamated and tidied version of scripts that I've previously published:
A detailed example of usage can be seen in "Energy usage Monitoring With TP-Link Smart Sockets and InfluxDB"
Configuration is achieved via a YAML file, there's an example to get you started in example/config.yml
The config file contains 4 sections
- poller
- tapo
- kasa
- influxdb
The poller
section contains configuration information for the polling script itself
poller:
# If true, run in an infinite loop
persist: true
# Interval in seconds between runs
interval: 20
Each of the remaining sections can have multiple devices:
tapo:
# Tapo devices require that you log in with the credentials
# that you use to log into the app
#
user: "[email protected]"
passw: "mysecretpass"
devices:
-
name: "washing-machine"
ip : 192.168.3.152
-
name: "big-fridge"
ip : 192.168.3.153
Once collected, stats will be written to all listed InfluxDB outputs:
influxdb:
-
name: "Cloud"
url: "https://foo.example.com"
token: "aaabbbccc=="
org: "my org"
bucket: "telegraf"
-
name: "local"
url: "http://192.168.3.84:8086"
token: ""
org: ""
bucket: "testing_db"
Outputs are written using the v2 API, so the upstream can be any of the following
- InfluxDB OSS/Enterprise >= 1.8.0
- InfluxDB OSS 2.x
- InfluxDB Cloud
- Telegraf (with influxdb_v2_listener)
The container is currently designed to be invoked via cron (although utilities/tp-link-to-influxdb#4 added the option of using a persistent container with an internal timer)
The configuration file needs to be exported into the container at /config.yml
(if for some reason you wish to override this, you can use the environment variable CONF_FILE
to tell the script where to find the config file)
docker run --rm \
--name="tplink_to_influxdb" \
-v $PWD/config.yml:/config.yml \
bentasker12/tplink_to_influxdb:latest
The script will write into a measurement called power_watts
Tags:
host
: the name of the monitored device
Fields:
consumption
: current reading (unit: W)watts_today
: reported usage today (Wh)
Although docker
is the easiest way to run the script, it can also be run directly once you've installed a few dependencies.
You'll need gcc
, g++
and make
installed: one of the dependencies is itself dependant on PycryptoDome which includes some bits which need compiling.
sudo apt-get install build-essential
sudo pip install pyyaml influxdb-client PyP100 python-kasa
You'll need to tell the script where to find the config file
export CONF_FILE="/path/to/config"
And then, finally, invoke the script
app/collect.py
By default the system mostly uses the default loglevels. The exception to this is the PyP100
module which can be quite noisy when the loglevel is set lower than logging.CRITICAL
.
If further information is required during troubleshooting, log verbosity can be increased by adding loglevels
attributes to the poller
section of the config file
poller:
loglevel: "debug"
I use Tapo P110s and Kasa KP115's, so this is known to definitely work with those.
You can check whether your device is supported by looking at the support matrix on the underlying libraries
In v1.2.1 of the Tapo firmware, TP-Link changed the way that authentication happens. Devices that have received this firmware update no longer work with the version of PyP100
in PyPi.
There is, however a fork and utilities/tp-link-to-influxdb#7 adds support for using it.
In order to install and use a copy of the forked library, run
pip3 install git+https://github.com/almottier/TapoP100.git@main
export CONF_FILE="/path/to/config"
./app/collect.py
This should work with devices running new firmware as well as those that haven't been updated.
However, the mixed mode support is implemented at a slight efficiency cost: it's necessary to try both auth modes to find one that succeeds.
It's therefore possible to define, in config, which should be used:
devices:
-
name: "big-fridge"
ip : 192.168.3.153
auth: "almottier_old"
-
name: "slow-cooker"
ip: 192.168.3.164
auth: "package_defaults"
-
name: "washing-machine"
ip: 192.168.3.167
auth: "all"
Valid values to auth
are:
all
: the default, will try each in turnpackage_defaults
: use whatever the default is for the module in use (new
for almottier's fork, old forPyPi
)almottier_old
: Use theold
protocol with the Almottier fork - will fail if the PyPi version is in use instead
If you're planning a setup you may be trying to decide which devices to buy.
Tapo is the newer family, so you'll get better/longer support in the app. But they also have a more complicated authentication model, which is reliant on external connectivity (at the script's end - you can still restrict the plug's connectivity once you've provisioned it).
Kasa plugs are more expensive and (increasingly) harder to get hold off. However, they support fully local comms.
Given the choice, personally, I'd buy Kasa every time.
Copyright (c) 2023 Ben Tasker.
Released under BSD-3-Clause