Shelly Plug Prometheus exporter.

Overview

Shelly Plug Prometheus Exporter

CI

I am a simple man.

I have a Shelly Plug. I don't want to flash a different firmware on it just to get a Prometheus endpoint. Though I might do it someday if I'm not lazy.

I want a Prometheus endpoint where I can scrape metrics for the Shelly Plug.

So I built this.

Could I use MQTT and use some sort of preconfigured broker / metrics generator? Yeah probably. But it's literally easier (if you don't already use MQTT and know how to set it up) to just write a little metrics scraper like this for a one-off.

How to Use

This thing is a PHP script that runs in a Docker container. That's it.

You could even run the thing without using Docker, it doesn't care, it's just a PHP script.

Set environment variables

I recommend adding a basic HTTP auth username and password to your Shelly Plug to give at least minimal security.

Run the PHP script inside Docker like so:

docker run -d -p 9924:80 --name shelly-plug \
  -e SHELLY_HOSTNAME='my-shelly-plug-host-or-ip' \
  -e SHELLY_HTTP_USERNAME='username' \
  -e SHELLY_HTTP_PASSWORD='password' \
  -v "$PWD":/var/www/html \
  php:8-apache

Or you can set it up inside a docker-compose file like so:

---
version: "3"

services:
  shelly-plug:
    container_name: shelly-plug
    image: php:8-apache
    ports:
      - "9924:80"
    environment:
      SHELLY_HOSTNAME='my-shelly-plug-host-or-ip'
      SHELLY_HTTP_USERNAME='username'
      SHELLY_HTTP_PASSWORD='password'
    volumes:
      - './:/var/www/html'
    restart: unless-stopped

If you run the script outside of Docker, make sure you set the necessary environment variables.

After it's running, test that it's working with curl:

$ curl http://localhost:9924/metrics
# HELP power Current real AC power being drawn, in Watts
# TYPE power gauge
power 91.81
# HELP is_valid Whether power metering self-checks OK
# TYPE is_valid gauge
is_valid 1
# HELP total Total energy consumed by the attached electrical appliance in Watt-minute
# TYPE total gauge
total 24239

If you get an error, check your environment variables and make sure they're correct, and make sure the Docker container (and the host it's on) can access your Shelly Plug over HTTP!

License

MIT.

Author

Jeff Geerling.

Comments
  • multiple endpoints

    multiple endpoints

    Hello simple man! :)

    I like your repo, thanks for sharing! Personally I felt the flashing approach was simpler, and went that way. However with little success, the Shelly would not expose a /metrics endpoint even after I flashed a custom Tasmota binary (I started a discussion here).

    What if I'd like to monitor multiple Shellys with your PHP docker? I find it a bit heavy to deploy a full PHP container for every Shelly I want to monitor... We're talking 5+.

    I think a cool approach would be to identify them by routes rather than ports. Eg:

    ---
    version: "3"
    
    services:
      shelly-plug:
        container_name: shelly-plug
        image: php:8-apache
        ports:
          - "9924:80"
        environment:
          SHELLY_1_HOSTNAME='my-shelly-plug1-host-or-ip'
          SHELLY_1_IDENTIFIER='shelly-kitchen'
          SHELLY_1_HTTP_USERNAME='username'
          SHELLY_1_HTTP_PASSWORD='password'
          SHELLY_2_HOSTNAME='my-shelly-plug2-host-or-ip'
          SHELLY_2_IDENTIFIER='shelly-living-room'
          SHELLY_2_HTTP_USERNAME='username'
          SHELLY_2_HTTP_PASSWORD='password'
          SHELLY_3_HOSTNAME='my-shelly-plug3-host-or-ip'
          SHELLY_3_IDENTIFIER='shelly-dining-room'
          SHELLY_3_HTTP_USERNAME='username'
          SHELLY_3_HTTP_PASSWORD='password'
        volumes:
          - './:/var/www/html'
        restart: unless-stopped
    

    Your code would then check for the defined env variables to know how many there are and expose them via the identifier variable: so to get metrics for the kitchen shelly, you'd go to: http://<dockerhost>:9924/shelly-kitchen/metrics and for the living room metrics: http://<dockerhost>:9924/shelly-living-room/metrics

    What do you think?

    stale 
    opened by shiftux 4
  • proposed evolution to collecte temperature

    proposed evolution to collecte temperature

    Hello,

    The version of metrics php file I'm using.

    I get the temperature and customise the plug name.

    metrics.txt

    And the grafana dashboard

    grafana_dashboard.txt

    stale 
    opened by eravion 2
  • docker-compose.yml issue on environment definition

    docker-compose.yml issue on environment definition

    Hello,

    On docker-compose.yml file this line : environment: SHELLY_HOSTNAME='my-shelly-plug-host-or-ip' SHELLY_HTTP_USERNAME='username' SHELLY_HTTP_PASSWORD='password'

    Should be like that : environment: - SHELLY_HOSTNAME=my-shelly-plug-host-or-ip - SHELLY_HTTP_USERNAME=username - SHELLY_HTTP_PASSWORD=password

    Removing ' and adding -

    stale 
    opened by eravion 2
  • Data not showing in Grafana

    Data not showing in Grafana

    I followed the instruction and I get the data shown when I run the curl command. Also, I was able to add the data source to Grafana but when I try to look up the data I get no metrics found and the query returns no data. Any further instructions on how the data can be used in a Grafana dashboard?

    opened by mdaghrir 1
  • Added support for the second generation - Shelly

    Added support for the second generation - Shelly "plus"

    Since the API for the plus devices is different, I've split up the functionality so that the functionality for polling the first generation is in one file, and the second gen in another.

    The structure of these files is largely the same, and you still access both of them via the "metrics" entry point.

    Key differences:

    • The new generation uses digest authentication rather than basic, and has a hardcoded username
    • The URL has changed, they now use an RPC style API
    • The format of the json you get back is also different (but standardised across all the new generation

    I've also added support for relays with multiple switches (i.e. PM2). I'm not able to test it extensively (yet), as all the relays I have are only of the single variety, but I'm fairly confident it should work, as it was only a minor tweak to the script

    Also of note: The new shellys are also able to report temperature and voltage (and current in amps): https://shelly-api-docs.shelly.cloud/gen2/Components/FunctionalComponents/Switch#status - it might be cool to add them to the graphana/prometheus internet-pi monitoring suite (though I'm not familiar at all with either of them and I'm not sure how the output of this script makes its way into graphana (yet!)

    opened by DanForever 13
Owner
Jeff Geerling
Catholic dad and husband. I write, build, and tinker on a Mac. #stl #drupal #ansible #k8s #raspberrypi #crohns
Jeff Geerling
Simple Magento 2 Prometheus Exporter.

Magento 2 Prometheus Exporter This Magento 2 Module exposes a new route under /metrics with Magento 2 specific metrics in the format of prometheus. Th

run_as_root GmbH 46 Nov 29, 2022
A Pocketmine Plug-in For Executing Commands While You Click on a item

ItemInteract What This Plugin Do? well This Plugin Is Usefull For Some Servers Like minigames,Skyblock,Lobby etc. This plugin can execute commands whi

null 6 May 31, 2022
Plug-and-Play Custom Enchants Plugin For PocketMine-PMP

DEVELOPER'S NOTE: I can tell you right now this plugin hasn't been tested. I haven't even checked to see if it loads without crashing yet. Half of thi

VANIC 10 Oct 23, 2021
Plug-and-Play Custom Enchants Plugin For PocketMine-PMP

DEVELOPER'S NOTE: I can tell you right now this plugin hasn't been tested. I haven't even checked to see if it loads without crashing yet. Half of thi

VANIC 10 Oct 23, 2021
This project aims to facilitate the management of websites monitored by the blackbox exporter, via a web UI.

This project aims to facilitate the management of websites monitored by the blackbox exporter, via a web UI. The UI would allow to add/remove sites, groups, and even add different fields in the prometheus database.

null 2 Nov 6, 2021
AirGradient Prometheus exporter.

AirGradient Prometheus Exporter AirGradient has a DIY air sensor. I built one (actually, more than one). I want to integrate sensor data into my in-ho

Jeff Geerling 103 Dec 24, 2022
Export Laravel Horizon metrics using this Prometheus exporter.

Laravel Horizon Prometheus Exporter Export Laravel Horizon metrics using this Prometheus exporter. This package leverages Exporter Contracts. ?? Suppo

Renoki Co. 22 Sep 18, 2022
Export Laravel Octane metrics using this Prometheus exporter.

Laravel Octane Prometheus Exporter Export Laravel Octane metrics using this Prometheus exporter. ?? Supporting If you are using one or more Renoki Co.

Renoki Co. 19 Dec 26, 2022
Prometheus exporter for Yii2

yii2-prometheus Prometheus Extension for Yii 2 This extension provides a Prometheus exporter component for Yii framework 2.0 applications. This extens

Mehdi Achour 3 Oct 27, 2021
Simple Magento 2 Prometheus Exporter.

Magento 2 Prometheus Exporter This Magento 2 Module exposes a new route under /metrics with Magento 2 specific metrics in the format of prometheus. Th

run_as_root GmbH 46 Nov 29, 2022
Plug n play avatar, turn name, email, and any other string into beautiful avatar (or gravatar), effortless.

laravolt/avatar Display unique avatar for any user based on their (initials) name. Preview Installation This package originally built for Laravel, but

Laravolt 1.7k Jan 3, 2023
Plug and play flat file markdown blog for your Laravel-projects

Ampersand Plug-and-play flat file markdown blog tool for your Laravel-project. Create an article or blog-section on your site without the hassle of se

Marcus Olsson 22 Dec 5, 2022
A Pocketmine Plug-in For Executing Commands While You Click on a item

ItemInteract What This Plugin Do? well This Plugin Is Usefull For Some Servers Like minigames,Skyblock,Lobby etc. This plugin can execute commands whi

null 6 May 31, 2022
The Prometheus monitoring system and time series database.

Prometheus Visit prometheus.io for the full documentation, examples and guides. Prometheus, a Cloud Native Computing Foundation project, is a systems

Prometheus 46.2k Jan 1, 2023
Plug-and-Play Custom Enchants Plugin For PocketMine-PMP

DEVELOPER'S NOTE: I can tell you right now this plugin hasn't been tested. I haven't even checked to see if it loads without crashing yet. Half of thi

VANIC 10 Oct 23, 2021
Plug-and-Play Custom Enchants Plugin For PocketMine-PMP

DEVELOPER'S NOTE: I can tell you right now this plugin hasn't been tested. I haven't even checked to see if it loads without crashing yet. Half of thi

VANIC 10 Oct 23, 2021
This project aims to facilitate the management of websites monitored by the blackbox exporter, via a web UI.

This project aims to facilitate the management of websites monitored by the blackbox exporter, via a web UI. The UI would allow to add/remove sites, groups, and even add different fields in the prometheus database.

null 2 Nov 6, 2021
Plug & Play [CURL + Composer Optional], Proxy as a Service, Multi-tenant, Multi-Threaded, with Cache & Article Spinner

?? .yxorP The SAAS(y), Multitenancy & Augmenting Web Proxy Guzzler is a 100% SAAS(y) plug-and-play (PHP CURL+Composer are Optional) solution that leverages SAAS architecture to provide multi-tenancy, multiple threads, caching, and an article spinner service.

4D/าต.com Dashboards 12 Nov 17, 2022