Skip to content

Commit

Permalink
Battery control: improve error handling (#13186)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Mar 28, 2024
1 parent 198e94e commit 72d33b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
8 changes: 5 additions & 3 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,11 @@ func (site *Site) update(lp updater) {
flexiblePower = site.prioritizer.GetChargePowerFlexibility(lp)
}

smartCostActive, err := site.smartCostActive(lp)
if err != nil {
site.log.ERROR.Println("smartCost:", err)
var smartCostActive bool
if rate, err := site.plannerRate(); err == nil {
smartCostActive = site.smartCostActive(lp, rate)
} else {
site.log.WARN.Println("smartCost:", err)
}

if sitePower, batteryBuffered, batteryStart, err := site.sitePower(totalChargePower, flexiblePower); err == nil {
Expand Down
25 changes: 15 additions & 10 deletions core/site_battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,40 @@ func (site *Site) applyBatteryMode(mode api.BatteryMode) error {
return nil
}

func (site *Site) smartCostActive(lp loadpoint.API) (bool, error) {
func (site *Site) plannerRate() (*api.Rate, error) {
tariff := site.GetTariff(PlannerTariff)
if tariff == nil || tariff.Type() == api.TariffTypePriceStatic {
return false, nil
return nil, nil
}

rates, err := tariff.Rates()
if err != nil {
return false, err
return nil, err
}

rate, err := rates.Current(time.Now())
if err != nil {
return false, err
return nil, err
}

return &rate, nil
}

func (site *Site) smartCostActive(lp loadpoint.API, rate *api.Rate) bool {
limit := lp.GetSmartCostLimit()
return limit != 0 && rate.Price <= limit, nil
return limit != 0 && rate != nil && rate.Price <= limit
}

func (site *Site) updateBatteryMode() {
mode := api.BatteryNormal

for _, lp := range site.Loadpoints() {
smartCostActive, err := site.smartCostActive(lp)
if err != nil {
site.log.WARN.Println("smart cost:", err)
}
rate, err := site.plannerRate()
if err != nil {
site.log.WARN.Println("smart cost:", err)
}

for _, lp := range site.Loadpoints() {
smartCostActive := site.smartCostActive(lp, rate)
if lp.GetStatus() == api.StatusC && (smartCostActive || lp.IsFastChargingActive()) {
mode = api.BatteryHold
break
Expand Down

0 comments on commit 72d33b5

Please sign in to comment.