From 71fad29cca5370ef22dbee5a583010cecaa935ef Mon Sep 17 00:00:00 2001 From: Gijsbert Date: Fri, 12 Apr 2024 17:05:28 +0200 Subject: [PATCH] Added `charge_discharge_power` register and change power calculation --- control/calculations.go | 11 +++++------ modbus/{consts.go => registers.go} | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) rename modbus/{consts.go => registers.go} (98%) diff --git a/control/calculations.go b/control/calculations.go index 77f3ef4..228b89a 100644 --- a/control/calculations.go +++ b/control/calculations.go @@ -14,18 +14,17 @@ func calculateHomeLoad() (float64, error) { if err != nil { return 0, err } + inverterInputPower = inverterInputPower * 1000 powerMeterActivePower, err := metrics.GetMetricLastEntrySum("power_meter", "active_power") if err != nil { return 0, err } - // Calculate home load when sun is out. - homeLoad = (inverterInputPower * 1000) - powerMeterActivePower - - // If sun is not out, calculate home load based on power meter. - if homeLoad < 0 { - homeLoad = powerMeterActivePower - (inverterInputPower * 1000) + if inverterInputPower > powerMeterActivePower { + homeLoad = inverterInputPower - powerMeterActivePower + } else { + homeLoad = powerMeterActivePower - inverterInputPower } return homeLoad, nil diff --git a/modbus/consts.go b/modbus/registers.go similarity index 98% rename from modbus/consts.go rename to modbus/registers.go index e56e3bd..6cc8c28 100644 --- a/modbus/consts.go +++ b/modbus/registers.go @@ -51,6 +51,7 @@ var ( } luna2000Registers = map[string]Register{ + "charge_discharge_power": Register{Namespace: "luna2000", Name: "charge_discharge_power", Fields: map[string]string{}, Address: 37765, Unit: "W", Gain: 1, Quantity: 2, Type: RegisterTypeInt32, Writeable: false}, "running_status_battery_1": Register{Namespace: "luna2000", Name: "running_status", Fields: map[string]string{"battery": "1"}, Address: 37000, Unit: "", Gain: 1, Quantity: 1, Type: RegisterTypeUint16, Writeable: false}, "charging_status_battery_1": Register{Namespace: "luna2000", Name: "charging_status", Fields: map[string]string{"battery": "1"}, Address: 37001, Unit: "W", Gain: 1, Quantity: 2, Type: RegisterTypeInt32, Writeable: false}, "bus_voltage_battery_1": Register{Namespace: "luna2000", Name: "bus_voltage", Fields: map[string]string{"battery": "1"}, Address: 37003, Unit: "V", Gain: 10, Quantity: 1, Type: RegisterTypeUint16, Writeable: false},