Skip to content

Commit

Permalink
✨ Add ampere and volts to power collector (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
daenney authored May 4, 2019
1 parent 596faaf commit a6bd17a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ MQTT topic.
* `contactSensorState`: gauge `sensor_contact_state`
* `currentPower`: gauge `sensor_power_current_watts`
* `energyUsed`: counter `sensor_power_total_kwh`
* `currentVoltage`: gauge `sensor_power_current_voltage`
* `currentAmpere`: gauge `sensor_power_current_ampere`
* `batteryLevel`: gauge `sensor_battery_level_percent`

Additionally a time series is exposed for humiture, also known as
Expand Down
38 changes: 35 additions & 3 deletions collectors/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (

// PowerCollector gets power data from sensors
type PowerCollector struct {
powerCurrent *prometheus.Desc
powerTotal *prometheus.Desc
m *server.Manager
powerCurrent *prometheus.Desc
powerTotal *prometheus.Desc
voltageCurrent *prometheus.Desc
ampereCurrent *prometheus.Desc
m *server.Manager
}

// NewPowerCollector returns a collector fetching power sensor data
Expand All @@ -28,13 +30,25 @@ func NewPowerCollector(m *server.Manager) (prometheus.Collector, error) {
"Total power usage in kWh",
[]string{"source"}, nil,
),
voltageCurrent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "current_voltage"),
"Current power draw in Volts",
[]string{"source"}, nil,
),
ampereCurrent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "current_ampere"),
"Current power draw in Amperes",
[]string{"source"}, nil,
),
}, nil
}

// Describe sends all metrics descriptions into the channel
func (c *PowerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.powerCurrent
ch <- c.powerTotal
ch <- c.voltageCurrent
ch <- c.ampereCurrent
}

// Collect sends metric updates into the channel
Expand All @@ -59,5 +73,23 @@ func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.powerTotal,
prometheus.CounterValue, v, s.Info().Topic)
}
if s.Feature("currentVoltage").Exists() {
v, err := toFloat(s.Feature("currentVoltage").Value())
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.voltageCurrent,
prometheus.GaugeValue, v, s.Info().Topic)
}
if s.Feature("currentAmpere").Exists() {
v, err := toFloat(s.Feature("currentAmpere").Value())
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.ampereCurrent,
prometheus.GaugeValue, v, s.Info().Topic)
}
}
}

0 comments on commit a6bd17a

Please sign in to comment.