No description
Find a file
2023-07-24 00:56:40 +00:00
.github Bump actions/setup-node from 3.6.0 to 3.7.0 (#307) 2023-07-06 00:51:21 +00:00
.homebridge Fix development environment 2022-11-09 00:41:02 +01:00
code-generation Document configuration default values (#91) 2023-01-03 22:00:31 +01:00
src Bump hap-nodejs from 0.11.0 to 0.11.1 (#239) 2023-05-22 16:03:50 +02:00
tests Terminate output with a linefeed 2023-01-08 17:21:41 +01:00
.envrc Setup direnv 2023-02-18 15:04:40 +01:00
.eslintrc.js Turn off import/no-named-as-default rule 2022-12-13 12:14:56 +01:00
.gitignore Ignore local release script 2022-11-10 13:54:12 +01:00
.npmignore Ignore release script 2022-11-16 23:26:32 +01:00
.release-it.json Run npm test in CI mode for releases 2022-11-10 13:43:23 +01:00
CHANGELOG.md Set up release-it as the release management tool (#18) 2022-11-10 13:40:34 +01:00
config.schema.json Add configuration option for the metric server interface (#25) 2022-11-17 13:15:07 +01:00
flake.lock Initial version 2022-11-07 15:23:48 +01:00
flake.nix Fix flake.nix description 2022-12-14 00:24:14 +01:00
jest.config.js Exclude dist folder from tests search paths 2022-11-24 19:23:40 +01:00
LICENSE Initial commit 2022-11-06 13:04:30 +01:00
nodemon.json Fix development environment 2022-11-09 00:41:02 +01:00
package-lock.json Bump @types/node from 20.4.2 to 20.4.4 (#325) 2023-07-24 00:56:40 +00:00
package.json Bump nodemon from 2.0.22 to 3.0.1 (#311) 2023-07-10 00:33:01 +00:00
prettier.config.js Initial version 2022-11-07 15:23:48 +01:00
README.md Document configuration default values (#91) 2023-01-03 22:00:31 +01:00
sonar-project.properties Exclude a few files from coverage reporting 2022-11-24 23:18:03 +01:00
tsconfig.json Bump typescript from 4.9.5 to 5.0.2 (#188) 2023-03-19 23:54:57 +01:00

Homebridge logo Plus sign Prometheus logo

Homebridge Prometheus Exporter

CI Quality Gate Status npm version Libraries.io dependency status for GitHub repo npm

What if we could store homebridge metrics in Prometheus

homebridge-prometheus-exporter is a plugin for homebridge that provides a metrics endpoint for Prometheus to scrape. Once the metrics are in Prometheus, they can be consumed and presented in various ways. One can use Prometheus Alerting Rules to trigger actions on certain thresholds or Grafana to build informative graphs or alerts.

A Grafana timeseries showing wattage and voltage of two home appliances on a timeseries chart

Installing

Install the plugin

Run this command to install the plugin as a global nodejs module:

npm install -g homebridge-prometheus-exporter

Configure homebridge

Edit the homebridge config.json to load the plugin:

{
    // …
    "platforms": [
    {
      "platform": "PrometheusExporter",
      "pin": "123-12-123",
    },
    // …
  ]
}

Customize homebridge startup

For homebridge-prometheus-exporter to work, homebridge has to run in "insecure mode". This means that any user who has access to your network can control your homebridge devices. This is usually not a big problem but it is still something you should consciously decide. homebridge-config-ui-x requires running in insecure mode if you want to control your devices from homebridge-config-ui-x.

To enable "insecure mode", edit the startup script for homebridge and add --insecure or -I. Assuming you run systemd, the proper way to override the config would be to use systemds drop-in mechanism.

Create /etc/systemd/system/homebridge.service.d folder:

mkdir /etc/systemd/system/homebridge.service.d

Write this drop-in configuration file to /etc/systemd/system/homebridge.service.d/insecure.conf:

[Service]
ExecStart=
ExecStart=/usr/lib/node_modules/homebridge/bin/homebridge --insecure

The first and empty ExecStart tells systemd to forget about the ExecStart from the original service definition and the second ExecStart declares the new file. Run systemctl daemon-reload to refresh systemds unit database and then run systemd-delta --type=extended to check if the drop-in worked as expected.

You should see something like this in the output:

…
[EXTENDED]   /lib/systemd/system/homebridge.service → /etc/systemd/system/homebridge.service.d/insecure.conf
…

If you are not using systemd, first of all, you absolutely should but second of all you will likely have some sort of env file, e.g. /etc/defaults/homebridge to customize the homebridge start command. Restart homebridge using systemctl restart homebridge.

Test that the metrics endpoint is available by accesing http://homebridge-host:36123/metrics. You should see a response similar to this:

# TYPE homebridge_air_purifier_active gauge
homebridge_air_purifier_active{name="…",…} 0 1667914208196
# TYPE homebridge_air_purifier_current_air_purifier_state gauge
homebridge_air_purifier_current_air_purifier_state{name="…",…} 0 1667914208196

Configuring Prometheus

With homebridge-prometheus-exporter up and running, it is now time to configure Prometheus to scrape the config endpoint. Go to your prometheus host and edit /etc/prometheus/prometheus.yml and add the following scrape config:


scrape_configs:
  - job_name: homebridge-exporter
    static_configs:
    - targets:
      - homebridge-host:36123

Once Prometheus is restarted, metrics with the homebridge_ prefix should start to be ingested.

Customize homebridge-prometheus-exporter

homebridge-prometheus-exporter offers a few advanced settings to customize its behavior.

{
  // ...
  "platforms": [
    {
      "platform": "PrometheusExporter",


      // Pin
      //
      // Homebridge PIN for service authentication
      "pin": "<string>",


      // Debug
      //
      // Default: false
      "debug": "<boolean>",


      // Metrics prefix
      //
      // Default: "homebridge"
      "prefix": "<string>",


      // Metrics server port
      //
      // TCP port where the Prometheus metrics server listens
      //
      // Default: 36123
      "port": "<integer>",


      // Metrics server interface
      //
      // Interface where the Prometheus metrics server listens. Can be an IP, a
      // hostname, "0.0.0.0" for all IPv4 interfaces, "::1" for all IPv6 interfaces.
      // Default is "::" which means "any interface"
      //
      // Default: "::"
      "interface": "<string>",


      // Service refresh interval
      //
      // Discover new services every <interval> seconds
      //
      // Default: 60
      "refresh_interval": "<integer>",


      // Request timeout
      //
      // Request timeout when interacting with homebridge instances
      //
      // Default: 10
      "request_timeout": "<integer>",


      // Service discovery timeout
      //
      // Discovery timeout after which the current discovery is considered failed
      //
      // Default: 20
      "discovery_timeout": "<integer>",


      // TLS cert file
      //
      // Path to TLS certificate file (in PEM format)
      "tls_cert_file": "<string>",


      // TLS key file
      //
      // Path to TLS key file
      "tls_key_file": "<string>",


      // Basic auth username/password pairs
      //
      // Usernames and passwords for basic auth. Object key is the username, object
      // value is the password. Password must be encoded with bcrypt. Example:
      // {"joanna": "$2a$12$5/mmmRB28wg9yzaXhee5Iupq3UrFr/qMgAe9LvAxGoY5jLcfVGTUq"}
      "basic_auth": "<object>"
    }
  ]
}