Skip to content

Commit

Permalink
Fix taking vehicle priorities into account (#8884)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jul 10, 2023
1 parent 4c72744 commit fb20a3b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 60 deletions.
6 changes: 5 additions & 1 deletion core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (lp *Loadpoint) collectDefaults() {
*actionCfg.MaxCurrent = lp.GetMaxCurrent()
*actionCfg.MinSoc = lp.GetMinSoc()
*actionCfg.TargetSoc = lp.GetTargetSoc()
*actionCfg.Priority = lp.Priority()
*actionCfg.Priority = lp.GetPriority()
} else {
lp.log.ERROR.Printf("error allocating action config: %v", err)
}
Expand Down Expand Up @@ -557,6 +557,9 @@ func (lp *Loadpoint) applyAction(actionCfg api.ActionConfig) {
if actionCfg.TargetSoc != nil {
lp.SetTargetSoc(*actionCfg.TargetSoc)
}
if actionCfg.Priority != nil {
lp.SetPriority(*actionCfg.Priority)
}
}

// Prepare loadpoint configuration by adding missing helper elements
Expand Down Expand Up @@ -603,6 +606,7 @@ func (lp *Loadpoint) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Even
}

lp.publish("mode", lp.GetMode())
lp.publish("priority", lp.GetPriority())
lp.publish(targetSoc, lp.GetTargetSoc())
lp.publish(minSoc, lp.GetMinSoc())

Expand Down
6 changes: 5 additions & 1 deletion core/loadpoint/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Controller interface {
type API interface {
// Title returns the defined loadpoint title
Title() string
Priority() int

//
// status
Expand All @@ -30,6 +29,11 @@ type API interface {
// settings
//

// GetPriority returns the loadpoint priority
GetPriority() int
// SetPriority sets the loadpoint priority
SetPriority(int)

// GetMode returns the charge mode
GetMode() api.ChargeMode
// SetMode sets the charge mode
Expand Down
40 changes: 26 additions & 14 deletions core/loadpoint/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions core/loadpoint_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ func (lp *Loadpoint) Title() string {
return lp.Title_
}

// Priority returns the loadpoint priority
func (lp *Loadpoint) Priority() int {
return lp.Priority_
}

// GetStatus returns the charging status
func (lp *Loadpoint) GetStatus() api.ChargeStatus {
lp.Lock()
Expand Down Expand Up @@ -102,6 +97,26 @@ func (lp *Loadpoint) SetTargetEnergy(energy float64) {
}
}

// GetPriority returns the loadpoint priority
func (lp *Loadpoint) GetPriority() int {
lp.Lock()
defer lp.Unlock()
return lp.Priority_
}

// SetPriority sets the loadpoint priority
func (lp *Loadpoint) SetPriority(prio int) {
lp.Lock()
defer lp.Unlock()

lp.log.DEBUG.Println("set priority:", prio)

if lp.Priority_ != prio {
lp.Priority_ = prio
lp.publish("priority", prio)
}
}

// GetTargetSoc returns loadpoint charge target soc
func (lp *Loadpoint) GetTargetSoc() int {
lp.Lock()
Expand Down
22 changes: 18 additions & 4 deletions core/prioritizer/prioritizer.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package prioritizer

import (
"fmt"

"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/util"
)

type Prioritizer struct {
log *util.Logger
demand map[loadpoint.API]float64
}

func New() *Prioritizer {
func New(log *util.Logger) *Prioritizer {
return &Prioritizer{
log: log,
demand: make(map[loadpoint.API]float64),
}
}
Expand All @@ -21,14 +26,23 @@ func (p *Prioritizer) UpdateChargePowerFlexibility(lp loadpoint.API) {
}

func (p *Prioritizer) GetChargePowerFlexibility(lp loadpoint.API) float64 {
prio := lp.Priority()
prio := lp.GetPriority()

var (
reduceBy float64
msg string
)

var reduceBy float64
for lp, power := range p.demand {
if lp.Priority() < prio {
if lp.GetPriority() < prio {
reduceBy += power
msg += fmt.Sprintf("%.0fW from %s at prio %d, ", power, lp.Title(), lp.GetPriority())
}
}

if p.log != nil && reduceBy > 0 {
p.log.DEBUG.Printf("lp %s at prio %d gets additional %stotal %.0fW\n", lp.Title(), lp.GetPriority(), msg, reduceBy)
}

return reduceBy
}
8 changes: 5 additions & 3 deletions core/prioritizer/prioritizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
func TestPrioritzer(t *testing.T) {
ctrl := gomock.NewController(t)

p := New()
p := New(nil)

lo := loadpoint.NewMockAPI(ctrl)
lo.EXPECT().Priority().Return(0).AnyTimes()
lo.EXPECT().Title().AnyTimes()
lo.EXPECT().GetPriority().Return(0).AnyTimes()

hi := loadpoint.NewMockAPI(ctrl)
hi.EXPECT().Priority().Return(1).AnyTimes()
hi.EXPECT().Title().AnyTimes()
hi.EXPECT().GetPriority().Return(1).AnyTimes()

// no additional power available
lo.EXPECT().GetChargePowerFlexibility().Return(300.0)
Expand Down
2 changes: 1 addition & 1 deletion core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewSiteFromConfig(
site.loadpoints = loadpoints
site.tariffs = tariffs
site.coordinator = coordinator.New(log, vehicles)
site.prioritizer = prioritizer.New()
site.prioritizer = prioritizer.New(log)
site.savings = NewSavings(tariffs)

site.restoreSettings()
Expand Down
28 changes: 1 addition & 27 deletions templates/definition/vehicle/offline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ params:
- name: capacity
- name: phases
advanced: true
- name: mode
advanced: true
- name: minCurrent
advanced: true
- name: maxCurrent
advanced: true
- name: identifiers
advanced: true
type: stringlist
- preset: vehicle-identify
render: |
type: custom
Expand All @@ -37,21 +28,4 @@ render: |
soc:
source: const
value: 0
onIdentify:
{{- if .mode }}
mode: {{ .mode }}
{{- end }}
minSoc: 0 # fixed - no soc available
targetSoc: 100 # fixed - no soc available
{{- if .minCurrent }}
minCurrent: {{ .minCurrent }}
{{- end }}
{{- if .maxCurrent }}
maxCurrent: {{ .maxCurrent }}
{{- end }}
{{- if (len .identifiers) }}
identifiers:
{{- range .identifiers }}
- {{ . }}
{{- end }}
{{- end }}
{{ include "vehicle-identify" . }}
4 changes: 0 additions & 4 deletions templates/docs/vehicle/offline_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ render:
capacity: 50 # Akkukapazität in kWh (Optional)
phases: 3 # Die maximale Anzahl der Phasen welche genutzt werden können (Optional)
mode: # Möglich sind Off, Now, MinPV und PV, oder leer wenn keiner definiert werden soll (Optional)
minCurrent: 6 # Definiert die minimale Stromstärke pro angeschlossener Phase mit welcher das Fahrzeug geladen werden soll (Optional)
maxCurrent: 16 # Definiert die maximale Stromstärke pro angeschlossener Phase mit welcher das Fahrzeug geladen werden soll (Optional)
identifiers: # Kann meist erst später eingetragen werden, siehe: https://docs.evcc.io/docs/guides/vehicles/#erkennung-des-fahrzeugs-an-der-wallbox (Optional)
mode: # Möglich sind Off, Now, MinPV und PV, oder leer wenn keiner definiert werden soll (Optional)
minSoc: 25 # Ladung mit maximaler Geschwindigkeit bis zu dem angegeben Ladestand unabhängig PV-Erzeugung, wenn der Lademodus nicht auf 'Aus' steht (Optional)
targetSoc: 80 # Bis zu welchem Ladestand (Soc) soll das Fahrzeug geladen werden (Optional)
minCurrent: 6 # Definiert die minimale Stromstärke pro angeschlossener Phase mit welcher das Fahrzeug geladen werden soll (Optional)
Expand Down

0 comments on commit fb20a3b

Please sign in to comment.