shelly-exporter

A Shelly power metrics exporter written in golang. Currently only tested for Shelly Plug S.

Go

Available metrics

Name Description
shelly_power_current Current real AC power being drawn, in Watts
shelly_power_total Total energy consumed by the attached electrical appliance in Watt-minute
shelly_temperature internal device temperature in °C
shelly_update_available Info whether newer firmware version is available
shelly_uptime Seconds elapsed since boot

All metrics include the following labels:

  • device IP
  • device name
  • device hostname

How to execute for development purposes

Metrics will be exposed on: http://localhost:8080/probe?target=http://<shelly_ip>

Nix / NixOS

This repository contains a flake.nix file.

# run the package
nix run .#shelly_exporter

# build the package
nix build .#shelly_exporter

General

Make sure golang is installed.

# run application
go run .

# build application
go build

# execute tests
go test -v ./...

# show test coverage
go test -covermode=count -coverpkg=./... -coverprofile cover.out -v ./...
go tool cover -html cover.out -o cover.html

How to install

NixOS

  1. Add this repository to your flake.nix:

{
  inputs.shelly-exporter = {
    url = "github:MayNiklas/shelly-exporter";
    inputs = { nixpkgs.follows = "nixpkgs"; };
  };
}
  1. Enable the service in your configuration:

{ shelly-exporter, ... }: {

  imports = [ shelly-exporter.nixosModules.default ];

  services.shelly-exporter = {
    enable = true;
    port = "8080";
    listen = "localhost";
    user = "shelly-exporter";
    group = "shelly-exporter";
  };
}
  1. Scrape exporter with Prometheus:

{ lib, pkgs, config, ... }:
let
  shellyTargets = [
    "http://192.168.0.2"
    "http://192.168.0.3"
    "http://192.168.0.4"
    "http://192.168.0.5"
    "http://192.168.0.6"
    "http://192.168.0.7"
  ];
in {
  services.prometheus = {
    enable = true;
    scrapeConfigs = [{
      job_name = "shelly";
      scrape_interval = "10s";
      metrics_path = "/probe";
      static_configs = [{ targets = shellyTargets; }];
      relabel_configs = [
        {
          source_labels = [ "__address__" ];
          target_label = "__param_target";
        }
        {
          source_labels = [ "__param_target" ];
          target_label = "instance";
        }
        {
          target_label = "__address__";
          replacement = "127.0.0.1:8080";
        }
      ];
    }];
  };
}

Libaries used

Libary documentation

API documentation

GitHub

View Github