-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scraping values from Fronius Smart Meter API #101
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ var ( | |
sitePowerPhotovoltaicsGauge = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Name: "site_power_photovoltaic", | ||
Help: "Site power supplied to or provided from the accumulator(s) in Watt", | ||
Help: "Site power from photovoltaic in Watt", | ||
}) | ||
|
||
siteAutonomyRatioGauge = promauto.NewGauge(prometheus.GaugeOpts{ | ||
|
@@ -84,6 +84,16 @@ var ( | |
Name: "site_mppt_current_dc", | ||
Help: "Site mppt current DC in A", | ||
}, []string{"inverter", "mppt"}) | ||
meterEnergyRealSumConsumedVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Name: "meter_energy_consumed_wh_total", | ||
Help: "Meter consumed energy from grid in Wh", | ||
}, []string{"device"}) | ||
meterEnergyRealSumProducedVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Name: "meter_energy_produced_wh_total", | ||
Help: "Meter produced energy to grid in Wh", | ||
}, []string{"device"}) | ||
) | ||
|
||
func collectMetricsFromTarget(client *fronius.SymoClient) { | ||
|
@@ -93,13 +103,15 @@ func collectMetricsFromTarget(client *fronius.SymoClient) { | |
"timeout": client.Options.Timeout, | ||
"powerFlowEnabled": client.Options.PowerFlowEnabled, | ||
"archiveEnabled": client.Options.ArchiveEnabled, | ||
"meterEnabled": client.Options.SmartMeterEnabled, | ||
}).Debug("Requesting data.") | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(2) | ||
wg.Add(3) | ||
|
||
collectPowerFlowData(client, &wg) | ||
collectArchiveData(client, &wg) | ||
collectSmartMeterData(client, &wg) | ||
|
||
wg.Wait() | ||
elapsed := time.Since(start) | ||
|
@@ -132,6 +144,19 @@ func collectArchiveData(client *fronius.SymoClient, w *sync.WaitGroup) { | |
} | ||
} | ||
|
||
func collectSmartMeterData(client *fronius.SymoClient, w *sync.WaitGroup) { | ||
defer w.Done() | ||
if client.Options.SmartMeterEnabled { | ||
meterData, err := client.GetMeterData() | ||
if err != nil { | ||
log.WithError(err).Warn("Could not collect Symo SmartMeter metrics.") | ||
scrapeErrorCount.Add(1) | ||
return | ||
} | ||
parseSmartMeterMetrics(meterData) | ||
} | ||
} | ||
|
||
func parsePowerFlowMetrics(data *fronius.SymoData) { | ||
log.WithField("powerFlowData", *data).Debug("Parsing data.") | ||
for key, inverter := range data.Inverters { | ||
|
@@ -165,3 +190,11 @@ func parseArchiveMetrics(data map[string]fronius.InverterArchive) { | |
siteMPPTVoltageGaugeVec.WithLabelValues(key, "2").Set(inverter.Data.VoltageDCString2.Values["0"]) | ||
} | ||
} | ||
|
||
func parseSmartMeterMetrics(data map[string]fronius.MeterData) { | ||
log.WithField("meterData", data).Debug("Parsing data.") | ||
for key, meter := range data { | ||
meterEnergyRealSumConsumedVec.WithLabelValues(key).Set(meter.EnergyRealSumConsumed + config.Symo.OffsetConsumed) | ||
meterEnergyRealSumProducedVec.WithLabelValues(key).Set(meter.EnergyRealSumProduced + config.Symo.OffsetProduced) | ||
Comment on lines
+197
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know the smart meter data... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above |
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,10 +13,12 @@ const ( | |||||||||||||
PowerDataPath = "/solar_api/v1/GetPowerFlowRealtimeData.fcgi" | ||||||||||||||
// ArchiveDataPath is the Fronius API URL-path for archive data | ||||||||||||||
ArchiveDataPath = "/solar_api/v1/GetArchiveData.cgi?Scope=System&Channel=Voltage_DC_String_1&Channel=Current_DC_String_1&Channel=Voltage_DC_String_2&Channel=Current_DC_String_2&HumanReadable=false" | ||||||||||||||
// MeterDataPath is the Fronius API URL-path for Smart Meter real time data | ||||||||||||||
MeterDataPath = "/solar_api/v1/GetMeterRealtimeData.cgi" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
type ( | ||||||||||||||
symoPowerFlow struct { | ||||||||||||||
SymoPowerFlow struct { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why expose this type? It's not meant to be public (i.e. usable by other Go packages), but is merely a struct to parse the JSON There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
Body struct { | ||||||||||||||
Data SymoData | ||||||||||||||
} | ||||||||||||||
|
@@ -64,7 +66,7 @@ type ( | |||||||||||||
} | ||||||||||||||
|
||||||||||||||
// SymoArchive holds the parsed archive data from Symo API | ||||||||||||||
symoArchive struct { | ||||||||||||||
SymoArchive struct { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
Body struct { | ||||||||||||||
Data map[string]InverterArchive | ||||||||||||||
} | ||||||||||||||
|
@@ -86,18 +88,34 @@ type ( | |||||||||||||
Values map[string]float64 | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
symoMeter struct { | ||||||||||||||
Body struct { | ||||||||||||||
Data map[string]MeterData | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
MeterData struct { | ||||||||||||||
// "EnergyReal_WAC_Sum_Consumed": 560839.0, | ||||||||||||||
// "EnergyReal_WAC_Sum_Produced": 94087.0, | ||||||||||||||
EnergyRealSumConsumed float64 `json:"EnergyReal_WAC_Sum_Consumed"` | ||||||||||||||
EnergyRealSumProduced float64 `json:"EnergyReal_WAC_Sum_Produced"` | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// SymoClient is a wrapper for making API requests against a Fronius Symo device. | ||||||||||||||
SymoClient struct { | ||||||||||||||
request *http.Request | ||||||||||||||
Options ClientOptions | ||||||||||||||
} | ||||||||||||||
// ClientOptions holds some parameters for the SymoClient. | ||||||||||||||
ClientOptions struct { | ||||||||||||||
URL string | ||||||||||||||
Headers http.Header | ||||||||||||||
Timeout time.Duration | ||||||||||||||
PowerFlowEnabled bool | ||||||||||||||
ArchiveEnabled bool | ||||||||||||||
URL string | ||||||||||||||
Headers http.Header | ||||||||||||||
Timeout time.Duration | ||||||||||||||
PowerFlowEnabled bool | ||||||||||||||
ArchiveEnabled bool | ||||||||||||||
SmartMeterEnabled bool | ||||||||||||||
OffsetConsumed float64 | ||||||||||||||
OffsetProduced float64 | ||||||||||||||
Comment on lines
+117
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From this naming alone it's not clear to me what it means or when it's relevant. Please either try to find a better name, or add field comments describing its effects.
Comment on lines
+117
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
|
@@ -126,7 +144,7 @@ func (c *SymoClient) GetPowerFlowData() (*SymoData, error) { | |||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
defer response.Body.Close() | ||||||||||||||
p := symoPowerFlow{} | ||||||||||||||
p := SymoPowerFlow{} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
err = json.NewDecoder(response.Body).Decode(&p) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
|
@@ -159,7 +177,33 @@ func (c *SymoClient) GetArchiveData() (map[string]InverterArchive, error) { | |||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
defer response.Body.Close() | ||||||||||||||
p := symoArchive{} | ||||||||||||||
p := SymoArchive{} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
err = json.NewDecoder(response.Body).Decode(&p) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
return p.Body.Data, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func (c *SymoClient) GetMeterData() (map[string]MeterData, error) { | ||||||||||||||
u, err := url.Parse(c.Options.URL + MeterDataPath) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
c.request.URL = u | ||||||||||||||
client := http.DefaultClient | ||||||||||||||
client.Timeout = c.Options.Timeout | ||||||||||||||
response, err := client.Do(c.request) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
defer response.Body.Close() | ||||||||||||||
p := SymoMeter{} | ||||||||||||||
err = json.NewDecoder(response.Body).Decode(&p) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,23 @@ func Test_Symo_GetArchiveData_GivenUrl_WhenRequestData_ThenParseStruct(t *testin | |
assert.Equal(t, float64(425.6), p["inverter/1"].Data.VoltageDCString1.Values["0"]) | ||
assert.Equal(t, float64(408.90000000000003), p["inverter/1"].Data.VoltageDCString2.Values["0"]) | ||
} | ||
|
||
func Test_Symo_GetMeterData_GivenUrl_WhenRequestData_ThenParseStruct(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
payload, err := os.ReadFile("testdata/test_meter.json") | ||
require.NoError(t, err) | ||
_, _ = rw.Write(payload) | ||
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for providing a test + testfile 👍 |
||
})) | ||
|
||
c, err := NewSymoClient(ClientOptions{ | ||
URL: server.URL, | ||
PowerFlowEnabled: true, | ||
ArchiveEnabled: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
p, err := c.GetMeterData() | ||
assert.NoError(t, err) | ||
assert.Equal(t, float64(560839), p["0"].EnergyRealSumConsumed) | ||
assert.Equal(t, float64(94087), p["0"].EnergyRealSumProduced) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"Body": { | ||
"Data": { | ||
"0": { | ||
"Current_AC_Phase_1": 0.52100000000000002, | ||
"Current_AC_Phase_2": 0.52300000000000002, | ||
"Current_AC_Phase_3": -0.56999999999999995, | ||
"Current_AC_Sum": 0.47400000000000009, | ||
"Details": { | ||
"Manufacturer": "Fronius", | ||
"Model": "Smart Meter TS 65A-3", | ||
"Serial": "08154711" | ||
}, | ||
"Enable": 1, | ||
"EnergyReactive_VArAC_Sum_Consumed": 1924.0, | ||
"EnergyReactive_VArAC_Sum_Produced": 399648.0, | ||
"EnergyReal_WAC_Minus_Absolute": 94087.0, | ||
"EnergyReal_WAC_Plus_Absolute": 560839.0, | ||
"EnergyReal_WAC_Sum_Consumed": 560839.0, | ||
"EnergyReal_WAC_Sum_Produced": 94087.0, | ||
"Frequency_Phase_Average": 50.0, | ||
"Meter_Location_Current": 0.0, | ||
"PowerApparent_S_Phase_1": 109.3, | ||
"PowerApparent_S_Phase_2": 88.0, | ||
"PowerApparent_S_Phase_3": 126.8, | ||
"PowerApparent_S_Sum": 324.10000000000002, | ||
"PowerFactor_Phase_1": 0.12, | ||
"PowerFactor_Phase_2": 0.86499999999999999, | ||
"PowerFactor_Phase_3": -0.98299999999999998, | ||
"PowerFactor_Sum": -0.19800000000000001, | ||
"PowerReactive_Q_Phase_1": -108.5, | ||
"PowerReactive_Q_Phase_2": -44.200000000000003, | ||
"PowerReactive_Q_Phase_3": -23.100000000000001, | ||
"PowerReactive_Q_Sum": -175.80000000000001, | ||
"PowerReal_P_Phase_1": 13.1, | ||
"PowerReal_P_Phase_2": 76.0, | ||
"PowerReal_P_Phase_3": -124.7, | ||
"PowerReal_P_Sum": -35.5, | ||
"TimeStamp": 1677777249, | ||
"Visible": 1, | ||
"Voltage_AC_PhaseToPhase_12": 398.69999999999999, | ||
"Voltage_AC_PhaseToPhase_23": 401.39999999999998, | ||
"Voltage_AC_PhaseToPhase_31": 398.60000000000002, | ||
"Voltage_AC_Phase_1": 228.59999999999999, | ||
"Voltage_AC_Phase_2": 231.80000000000001, | ||
"Voltage_AC_Phase_3": 231.69999999999999 | ||
} | ||
} | ||
}, | ||
"Head": { | ||
"RequestArguments": { | ||
"Scope": "System" | ||
}, | ||
"Status": { | ||
"Code": 0, | ||
"Reason": "", | ||
"UserMessage": "" | ||
}, | ||
"Timestamp": "2023-03-02T17:14:10+00:00" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe the flags better. What does "offset for consumed/produced" mean? What unit (if there is)?
If the values are only relevant for the smart meter endpoint, maybe make it
symo.smartmeter.offset-{consumed/produced}
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The offset is meant to make it possible to keep the metric in sync with the official electric meter. The unit is Wh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks. I'd propose to update the desc accordingly (better description welcome).