Skip to content

Commit

Permalink
fix shellypro 3em metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Oct 7, 2023
1 parent c199e1a commit 5ae21dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
10 changes: 10 additions & 0 deletions shellyplug/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
powerFactor *prometheus.GaugeVec
powerFrequency *prometheus.GaugeVec
powerVoltage *prometheus.GaugeVec
powerAmpere *prometheus.GaugeVec

sysUnixtime *prometheus.GaugeVec
sysUptime *prometheus.GaugeVec
Expand Down Expand Up @@ -234,6 +235,15 @@ func (sp *ShellyPlug) initMetrics() {
)
sp.registry.MustRegister(sp.prometheus.powerVoltage)

sp.prometheus.powerAmpere = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "shellyplug_power_ampere",
Help: "ShellyPlug configured power ampere",
},
powerLabels,
)
sp.registry.MustRegister(sp.prometheus.powerAmpere)

// ##########################################
// System

Expand Down
24 changes: 9 additions & 15 deletions shellyplug/prober.gen2.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func (sp *ShellyPlug) collectFromTargetGen2(target discovery.DiscoveryTarget, lo
powerUsageLabels := copyLabelMap(targetLabels)
powerUsageLabels["id"] = fmt.Sprintf("switch:%d", configData.Id)
powerUsageLabels["name"] = configData.Name
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.Current)
sp.prometheus.powerTotal.With(powerUsageLabels).Set(result.Apower)
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.Apower)
sp.prometheus.powerVoltage.With(powerUsageLabels).Set(result.Voltage)
sp.prometheus.powerAmpere.With(powerUsageLabels).Set(result.Current)
} else {
logger.Errorf(`failed to decode switchStatus: %v`, err)
}
Expand All @@ -84,41 +85,34 @@ func (sp *ShellyPlug) collectFromTargetGen2(target discovery.DiscoveryTarget, lo
powerUsageLabels := copyLabelMap(targetLabels)
powerUsageLabels["id"] = fmt.Sprintf("em:%d:A", configData.Id)
powerUsageLabels["name"] = configData.Name
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.ACurrent)
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.AActPower)
sp.prometheus.powerApparentCurrent.With(powerUsageLabels).Set(result.AAprtPower)
sp.prometheus.powerTotal.With(powerUsageLabels).Set(result.AActPower)
sp.prometheus.powerFactor.With(powerUsageLabels).Set(result.APf)
sp.prometheus.powerFrequency.With(powerUsageLabels).Set(result.AFreq)
sp.prometheus.powerVoltage.With(powerUsageLabels).Set(result.AVoltage)
sp.prometheus.powerAmpere.With(powerUsageLabels).Set(result.ACurrent)

// phase B
powerUsageLabels = copyLabelMap(targetLabels)
powerUsageLabels["id"] = fmt.Sprintf("em:%d:B", configData.Id)
powerUsageLabels["name"] = configData.Name
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.BCurrent)
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.BActPower)
sp.prometheus.powerApparentCurrent.With(powerUsageLabels).Set(result.BAprtPower)
sp.prometheus.powerTotal.With(powerUsageLabels).Set(result.BActPower)
sp.prometheus.powerFactor.With(powerUsageLabels).Set(result.BPf)
sp.prometheus.powerFrequency.With(powerUsageLabels).Set(result.BFreq)
sp.prometheus.powerVoltage.With(powerUsageLabels).Set(result.BVoltage)
sp.prometheus.powerAmpere.With(powerUsageLabels).Set(result.BCurrent)

// phase C
powerUsageLabels = copyLabelMap(targetLabels)
powerUsageLabels["id"] = fmt.Sprintf("em:%d:C", configData.Id)
powerUsageLabels["name"] = configData.Name
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.CCurrent)
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.CActPower)
sp.prometheus.powerApparentCurrent.With(powerUsageLabels).Set(result.CAprtPower)
sp.prometheus.powerTotal.With(powerUsageLabels).Set(result.CActPower)
sp.prometheus.powerFactor.With(powerUsageLabels).Set(result.CPf)
sp.prometheus.powerFrequency.With(powerUsageLabels).Set(result.CFreq)
sp.prometheus.powerVoltage.With(powerUsageLabels).Set(result.CVoltage)

// phase C
powerUsageLabels = copyLabelMap(targetLabels)
powerUsageLabels["id"] = "em:total"
powerUsageLabels["name"] = configData.Name
sp.prometheus.powerCurrent.With(powerUsageLabels).Set(result.TotalCurrent)
sp.prometheus.powerTotal.With(powerUsageLabels).Set(result.TotalActPower)
sp.prometheus.powerAmpere.With(powerUsageLabels).Set(result.CCurrent)
} else {
logger.Errorf(`failed to decode switchStatus: %v`, err)
}
Expand Down

0 comments on commit 5ae21dc

Please sign in to comment.