Skip to content

Commit

Permalink
Discharge batteries simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
GJSBRT committed Apr 6, 2024
1 parent ae783f2 commit 89ef984
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,38 +172,43 @@ func (c *Control) Start() {
metrics.SetMetricValue("control", "action", map[string]string{"action": "discharge_battery"}, 0)
}

if wattsRequired > 0 {
c.logger.WithFields(logrus.Fields{"wattsRequired": wattsRequired}).Info("Discharging battery")
}

maxBatteryDischargeWatts := uint(len(batteries)) * 5000

var wattsFromGrid uint
if wattsRequired > maxBatteryDischargeWatts {

Check failure on line 182 in control/control.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: wattsRequired > maxBatteryDischargeWatts (mismatched types float64 and uint)
wattsFromGrid = uint(wattsRequired) - maxBatteryDischargeWatts
wattsRequired = float64(maxBatteryDischargeWatts)
}

wattsRequiredPerBattery := wattsRequired / float64(len(batteries))

for _, battery := range batteries {
if wattsRequired > 0 {
if battery.capacity > float64(c.config.MinimumBatteryCapacity) {
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, "watts": useWatts}).Info("Discharging battery")

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")
if battery.capacity < float64(c.config.MinimumBatteryCapacity) {
c.logger.WithFields(logrus.Fields{"inverter": battery.inverter, "battery": battery.battery, "capacity": battery.capacity}).Info("Battery capacity is too low, skipping discharge and setting battery to stop")

err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_STOP, 0)
if err != nil {
c.errChannel <- err
}

continue
}

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

err := c.modbus.ChangeBatteryForceCharge(battery.inverter, battery.battery, modbus.MODBUS_STATE_BATTERY_FORCIBLE_CHARGE_DISCHARGE_DISCHARGE, wattsRequiredPerBattery)

Check failure on line 203 in control/control.go

View workflow job for this annotation

GitHub Actions / build

cannot use wattsRequiredPerBattery (variable of type float64) as uint value in argument to c.modbus.ChangeBatteryForceCharge
if err != nil {
c.errChannel <- err
}
}

if wattsRequired > 0 {
if wattsFromGrid > 0 {
metrics.SetMetricValue("control", "action", map[string]string{"action": "pull_from_grid"}, 1)
c.logger.WithFields(logrus.Fields{"wattsRequired": wattsRequired}).Info("Pulling watts from grid")
c.logger.WithFields(logrus.Fields{"wattsFromGrid": wattsFromGrid}).Info("Pulling watts from grid")
} else {
metrics.SetMetricValue("control", "action", map[string]string{"action": "pull_from_grid"}, 0)
}
Expand Down

0 comments on commit 89ef984

Please sign in to comment.