Skip to content

Commit 4f6b546

Browse files
bonandaenney
authored andcommitted
add new metrics for currentPowerProduced and energyProduced
1 parent a40f46f commit 4f6b546

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ MQTT topic.
3030
* `currentRelativeHumidity`: gauge `sensors_humidity_relative_percent`
3131
* `contactSensorState`: gauge `sensors_contact_state`
3232
* `currentPower`: gauge `sensors_power_current_watts`
33+
* `currentPowerProduced`: gauge `sensors_power_produced_current_watts`
3334
* `energyUsed`: counter `sensors_power_total_kwh`
35+
* `energyProduced`: counter `sensors_power_produced_total_kwh`
3436
* `currentVoltage`: gauge `sensors_power_current_voltage`
3537
* `currentAmpere`: gauge `sensors_power_current_ampere`
3638
* `batteryLevel`: gauge `sensors_battery_level_percent`

collectors/power.go

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package collectors
22

33
import (
4+
"fmt"
45
"log"
6+
"strconv"
57

68
"github.com/prometheus/client_golang/prometheus"
79

@@ -11,11 +13,15 @@ import (
1113

1214
// PowerCollector gets power data from sensors
1315
type PowerCollector struct {
14-
powerCurrent *prometheus.Desc
15-
powerTotal *prometheus.Desc
16-
voltageCurrent *prometheus.Desc
17-
ampereCurrent *prometheus.Desc
18-
m *server.Manager
16+
powerCurrent *prometheus.Desc
17+
powerProducedCurrent *prometheus.Desc
18+
powerTotal *prometheus.Desc
19+
powerProducedTotal *prometheus.Desc
20+
voltageCurrent *prometheus.Desc
21+
voltageCurrentPhase *prometheus.Desc
22+
ampereCurrent *prometheus.Desc
23+
ampereCurrentPhase *prometheus.Desc
24+
m *server.Manager
1925
}
2026

2127
// NewPowerCollector returns a collector fetching power sensor data
@@ -27,28 +33,50 @@ func NewPowerCollector(m *server.Manager) (prometheus.Collector, error) {
2733
"Current power draw in Watts",
2834
[]string{"source"}, nil,
2935
),
36+
powerProducedCurrent: prometheus.NewDesc(
37+
prometheus.BuildFQName(namespace, "power", "produced_current_watts"),
38+
"Current power production in Watts",
39+
[]string{"source"}, nil,
40+
),
3041
powerTotal: prometheus.NewDesc(
3142
prometheus.BuildFQName(namespace, "power", "total_kwh"),
3243
"Total power usage in kWh",
3344
[]string{"source"}, nil,
3445
),
46+
powerProducedTotal: prometheus.NewDesc(
47+
prometheus.BuildFQName(namespace, "power", "produced_total_kwh"),
48+
"Total power production in kWh",
49+
[]string{"source"}, nil,
50+
),
3551
voltageCurrent: prometheus.NewDesc(
3652
prometheus.BuildFQName(namespace, "power", "current_voltage"),
37-
"Current power draw in Volts",
53+
"Current voltage",
3854
[]string{"source"}, nil,
3955
),
4056
ampereCurrent: prometheus.NewDesc(
4157
prometheus.BuildFQName(namespace, "power", "current_ampere"),
4258
"Current power draw in Amperes",
4359
[]string{"source"}, nil,
4460
),
61+
voltageCurrentPhase: prometheus.NewDesc(
62+
prometheus.BuildFQName(namespace, "power", "current_voltage"),
63+
"Current voltage",
64+
[]string{"source", "phase"}, nil,
65+
),
66+
ampereCurrentPhase: prometheus.NewDesc(
67+
prometheus.BuildFQName(namespace, "power", "current_ampere"),
68+
"Current power draw in Amperes",
69+
[]string{"source", "phase"}, nil,
70+
),
4571
}, nil
4672
}
4773

4874
// Describe sends all metrics descriptions into the channel
4975
func (c *PowerCollector) Describe(ch chan<- *prometheus.Desc) {
5076
ch <- c.powerCurrent
77+
ch <- c.powerProducedCurrent
5178
ch <- c.powerTotal
79+
ch <- c.powerProducedTotal
5280
ch <- c.voltageCurrent
5381
ch <- c.ampereCurrent
5482
}
@@ -70,6 +98,19 @@ func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) {
7098
ch <- prometheus.MustNewConstMetric(c.powerCurrent,
7199
prometheus.GaugeValue, vf, s.Info().Topic)
72100
}
101+
if ft := s.Feature("currentPowerProduced"); ft.Exists() {
102+
v := ft.Value()
103+
if v == "" {
104+
continue
105+
}
106+
vf, err := toFloat(v)
107+
if err != nil {
108+
log.Print(err.Error())
109+
continue
110+
}
111+
ch <- prometheus.MustNewConstMetric(c.powerProducedCurrent,
112+
prometheus.GaugeValue, vf, s.Info().Topic)
113+
}
73114
if ft := s.Feature(feature.EnergyUsed.String()); ft.Exists() {
74115
v := ft.Value()
75116
if v == "" {
@@ -83,6 +124,19 @@ func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) {
83124
ch <- prometheus.MustNewConstMetric(c.powerTotal,
84125
prometheus.CounterValue, vf, s.Info().Topic)
85126
}
127+
if ft := s.Feature("energyProduced"); ft.Exists() {
128+
v := ft.Value()
129+
if v == "" {
130+
continue
131+
}
132+
vf, err := toFloat(v)
133+
if err != nil {
134+
log.Print(err.Error())
135+
continue
136+
}
137+
ch <- prometheus.MustNewConstMetric(c.powerProducedTotal,
138+
prometheus.CounterValue, vf, s.Info().Topic)
139+
}
86140
if ft := s.Feature(feature.CurrentVoltage.String()); ft.Exists() {
87141
v := ft.Value()
88142
if v == "" {
@@ -109,5 +163,33 @@ func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) {
109163
ch <- prometheus.MustNewConstMetric(c.ampereCurrent,
110164
prometheus.GaugeValue, vf, s.Info().Topic)
111165
}
166+
for _, phase := range []int{1, 2, 3} {
167+
if ft := s.Feature(fmt.Sprintf("phase%dVoltage", phase)); ft.Exists() {
168+
v := ft.Value()
169+
if v == "" {
170+
continue
171+
}
172+
vf, err := toFloat(v)
173+
if err != nil {
174+
log.Print(err.Error())
175+
continue
176+
}
177+
ch <- prometheus.MustNewConstMetric(c.voltageCurrentPhase,
178+
prometheus.GaugeValue, vf, s.Info().Topic, strconv.Itoa(phase))
179+
}
180+
if ft := s.Feature(fmt.Sprintf("phase%dCurrent", phase)); ft.Exists() {
181+
v := ft.Value()
182+
if v == "" {
183+
continue
184+
}
185+
vf, err := toFloat(v)
186+
if err != nil {
187+
log.Print(err.Error())
188+
continue
189+
}
190+
ch <- prometheus.MustNewConstMetric(c.ampereCurrentPhase,
191+
prometheus.GaugeValue, vf, s.Info().Topic, strconv.Itoa(phase))
192+
}
193+
}
112194
}
113195
}

0 commit comments

Comments
 (0)