From 2e818fc32701b841a0a5a571bc0628318eae9279 Mon Sep 17 00:00:00 2001 From: Richard Mitchell <richard.j.mitchell@gmail.com> Date: Sun, 5 Aug 2018 14:21:40 +0100 Subject: [PATCH] Add device_id field to sensors --- README.md | 2 +- sensors.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f2e5c7b..f06cbe6 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Each group metric is labelled with the name, and the type. ## Sensor metrics -Each sensor metric is labelled with the friendly name, the model, the type, the product name, the manufacturer name, and the unique ID. +Each sensor metric is labelled with the friendly name, the model, the type, the product name, the manufacturer name, the unique ID and the device ID (for motion sensor components). The device ID is a truncated version of the unique ID, that may be used to group the individual sensors that make up a single physical device. * `hue_sensor_value`: value varies depending on the `type` of the sensor. For switches, it's the value of the last button pressed; for daylight and presence sensors it's a `0` or `1` representing false or true values; for the temperature sensor it's hundredths of a degree celsius; for the light level sensor it's Lux. * `hue_sensor_battery`: battery level percentage (0 for sensors that have no battery) diff --git a/sensors.go b/sensors.go index 5699a9e..c815324 100644 --- a/sensors.go +++ b/sensors.go @@ -24,6 +24,7 @@ var variableSensorLabelNames = []string{ "manufacturer_name", "product_name", "unique_id", + "device_id", } func contains(a []string, x string) bool { @@ -122,6 +123,7 @@ func (c sensorCollector) Collect(ch chan<- prometheus.Metric) { for _, sensor := range sensors { var sensorValue float64 + deviceID := sensor.UniqueID if contains(c.ignoreTypes, sensor.Type) { continue } else if sensor.Type == "Daylight" { @@ -131,19 +133,24 @@ func (c sensorCollector) Collect(ch chan<- prometheus.Metric) { } } else if sensor.Type == "ZGPSwitch" { // Hue tap switch + deviceID = sensor.UniqueID[0:23] sensorValue = float64(sensor.State.ButtonEvent) } else if sensor.Type == "ZLLSwitch" { // Hue dimmer switch + deviceID = sensor.UniqueID[0:23] sensorValue = float64(sensor.State.ButtonEvent) } else if sensor.Type == "ClipGenericStatus" { sensorValue = float64(sensor.State.Status) } else if sensor.Type == "ZLLTemperature" { + deviceID = sensor.UniqueID[0:23] sensorValue = float64(sensor.State.Temperature) } else if sensor.Type == "ZLLPresence" { + deviceID = sensor.UniqueID[0:23] if sensor.State.Presence { sensorValue = 1 } } else if sensor.Type == "ZLLLightLevel" { + deviceID = sensor.UniqueID[0:23] sensorValue = float64(sensor.State.LightLevel) } @@ -153,6 +160,7 @@ func (c sensorCollector) Collect(ch chan<- prometheus.Metric) { "manufacturer_name": sensor.ManufacturerName, "type": sensor.Type, "unique_id": sensor.UniqueID, + "device_id": deviceID, "product_name": sensor.ProductName, } -- GitLab