Skip to content

bentasker/tplink_to_influxdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TP-Link Smartplugs to InfluxDB

Background

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"


Usage

Configuration

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


Invocation

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

Output

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)

Running Without Docker

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

Loglevels

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" 

Other Stuff

Device Support

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


Tapo Authentication Mechanism Update

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 turn
  • package_defaults: use whatever the default is for the module in use (new for almottier's fork, old for PyPi)
  • almottier_old: Use the old protocol with the Almottier fork - will fail if the PyPi version is in use instead

Tapo vs Kasa

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.


License

Copyright (c) 2023 Ben Tasker.

Released under BSD-3-Clause

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published