Monitoring with Smart Plugs and Prometheus

Setting up Smart Plugs with Tasmota and Prometheus
As a DIY electronics enthusiast, you're likely familiar with home automation and monitoring. In this article, we'll explore how to track energy usage with smart plugs, Prometheus, and Grafana. We'll start by setting up smart plugs with Tasmota and Prometheus.
Tasmota is an open-source firmware that can be flashed onto various smart plugs, including the TP-LINK HS100 Wi-Fi smart plug [1]. To set up Tasmota on your smart plug, follow these steps:
- Download the Tasmota firmware and flash it onto your smart plug.
- Configure Tasmota using the web interface or the Tasmota CLI.
- Enable the "Power Meter" feature in Tasmota to collect power meter data.
Once you've set up Tasmota on your smart plug, you'll need to configure Prometheus to collect the power meter data. You can use the tasmota-power-exporter solution [1] to scrape the plugs' metrics once per second.
Tasmota Power Exporter Configuration
The tasmota-power-exporter solution requires configuration to scrape the smart plug's metrics. You can configure it using the following parameters:
tasmota_url: The URL of the Tasmota instance running on the smart plug.tasmota_apikey: The API key for the Tasmota instance.scrape_interval: The interval at which to scrape the smart plug's metrics.
Here's an example of how to configure the tasmota-power-exporter solution:
tasmota-power-exporter.yml
tasmota_url: http://smartplug.local:8080
tasmota_apikey: YOUR_API_KEY
scrape_interval: 1s
Prometheus Configuration
Once you've configured the tasmota-power-exporter solution, you'll need to configure Prometheus to collect the power meter data. You can do this by adding the following configuration to your prometheus.yml file:
scrape_configs:
- job_name: 'tasmota-power-exporter'
static_configs:
- targets: ['localhost:9091']
This will tell Prometheus to scrape the tasmota-power-exporter solution once per second.
Collecting Power Meter Data with Prometheus Remote Write
To collect power meter data from your smart plugs, you'll need to configure Prometheus to use the tasmota-power-exporter solution. Here's an example of how to do this:
prometheus.yml
global:
scrape_interval: 1m
remote_write:
- url: 'https://uptrace.io/api/prometheus'
username: 'your_username'
password: 'your_password'
This will tell Prometheus to send the collected power meter data to Uptrace.
Visualizing Energy Usage with Grafana
Once you've collected power meter data from your smart plugs, you can use Grafana to visualize energy usage. To do this, follow these steps:
- Install Grafana on your server.
- Configure Grafana to use Prometheus as a data source.
- Create a new dashboard in Grafana to display energy usage data.
Here's an example of what a Grafana dashboard might look like:

In this example, we're displaying average energy consumption for each energy source. You can customize the dashboard to display other metrics, such as peak energy consumption or total energy consumption.
Integrating Uptrace with Prometheus and Grafana
Uptrace supports the Prometheus Remote Write protocol, which allows Prometheus to send collected metrics data to a long-term storage solution such as Uptrace [2]. To integrate Uptrace with Prometheus and Grafana, follow these steps:
- Install Uptrace on your server.
- Configure Prometheus to use Uptrace as a remote write destination.
- Configure Grafana to use Uptrace as a data source.
Here's an example of how to configure Prometheus to use Uptrace as a remote write destination:
prometheus.yml
global:
scrape_interval: 1m
remote_write:
- url: 'https://uptrace.io/api/prometheus'
username: 'your_username'
password: 'your_password'
This will tell Prometheus to send the collected metrics data to Uptrace.
Security Considerations for Smart Plug Communication
When communicating with smart plugs, it's essential to consider security. To ensure secure communication, follow these best practices:
- Use a secure protocol, such as HTTPS, to communicate with smart plugs.
- Use authentication and authorization mechanisms to prevent unauthorized access.
- Use encryption to protect data transmitted between smart plugs and your server.
Here's an example of how to use HTTPS to communicate with a smart plug:
curl -X GET 'https://smartplug.com/api/data' -H 'Authorization: Bearer your_token'
In this example, we're using HTTPS to communicate with the smart plug and authenticating using a Bearer token.
Table: Comparison of Smart Plug Communication Protocols
| Protocol | Security Features |
|---|---|
| HTTPS | Encryption, Authentication, Authorization |
| TCP |
In this table, we're comparing the security features of HTTPS and TCP. HTTPS provides encryption, authentication, and authorization, making it a more secure choice for smart plug communication.
Diagram: Collecting Power Meter Data with Prometheus Remote Write
In this diagram, we're showing how the tasmota-power-exporter solution collects power meter data from smart plugs and sends it to Uptrace. The client requests data from the server, which sends the collected data to Uptrace.
Figure: Visualizing Energy Usage with Grafana
In this figure, we're showing how a client requests data from the API gateway, which authenticates the request and sends it to the auth service. The auth service then forwards the request to three workers, which write the data to Postgres.
Trade-Offs and Limitations
When using smart plugs and Prometheus to monitor energy usage, there are several trade-offs and limitations to consider:
- Accuracy: The accuracy of the power meter data collected by the smart plugs may not be 100%. The data may be affected by factors such as power quality issues or calibration errors.
- Scalability: As the number of smart plugs increases, the amount of data collected by Prometheus may become overwhelming. You may need to implement a data retention policy to prevent data from becoming too large.
- Security: As with any network-connected device, there is a risk of unauthorized access to the smart plugs and the data they collect. You should ensure that the smart plugs and the server running Prometheus are properly secured.
Conclusion
In this article, we've explored how to use smart plugs and Prometheus to monitor energy usage. We've covered the setup and configuration of Tasmota and Prometheus, as well as the integration of Uptrace with Prometheus and Grafana. We've also discussed the security considerations for smart plug communication and the trade-offs and limitations of using smart plugs and Prometheus.
References
[3] The free version of Power BI has never limited the user in any way for their needs. from: https://news.ycombinator.com/item?id=26357387 supporting quote: The free version has never limited me in any way for my needs.
[3] Metabase is an open source BI tool that works out of the box and is low maintenance. from: https://news.ycombinator.com/item?id=26357387 supporting quote: It’s open source BI tool which works nice out of the box and it’s low maintenance.
[3] Grafana can be used for creating dashboards after the data structures are defined. from: https://news.ycombinator.com/item?id=26357387 supporting quote: b) Grafana for dashboards, once the data structures are defined.
[2] Uptrace supports Prometheus Remote Write protocol, allowing Prometheus to send collected metrics data to a long-term storage solution such as Uptrace. from: https://uptrace.dev/blog/uptrace-v16.html supporting quote: Starting with the v1.6 release, Uptrace natively supports Prometheus Remote Write protocol, which allows Prometheus to send its collected metrics data to a long-term storage solution such as Uptrace.
[2] You can use Uptrace as a Prometheus data source in Grafana, allowing you to use existing Grafana dashboards with the Uptrace data source. from: https://uptrace.dev/blog/uptrace-v16.html supporting quote: You can also use Uptrace as a Prometheus data source in Grafana. Uptrace uses the original Prometheus engine so all Prometheus queries should be supported and you should be able to use existing Grafana dashboards with the Uptrace data source.
[2] Uptrace supports service graphs, providing a visual representation of service interactions, dependencies, and performance metrics. from: https://uptrace.dev/blog/uptrace-v16.html supporting quote: Service Graphs provide a visual representation of service interactions, dependencies, and performance metrics. They are built by analyzing span relationships and require certain span attributes.
[2] You can create annotations in Uptrace using an HTTP POST request to add labels or notes to charts, providing additional information or context. from: https://uptrace.dev/blog/uptrace-v16.html supporting quote: You can create annotations by sending an HTTP POST request to the Uptrace API, for example, when deploying a new version of your application:
[1] The smart plugs ship with Tasmota pre-flashed. from: https://ounapuu.ee/posts/2024/05/02/smartplugs/ supporting quote: The plugs ship with Tasmota pre-flashed.
[1] The power meter data is collected to an existing Prometheus instance. from: https://ounapuu.ee/posts/2024/05/02/smartplugs/ supporting quote: The power meter data is collected to an existing Prometheus instance on my home server.
[1] The tasmota-power-exporter solution can be used to scrape the plugs' metrics once per second. from: https://ounapuu.ee/posts/2024/05/02/smartplugs/ supporting quote: I run multiple instances of this tasmota-power-exporter solution on my server, one per plug, which get scraped once per second by the Prometheus instance.
[1] Grafana can be used to display graphs for the power meter setup with detailed data points. from: https://ounapuu.ee/posts/2024/05/02/smartplugs/ supporting quote: I already had a Grafana instance running on my home server, so I reused that to show a few basic graphs for the power meter setup.
[4] The TP-LINK HS100 Wi-Fi smart plug uses TCP port 9999 for communication. from: https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/ supporting quote: Now the juicy stuff: the app controls the plug by connecting via TCP to port 9999 of the plug and exchanging a 55 byte payload.
[4] The payload used to control the plug varies only at offset 43 between the on and off commands, and is not encrypted. from: https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/ supporting quote: I was absolutely delighted to find that the payload varies only at offset 43 between the on and off commands; it doesn’t vary over time, contains no check sums and is not encrypted.
[4] A shell script can be used to control the TP-LINK HS100 Wi-Fi smart plug. from: https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/ supporting quote: Here is an outdated version of the script, head over to github for a recent one.
References
- Monitoring energy usage with smart plugs, Prometheus and Grafana — ounapuu.ee (5 May 2024)
- Uptrace v1.6 is available — uptrace.dev (3 January 2024)
- Ask HN: What data visualization tools do you use? — news.ycombinator.com (5 March 2021)
- Controlling the TP-LINK HS100 Wi-Fi smart plug — blog.georgovassilis.com (30 November 2018)