Recently I bought some TP Link Smart Plugs to monitor the energy usage of some equipment in my house. The Kasa app is fine but I wanted a way to see all the usage in one place. I found this great exporter for Prometheus on GitHub. In this tutorial I am assuming you already have Grafana and Prometheus up and running locally (I have used ubuntu 20.04). There are a lot of guides around and I may do one myself at a later date. I am first going to create a service to run this exporter in, it makes it easier for managing later.

Install/Setup tplink-plug-exporter

  1. Open the terminal on your server and download the latest version of tplink-plug-exporter. wget -c https://github.com/fffonion/tplink-plug-exporter/releases/download/v0.4.0/tplink-plug-exporter_0.4.0_linux_x86_64.tar.gz
  2. Export the files tar xvf tplink-plug-exporter_0.4.0_linux_x86_64.tar.gz
  3. Make a directory for tplink-plug-exporter-explorer sudo mkdir /opt/tplink-plug-exporter
  4. Add a user account to run the service sudo useradd -r tplink-plug-exporter
  5. Move the files to the new directory sudo mv tplink-plug-exporter /opt/tplink-plug-exporter
  6. Change the group permissions to the new tplink-plug-exporter user sudo chgrp tplink-plug-exporter /opt/tplink-plug-exporter
  7. sudo chgrp tplink-plug-exporter /opt/tplink-plug-exporter/tplink-plug-exporter
  8. Set the file so it can be run chmod +x /opt/tplink-plug-exporter/tplink-plug-exporter
  9. Create a service file for tplink-plug-exporter sudo nano /lib/systemd/system/tplink-plug-exporter.service
  10. Paste in the following code.
[Unit]
Description=tplink-plug-exporter

[Service]
ExecStart=/opt/tplink-plug-exporter/tplink-plug-exporter -metrics.listen-addr localhost:9233
WorkingDirectory=/opt/tplink-plug-exporter
Restart=always
User=tplink-plug-exporter

[Install]
WantedBy=multi-user.target
  1. Press ctrl + x and type y then enter to save the file
  2. Reload the system daemon systemctl daemon-reload
  3. Run the service service tplink-plug-exporter start
  4. Enable it to run at start up systemctl enable tplink-plug-exporter
  5. Lastly check the status systemctl status tplink-plug-exporter – press q to exit this view

Setup Prometheus and Grafana

  1. Open your Prometheus settings file. sudo nano /etc/prometheus/prometheus.yml (Your path may vary depending on how you installed it)
  2. Under the heading scrape_configs: add the following code
  3. Replace the 10.0.0.34 with the IP of your TP-link Plug, you can add more IP’s if needed.
- job_name: 'kasa'
    static_configs:
      - targets:
      # IP of your smart plugs
        - 10.0.0.34
    metrics_path: /scrape
    relabel_configs:
      - source_labels : [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        # IP of the exporter
        replacement: localhost:9233

# scrape kasa_exporter itself
  - job_name: 'kasa_exporter'
    static_configs:
      - targets:
        # IP of the exporter
        - localhost:9233
  1. Press ctrl + x and type y then enter to save the file
  2. Enter the following to restart prometheus sudo systemctl restart prometheus
  3. Go to your Prometheus targets URL and check that the service is up, http://10.0.0.5:9090/classic/targets (your IP will differ)
  4. If all is working, head to Grafana and select import a dashboard. Enter the number 10957 
  5. Enter the Dashboard number and press import.
  6. If everything has worked, you should see the dashboard start to display data. (I have also added an additional widget to mine)

Links