Skip to content

Commit

Permalink
Use watts needed
Browse files Browse the repository at this point in the history
  • Loading branch information
GJSBRT committed Mar 17, 2024
1 parent e95e41f commit c7b4bc4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
23 changes: 14 additions & 9 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ func (c *Control) Start() {
for _, battery := range batteries {
if battery.capacity < 100 {
c.logger.WithFields(logrus.Fields{"inverter": battery.inverter, "battery": battery.battery, "capacity": battery.capacity, "watts": batteryChargeWatts}).Info("Battery is not fully charged, starting charge")
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_CHARGE, batteryChargeWatts)
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_CHARGE, batteryChargeWatts)
if err != nil {
c.errChannel <- err
continue
}
} else {
c.logger.WithFields(logrus.Fields{"inverter": battery.inverter, "battery": battery.battery, "watts": batteryChargeWatts}).Info("Battery is fully charged, stopping charge")
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_STOP, batteryChargeWatts)
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_STOP, batteryChargeWatts)
if err != nil {
c.errChannel <- err
continue
Expand All @@ -163,31 +163,36 @@ func (c *Control) Start() {

// 5. if solar production < home energy consumption && battery capacity > 5%, discharge battery
wattsRequired := math.Ceil(avgHomeLoad - avgSolarIn)
batteriesRequired := math.Ceil(wattsRequired / 5000)
if avgSolarIn < avgHomeLoad {
if batteriesRequired > 0 {
if wattsRequired > 0 {
metrics.SetMetricValue("control", "action", map[string]string{"action": "discharge_battery"}, 1)
} else {
metrics.SetMetricValue("control", "action", map[string]string{"action": "discharge_battery"}, 0)
}

for _, battery := range batteries {
if batteriesRequired > 0 {
if wattsRequired > 0 {
if battery.capacity > 5 {
batteriesRequired--
wattsRequired -= 5000
var useWatts uint
if wattsRequired > 5000 {
useWatts = 5000
wattsRequired -= 5000
} else {
useWatts = uint(wattsRequired)
wattsRequired = 0
}

c.logger.WithFields(logrus.Fields{"inverter": battery.inverter, "battery": battery.battery, "capacity": battery.capacity}).Info("Discharging battery")

err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE, 5000)
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE, useWatts)
if err != nil {
c.errChannel <- err
}
}
} else {
c.logger.WithFields(logrus.Fields{"inverter": battery.inverter, "battery": battery.battery}).Info("Battery is not required, stopping discharge")

err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_STOP, 5000)
err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_STOP, 0)
if err != nil {
c.errChannel <- err
}
Expand Down
6 changes: 3 additions & 3 deletions modbus/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const (
)

const (
MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_STOP uint16 = iota
MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_CHARGE
MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE
MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_STOP uint16 = iota
MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_CHARGE
MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE
)

var (
Expand Down
4 changes: 2 additions & 2 deletions modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ func (m *Modbus) ChangeBatteryForceCharge(inverter string, battery string, state
}

switch state {
case MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_CHARGE:
case MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_CHARGE:
err = connection.client.WriteUint32(luna2000Registers["forcible_charge_power_battery_1"].Address, uint32(watts))
if err != nil {
return err
}
case MODBUS_STATE_BATTERY_1_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE:
case MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE:
err = connection.client.WriteUint32(luna2000Registers["forcible_discharge_power_battery_1"].Address, uint32(watts))
if err != nil {
return err
Expand Down

0 comments on commit c7b4bc4

Please sign in to comment.