Skip to content

Commit

Permalink
Add metric for humidity sensors
Browse files Browse the repository at this point in the history
This adds support for humidity sensors (as available in Fritz DECT 440
devices), exporting a new gauge metric: fritzbox_humidity_percent

Fixes #9
  • Loading branch information
jayme-github committed Feb 9, 2022
1 parent 96dfc65 commit fd94c43
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
16 changes: 15 additions & 1 deletion collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

var (
genericLabels = []string{"device_id", "device_type", "device_name"}
ErrParsingSwitchString = errors.New("Error parsing switch string")
ErrParsingSwitchString = errors.New("error parsing switch string")
)

type fritzCollector struct {
Info *prometheus.Desc
Present *prometheus.Desc
Temperature *prometheus.Desc
TemperatureOffset *prometheus.Desc
Humidity *prometheus.Desc
EnergyWh *prometheus.Desc
PowerW *prometheus.Desc
SwitchState *prometheus.Desc
Expand All @@ -40,6 +41,7 @@ func (fc *fritzCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- fc.Present
ch <- fc.Temperature
ch <- fc.TemperatureOffset
ch <- fc.Humidity
ch <- fc.EnergyWh
ch <- fc.PowerW
ch <- fc.SwitchState
Expand Down Expand Up @@ -81,6 +83,7 @@ func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.NewInvalidMetric(fc.Present, err)
ch <- prometheus.NewInvalidMetric(fc.Temperature, err)
ch <- prometheus.NewInvalidMetric(fc.TemperatureOffset, err)
ch <- prometheus.NewInvalidMetric(fc.Humidity, err)
ch <- prometheus.NewInvalidMetric(fc.EnergyWh, err)
ch <- prometheus.NewInvalidMetric(fc.PowerW, err)
ch <- prometheus.NewInvalidMetric(fc.SwitchState, err)
Expand Down Expand Up @@ -139,6 +142,11 @@ func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
log.Printf("Unable to parse power data of \"%s\" : %v\n", dev.Name, err)
}
}
if dev.Present == 1 && dev.CanMeasureHumidity() {
if err := mustStringToFloatMetric(ch, fc.Humidity, dev.Humidity.FmtRelativeHumidity(), &dev); err != nil {
log.Printf("Unable to parse humidity data of \"%s\" : %v\n", dev.Name, err)
}
}

if dev.IsThermostat() {
// Battery charge level is optional
Expand Down Expand Up @@ -221,6 +229,12 @@ func NewFritzCollector() *fritzCollector {
genericLabels,
prometheus.Labels{},
),
Humidity: prometheus.NewDesc(
"fritzbox_humidity_percent",
"Relative humidity measured as full percentile",
genericLabels,
prometheus.Labels{},
),
EnergyWh: prometheus.NewDesc(
"fritzbox_energy",
"Absolute energy consumption (in Wh) since the device started operating",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ require (
google.golang.org/protobuf v1.21.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/bpicode/fritzctl => github.com/jayme-github/fritzctl v1.4.23-0.20220206160018-2185c66d7f6f
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bpicode/fritzctl v1.4.24-0.20210413183853-48944781b59f h1:mkODdHPkcB63TENQYUJOiNRF6t6hIwYZk0MMhmaVM48=
github.com/bpicode/fritzctl v1.4.24-0.20210413183853-48944781b59f/go.mod h1:8bJv/qlxE0sdIAwjSLyuc+NeQ4kz77bMCdCCZOKJQjc=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
Expand Down Expand Up @@ -64,6 +62,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jayme-github/fritzctl v1.4.23-0.20220206160018-2185c66d7f6f h1:0Hw74rytK5dUZJKHw5hiQX5V8NSl+uXZOTsIqO/joL8=
github.com/jayme-github/fritzctl v1.4.23-0.20220206160018-2185c66d7f6f/go.mod h1:8bJv/qlxE0sdIAwjSLyuc+NeQ4kz77bMCdCCZOKJQjc=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
7 changes: 7 additions & 0 deletions test/devicelist_729.metrics
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fritzbox_device_info{device_id="12345 0000003",device_name="HKR_2",device_type="
fritzbox_device_info{device_id="12345 0000004",device_name="HKR_3",device_type="Comet DECT",functionbitmask="320",fw_version="03.68",internal_id="23",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT",functionbitmask="320",fw_version="03.68",internal_id="24",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210",functionbitmask="35712",fw_version="04.17",internal_id="25",manufacturer="AVM"} 1
fritzbox_device_info{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440",functionbitmask="1048864",fw_version="05.21",internal_id="26",manufacturer="AVM"} 1
# HELP fritzbox_device_present Device connected (1) or not (0)
# TYPE fritzbox_device_present gauge
fritzbox_device_present{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 1
Expand All @@ -14,10 +15,14 @@ fritzbox_device_present{device_id="12345 0000003",device_name="HKR_2",device_typ
fritzbox_device_present{device_id="12345 0000004",device_name="HKR_3",device_type="Comet DECT"} 1
fritzbox_device_present{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT"} 1
fritzbox_device_present{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210"} 1
fritzbox_device_present{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440"} 1
# HELP fritzbox_energy Absolute energy consumption (in Wh) since the device started operating
# TYPE fritzbox_energy gauge
fritzbox_energy{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 339584
fritzbox_energy{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210"} 40525
# HELP fritzbox_humidity_percent Relative humidity measured as full percentile
# TYPE fritzbox_humidity_percent gauge
fritzbox_humidity_percent{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440"} 54
# HELP fritzbox_power Current power (in W), refreshed approx every 2 minutes
# TYPE fritzbox_power gauge
fritzbox_power{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 0
Expand Down Expand Up @@ -46,6 +51,7 @@ fritzbox_temperature{device_id="12345 0000003",device_name="HKR_2",device_type="
fritzbox_temperature{device_id="12345 0000004",device_name="HKR_3",device_type="Comet DECT"} 16.5
fritzbox_temperature{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT"} 17.5
fritzbox_temperature{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210"} 9.5
fritzbox_temperature{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440"} 17.5
# HELP fritzbox_temperature_offset Temperature offset (set by the user) in units of 0.1 °C
# TYPE fritzbox_temperature_offset gauge
fritzbox_temperature_offset{device_id="12345 0000001",device_name="SW_1",device_type="FRITZ!DECT 200"} 0
Expand All @@ -54,6 +60,7 @@ fritzbox_temperature_offset{device_id="12345 0000003",device_name="HKR_2",device
fritzbox_temperature_offset{device_id="12345 0000004",device_name="HKR_3",device_type="Comet DECT"} -2
fritzbox_temperature_offset{device_id="12345 0000005",device_name="HKR_4",device_type="Comet DECT"} -1
fritzbox_temperature_offset{device_id="12345 0000006",device_name="SW_2",device_type="FRITZ!DECT 210"} 0
fritzbox_temperature_offset{device_id="12345 0000007",device_name="BUTTON_1",device_type="FRITZ!DECT 440"} 0
# HELP fritzbox_thermostat_battery_charge_level Battery charge level in percent
# TYPE fritzbox_thermostat_battery_charge_level gauge
fritzbox_thermostat_battery_charge_level{device_id="12345 0000002",device_name="HKR_1",device_type="Comet DECT"} 70
Expand Down
30 changes: 30 additions & 0 deletions test/devicelist_729.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,34 @@
<offset>0</offset>
</temperature>
</device>
<device identifier="12345 0000007" id="26" functionbitmask="1048864" fwversion="05.21" manufacturer="AVM" productname="FRITZ!DECT 440">
<present>1</present>
<txbusy>0</txbusy>
<name>BUTTON_1</name>
<battery>100</battery>
<batterylow>0</batterylow>
<temperature>
<celsius>175</celsius>
<offset>0</offset>
</temperature>
<humidity>
<rel_humidity>54</rel_humidity>
</humidity>
<button identifier="12345 0000006-1" id="5008">
<name>Taster Bad: Oben rechts</name>
<lastpressedtimestamp>1643533662</lastpressedtimestamp>
</button>
<button identifier="12345 0000006-3" id="5009">
<name>Taster Bad: Unten rechts</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
<button identifier="12345 0000006-5" id="5010">
<name>Taster Bad: Unten links</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
<button identifier="12345 0000006-7" id="5011">
<name>Taster Bad: Oben links</name>
<lastpressedtimestamp></lastpressedtimestamp>
</button>
</device>
</devicelist>

0 comments on commit fd94c43

Please sign in to comment.