forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
1004 lines (840 loc) Β· 28.8 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package core
import (
"context"
"errors"
"fmt"
"math"
"strings"
"sync"
"testing"
"time"
"github.com/avast/retry-go/v4"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/cmd/shutdown"
"github.com/evcc-io/evcc/core/coordinator"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/planner"
"github.com/evcc-io/evcc/core/prioritizer"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/core/soc"
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/push"
"github.com/evcc-io/evcc/server/db"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/tariff"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/telemetry"
"github.com/smallnest/chanx"
)
const standbyPower = 10 // consider less than 10W as charger in standby
// Updater abstracts the Loadpoint implementation for testing
type Updater interface {
loadpoint.API
Update(availablePower float64, autoCharge, batteryBuffered, batteryStart bool, greenShare float64, effectivePrice, effectiveCo2 *float64)
}
// meterMeasurement is used as slice element for publishing structured data
type meterMeasurement struct {
Power float64 `json:"power"`
Energy float64 `json:"energy,omitempty"`
}
// batteryMeasurement is used as slice element for publishing structured data
type batteryMeasurement struct {
Power float64 `json:"power"`
Energy float64 `json:"energy,omitempty"`
Soc float64 `json:"soc,omitempty"`
Capacity float64 `json:"capacity,omitempty"`
Controllable bool `json:"controllable"`
}
// Site is the main configuration container. A site can host multiple loadpoints.
type Site struct {
uiChan chan<- util.Param // client push messages
lpUpdateChan chan *Loadpoint
*Health
sync.RWMutex
log *util.Logger
// configuration
Title string `mapstructure:"title"` // UI title
Voltage float64 `mapstructure:"voltage"` // Operating voltage. 230V for Germany.
ResidualPower float64 `mapstructure:"residualPower"` // PV meter only: household usage. Grid meter: household safety margin
Meters MetersConfig // Meter references
MaxGridSupplyWhileBatteryCharging float64 `mapstructure:"maxGridSupplyWhileBatteryCharging"` // ignore battery charging if AC consumption is above this value
// meters
gridMeter api.Meter // Grid usage meter
pvMeters []api.Meter // PV generation meters
batteryMeters []api.Meter // Battery charging meters
auxMeters []api.Meter // Auxiliary meters
otherMeters []api.Meter // Other meters
// cost settings
smartCostLimit float64 // always charge if cost is below this value
// battery settings
prioritySoc float64 // prefer battery up to this Soc
bufferSoc float64 // continue charging on battery above this Soc
bufferStartSoc float64 // start charging on battery above this Soc
batteryDischargeControl bool // prevent battery discharge for fast and planned charging
loadpoints []*Loadpoint // Loadpoints
tariffs *tariff.Tariffs // Tariffs
coordinator *coordinator.Coordinator // Vehicles
prioritizer *prioritizer.Prioritizer // Power budgets
stats *Stats // Stats
// cached state
gridPower float64 // Grid power
pvPower float64 // PV power
batteryPower float64 // Battery charge power
batterySoc float64 // Battery soc
batteryMode api.BatteryMode // Battery mode
publishCache map[string]any // store last published values to avoid unnecessary republishing
}
// MetersConfig contains the loadpoint's meter configuration
type MetersConfig struct {
GridMeterRef string `mapstructure:"grid"` // Grid usage meter
PVMetersRef []string `mapstructure:"pv"` // PV meter
BatteryMetersRef []string `mapstructure:"battery"` // Battery charging meter
AuxMetersRef []string `mapstructure:"aux"` // Auxiliary meters
OtherMetersRef []string `mapstructure:"other"` // Other meters
}
// NewSiteFromConfig creates a new site
func NewSiteFromConfig(
log *util.Logger,
other map[string]interface{},
loadpoints []*Loadpoint,
tariffs *tariff.Tariffs,
) (*Site, error) {
site := NewSite()
if err := util.DecodeOther(other, site); err != nil {
return nil, err
}
Voltage = site.Voltage
site.loadpoints = loadpoints
site.tariffs = tariffs
handler := config.Vehicles()
site.coordinator = coordinator.New(log, config.Instances(handler.Devices()))
handler.Subscribe(site.updateVehicles)
site.prioritizer = prioritizer.New(log)
site.stats = NewStats()
// upload telemetry on shutdown
if telemetry.Enabled() {
shutdown.Register(func() {
telemetry.Persist(log)
})
}
tariff := site.GetTariff(PlannerTariff)
// give loadpoints access to vehicles and database
for _, lp := range loadpoints {
lp.coordinator = coordinator.NewAdapter(lp, site.coordinator)
lp.planner = planner.New(lp.log, tariff)
if db.Instance != nil {
var err error
if lp.db, err = session.NewStore(lp.Title(), db.Instance); err != nil {
return nil, err
}
// Fix any dangling history
if err := lp.db.ClosePendingSessionsInHistory(lp.chargeMeterTotal()); err != nil {
return nil, err
}
// NOTE: this requires stopSession to respect async access
shutdown.Register(lp.stopSession)
}
}
// add meters from config
site.restoreMeters()
// grid meter
if site.Meters.GridMeterRef != "" {
dev, err := config.Meters().ByName(site.Meters.GridMeterRef)
if err != nil {
return nil, err
}
site.gridMeter = dev.Instance()
}
// multiple pv
for _, ref := range site.Meters.PVMetersRef {
dev, err := config.Meters().ByName(ref)
if err != nil {
return nil, err
}
site.pvMeters = append(site.pvMeters, dev.Instance())
}
// multiple batteries
for _, ref := range site.Meters.BatteryMetersRef {
dev, err := config.Meters().ByName(ref)
if err != nil {
return nil, err
}
site.batteryMeters = append(site.batteryMeters, dev.Instance())
}
if len(site.batteryMeters) > 0 && site.ResidualPower <= 0 {
site.log.WARN.Println("battery configured but residualPower is missing or <= 0 (add residualPower: 100 to site), see https://docs.evcc.io/en/docs/reference/configuration/site#residualpower")
}
// auxiliary meters
for _, ref := range site.Meters.AuxMetersRef {
dev, err := config.Meters().ByName(ref)
if err != nil {
return nil, err
}
site.auxMeters = append(site.auxMeters, dev.Instance())
}
// other meters
for _, ref := range site.Meters.OtherMetersRef {
dev, err := config.Meters().ByName(ref)
if err != nil {
return nil, err
}
site.otherMeters = append(site.otherMeters, dev.Instance())
}
// configure meter from references
if site.gridMeter == nil && len(site.pvMeters) == 0 {
return nil, errors.New("missing either grid or pv meter")
}
// revert battery mode on shutdown
shutdown.Register(func() {
if mode := site.GetBatteryMode(); batteryModeModified(mode) {
if err := site.updateBatteryMode(api.BatteryNormal); err != nil {
site.log.ERROR.Println("battery mode:", err)
}
}
})
return site, nil
}
// NewSite creates a Site with sane defaults
func NewSite() *Site {
lp := &Site{
log: util.NewLogger("site"),
publishCache: make(map[string]any),
Voltage: 230, // V
}
return lp
}
// restoreMeters restores site meter configuration
func (site *Site) restoreMeters() {
if testing.Testing() {
return
}
if v, err := settings.String(keys.GridMeter); err == nil && v != "" {
site.Meters.GridMeterRef = v
}
if v, err := settings.String(keys.PvMeters); err == nil && v != "" {
site.Meters.PVMetersRef = append(site.Meters.PVMetersRef, strings.Split(v, ",")...)
}
if v, err := settings.String(keys.BatteryMeters); err == nil && v != "" {
site.Meters.BatteryMetersRef = append(site.Meters.BatteryMetersRef, strings.Split(v, ",")...)
}
if v, err := settings.String(keys.AuxMeters); err == nil && v != "" {
site.Meters.AuxMetersRef = append(site.Meters.AuxMetersRef, strings.Split(v, ",")...)
}
if v, err := settings.String(keys.OtherMeters); err == nil && v != "" {
site.Meters.OtherMetersRef = append(site.Meters.OtherMetersRef, strings.Split(v, ",")...)
}
}
// restoreSettings restores site settings
func (site *Site) restoreSettings() error {
if testing.Testing() {
return nil
}
if v, err := settings.String(keys.Title); err == nil {
site.Title = v
}
if v, err := settings.Float(keys.BufferSoc); err == nil {
if err := site.SetBufferSoc(v); err != nil {
return err
}
}
if v, err := settings.Float(keys.BufferStartSoc); err == nil {
if err := site.SetBufferStartSoc(v); err != nil {
return err
}
}
if v, err := settings.Float(keys.SmartCostLimit); err == nil {
if err := site.SetSmartCostLimit(v); err != nil {
return err
}
}
if v, err := settings.Float(keys.PrioritySoc); err == nil {
if err := site.SetPrioritySoc(v); err != nil {
return err
}
}
if v, err := settings.Bool(keys.BatteryDischargeControl); err == nil {
if err := site.SetBatteryDischargeControl(v); err != nil {
return err
}
}
return nil
}
func meterCapabilities(name string, meter interface{}) string {
_, power := meter.(api.Meter)
_, energy := meter.(api.MeterEnergy)
_, currents := meter.(api.PhaseCurrents)
name += ":"
return fmt.Sprintf(" %-10s power %s energy %s currents %s",
name,
presence[power],
presence[energy],
presence[currents],
)
}
// DumpConfig site configuration
func (site *Site) DumpConfig() {
// verify vehicle detection
if vehicles := site.Vehicles().Instances(); len(vehicles) > 1 {
for _, v := range vehicles {
if _, ok := v.(api.ChargeState); !ok {
site.log.WARN.Printf("vehicle '%s' does not support automatic detection", v.Title())
}
}
}
site.log.INFO.Println("site config:")
site.log.INFO.Printf(" meters: grid %s pv %s battery %s",
presence[site.gridMeter != nil],
presence[len(site.pvMeters) > 0],
presence[len(site.batteryMeters) > 0],
)
if site.gridMeter != nil {
site.log.INFO.Println(meterCapabilities("grid", site.gridMeter))
}
if len(site.pvMeters) > 0 {
for i, pv := range site.pvMeters {
site.log.INFO.Println(meterCapabilities(fmt.Sprintf("pv %d", i+1), pv))
}
}
if len(site.batteryMeters) > 0 {
for i, battery := range site.batteryMeters {
_, ok := battery.(api.Battery)
_, hasCapacity := battery.(api.BatteryCapacity)
site.log.INFO.Println(
meterCapabilities(fmt.Sprintf("battery %d", i+1), battery),
fmt.Sprintf("soc %s capacity %s", presence[ok], presence[hasCapacity]),
)
}
}
if vehicles := site.Vehicles().Instances(); len(vehicles) > 0 {
site.log.INFO.Println(" vehicles:")
for i, v := range vehicles {
_, rng := v.(api.VehicleRange)
_, finish := v.(api.VehicleFinishTimer)
_, status := v.(api.ChargeState)
_, climate := v.(api.VehicleClimater)
_, wakeup := v.(api.Resurrector)
site.log.INFO.Printf(" vehicle %d: range %s finish %s status %s climate %s wakeup %s",
i+1, presence[rng], presence[finish], presence[status], presence[climate], presence[wakeup],
)
}
}
for i, lp := range site.loadpoints {
lp.log.INFO.Printf("loadpoint %d:", i+1)
lp.log.INFO.Printf(" mode: %s", lp.GetMode())
_, power := lp.charger.(api.Meter)
_, energy := lp.charger.(api.MeterEnergy)
_, currents := lp.charger.(api.PhaseCurrents)
_, phases := lp.charger.(api.PhaseSwitcher)
_, wakeup := lp.charger.(api.Resurrector)
lp.log.INFO.Printf(" charger: power %s energy %s currents %s phases %s wakeup %s",
presence[power],
presence[energy],
presence[currents],
presence[phases],
presence[wakeup],
)
lp.log.INFO.Printf(" meters: charge %s", presence[lp.HasChargeMeter()])
if lp.HasChargeMeter() {
lp.log.INFO.Printf(meterCapabilities("charge", lp.chargeMeter))
}
}
}
// publish sends values to UI and databases
func (site *Site) publish(key string, val interface{}) {
// test helper
if site.uiChan == nil {
return
}
if s, ok := val.(fmt.Stringer); ok {
val = s.String()
}
site.uiChan <- util.Param{Key: key, Val: val}
}
// publishDelta deduplicates messages before publishing
func (site *Site) publishDelta(key string, val interface{}) {
if v, ok := site.publishCache[key]; ok && v == val {
return
}
site.publishCache[key] = val
site.publish(key, val)
}
// updateMeter updates and publishes single meter
func (site *Site) updateMeter(meter api.Meter, power *float64) func() error {
return func() error {
value, err := meter.CurrentPower()
if err == nil {
*power = value // update value if no error
}
return err
}
}
// retryMeter retries meter update
func (site *Site) retryMeter(name string, meter api.Meter, power *float64) error {
if meter == nil {
return nil
}
err := retry.Do(site.updateMeter(meter, power), retryOptions...)
if err == nil {
site.log.DEBUG.Printf("%s power: %.0fW", name, *power)
site.publish(name+"Power", *power)
} else {
err = fmt.Errorf("%s meter: %v", name, err)
site.log.ERROR.Println(err)
}
return err
}
// updateMeter updates and publishes single meter
func (site *Site) updateMeters() error {
if len(site.pvMeters) > 0 {
var totalEnergy float64
site.pvPower = 0
mm := make([]meterMeasurement, len(site.pvMeters))
for i, meter := range site.pvMeters {
// pv power
var power float64
err := retry.Do(site.updateMeter(meter, &power), retryOptions...)
if err == nil {
// ignore negative values which represent self-consumption
site.pvPower += max(0, power)
if power < -500 {
site.log.WARN.Printf("pv %d power: %.0fW is negative - check configuration if sign is correct", i+1, power)
}
} else {
err = fmt.Errorf("pv %d power: %v", i+1, err)
site.log.ERROR.Println(err)
}
// pv energy (production)
var energy float64
if m, ok := meter.(api.MeterEnergy); err == nil && ok {
energy, err = m.TotalEnergy()
if err == nil {
totalEnergy += energy
} else {
site.log.ERROR.Printf("pv %d energy: %v", i+1, err)
}
}
mm[i] = meterMeasurement{
Power: power,
Energy: energy,
}
}
site.log.DEBUG.Printf("pv power: %.0fW", site.pvPower)
site.publish(keys.PvPower, site.pvPower)
site.publish(keys.PvEnergy, totalEnergy)
site.publish(keys.Pv, mm)
}
if len(site.batteryMeters) > 0 {
var totalCapacity float64
var totalEnergy float64
site.batteryPower = 0
site.batterySoc = 0
mm := make([]batteryMeasurement, len(site.batteryMeters))
for i, meter := range site.batteryMeters {
// battery power
var power float64
// NOTE battery errors are logged but ignored as we don't consider them relevant
err := retry.Do(site.updateMeter(meter, &power), retryOptions...)
if err == nil {
site.batteryPower += power
if len(site.batteryMeters) > 1 {
site.log.DEBUG.Printf("battery %d power: %.0fW", i+1, power)
}
} else {
site.log.ERROR.Printf("battery %d power: %v", i+1, err)
}
// battery energy (discharge)
var energy float64
if m, ok := meter.(api.MeterEnergy); err == nil && ok {
energy, err = m.TotalEnergy()
if err == nil {
totalEnergy += energy
} else {
site.log.ERROR.Printf("battery %d energy: %v", i+1, err)
}
}
// battery soc and capacity
var batSoc, capacity float64
if meter, ok := meter.(api.Battery); ok {
batSoc, err = soc.Guard(meter.Soc())
if err == nil {
// weigh soc by capacity and accumulate total capacity
weighedSoc := batSoc
if m, ok := meter.(api.BatteryCapacity); ok {
capacity = m.Capacity()
totalCapacity += capacity
weighedSoc *= capacity
}
site.batterySoc += weighedSoc
if len(site.batteryMeters) > 1 {
site.log.DEBUG.Printf("battery %d soc: %.0f%%", i+1, batSoc)
}
} else {
site.log.ERROR.Printf("battery %d soc: %v", i+1, err)
}
}
_, controllable := meter.(api.BatteryController)
mm[i] = batteryMeasurement{
Power: power,
Energy: energy,
Soc: batSoc,
Capacity: capacity,
Controllable: controllable,
}
}
site.publish(keys.BatteryCapacity, totalCapacity)
// convert weighed socs to total soc
if totalCapacity == 0 {
totalCapacity = float64(len(site.batteryMeters))
}
site.batterySoc /= totalCapacity
site.log.DEBUG.Printf("battery soc: %.0f%%", math.Round(site.batterySoc))
site.publish(keys.BatterySoc, site.batterySoc)
site.log.DEBUG.Printf("battery power: %.0fW", site.batteryPower)
site.publish(keys.BatteryPower, site.batteryPower)
site.publish(keys.BatteryEnergy, totalEnergy)
site.publish(keys.Battery, mm)
}
// grid power
err := site.retryMeter("grid", site.gridMeter, &site.gridPower)
// grid phase powers
var p1, p2, p3 float64
if phaseMeter, ok := site.gridMeter.(api.PhasePowers); err == nil && ok {
p1, p2, p3, err = phaseMeter.Powers()
if err == nil {
phases := []float64{p1, p2, p3}
site.log.DEBUG.Printf("grid powers: %.0fW", phases)
site.publish(keys.GridPowers, phases)
} else {
err = fmt.Errorf("grid powers: %w", err)
}
}
// grid phase currents (signed)
if phaseMeter, ok := site.gridMeter.(api.PhaseCurrents); err == nil && ok {
var i1, i2, i3 float64
i1, i2, i3, err = phaseMeter.Currents()
if err == nil {
phases := []float64{util.SignFromPower(i1, p1), util.SignFromPower(i2, p2), util.SignFromPower(i3, p3)}
site.log.DEBUG.Printf("grid currents: %.3gA", phases)
site.publish(keys.GridCurrents, phases)
} else {
err = fmt.Errorf("grid currents: %w", err)
}
}
// grid energy (import)
if energyMeter, ok := site.gridMeter.(api.MeterEnergy); err == nil && ok {
var f float64
f, err = energyMeter.TotalEnergy()
if err == nil {
site.publish(keys.GridEnergy, f)
} else {
site.log.ERROR.Printf("grid energy: %v", err)
}
}
return err
}
// sitePower returns
// - the net power exported by the site minus a residual margin
// (negative values mean grid: export, battery: charging
// - if battery buffer can be used for charging
func (site *Site) sitePower(totalChargePower, flexiblePower float64) (float64, bool, bool, error) {
if err := site.updateMeters(); err != nil {
return 0, false, false, err
}
// allow using PV as estimate for grid power
if site.gridMeter == nil {
site.gridPower = totalChargePower - site.pvPower
}
// allow using grid and charge as estimate for pv power
if site.pvMeters == nil {
site.pvPower = totalChargePower - site.gridPower + site.ResidualPower
if site.pvPower < 0 {
site.pvPower = 0
}
site.log.DEBUG.Printf("pv power: %.0fW", site.pvPower)
site.publish(keys.PvPower, site.pvPower)
}
// honour battery priority
batteryPower := site.batteryPower
// handed to loadpoint
var batteryBuffered, batteryStart bool
if len(site.batteryMeters) > 0 {
site.RLock()
defer site.RUnlock()
// if battery is charging below prioritySoc give it priority
if site.batterySoc < site.prioritySoc && batteryPower < 0 {
site.log.DEBUG.Printf("battery has priority at soc %.0f%% (< %.0f%%)", site.batterySoc, site.prioritySoc)
batteryPower = 0
} else {
// if battery is above bufferSoc allow using it for charging
batteryBuffered = site.bufferSoc > 0 && site.batterySoc > site.bufferSoc
batteryStart = site.bufferStartSoc > 0 && site.batterySoc > site.bufferStartSoc
}
}
sitePower := sitePower(site.log, site.MaxGridSupplyWhileBatteryCharging, site.gridPower, batteryPower, site.ResidualPower)
// deduct smart loads
if len(site.auxMeters) > 0 {
var auxPower float64
mm := make([]meterMeasurement, len(site.auxMeters))
for i, meter := range site.auxMeters {
if power, err := meter.CurrentPower(); err == nil {
auxPower += power
mm[i].Power = power
site.log.DEBUG.Printf("aux power %d: %.0fW", i+1, power)
} else {
site.log.ERROR.Printf("aux meter %d: %v", i+1, err)
}
}
sitePower -= auxPower
site.log.DEBUG.Printf("aux power: %.0fW", auxPower)
site.publish(keys.AuxPower, auxPower)
site.publish(keys.Aux, mm)
}
// handle other meters
if len(site.otherMeters) > 0 {
var otherPower float64
var otherEnergy float64
mm := make([]meterMeasurement, len(site.otherMeters))
for i, meter := range site.otherMeters {
if power, err := meter.CurrentPower(); err == nil {
otherPower += power
mm[i].Power = power
site.log.DEBUG.Printf("other power %d: %.0fW", i+1, power)
} else {
site.log.ERROR.Printf("other meter %d: %v", i+1, err)
}
if m, ok := meter.(api.MeterEnergy); ok {
if energy, err := m.TotalEnergy(); err == nil {
otherEnergy += energy
mm[i].Energy = energy
site.log.DEBUG.Printf("other energy %d: %v", i+1, energy)
} else {
site.log.ERROR.Printf("other meter %d: %v", i+1, err)
}
}
}
site.log.DEBUG.Printf("other power: %.0fW", otherPower)
site.publish(keys.OtherPower, otherPower)
site.log.DEBUG.Printf("other energy: %v", otherEnergy)
site.publish(keys.OtherEnergy, otherEnergy)
site.publish(keys.Other, mm)
}
// handle priority
if flexiblePower > 0 {
site.log.DEBUG.Printf("giving loadpoint priority for additional: %.0fW", flexiblePower)
sitePower -= flexiblePower
}
site.log.DEBUG.Printf("site power: %.0fW", sitePower)
return sitePower, batteryBuffered, batteryStart, nil
}
// greenShare returns
// - the current green share, calculated for the part of the consumption between powerFrom and powerTo
// the consumption below powerFrom will get the available green power first
func (site *Site) greenShare(powerFrom float64, powerTo float64) float64 {
greenPower := math.Max(0, site.pvPower) + math.Max(0, site.batteryPower)
greenPowerAvailable := math.Max(0, greenPower-powerFrom)
power := powerTo - powerFrom
share := math.Min(greenPowerAvailable, power) / power
if math.IsNaN(share) {
if greenPowerAvailable > 0 {
share = 1
} else {
share = 0
}
}
return share
}
// effectivePrice calculates the real energy price based on self-produced and grid-imported energy.
func (site *Site) effectivePrice(greenShare float64) *float64 {
if grid, err := site.tariffs.CurrentGridPrice(); err == nil {
feedin, err := site.tariffs.CurrentFeedInPrice()
if err != nil {
feedin = 0
}
effPrice := grid*(1-greenShare) + feedin*greenShare
return &effPrice
}
return nil
}
// effectiveCo2 calculates the amount of emitted co2 based on self-produced and grid-imported energy.
func (site *Site) effectiveCo2(greenShare float64) *float64 {
if co2, err := site.tariffs.CurrentCo2(); err == nil {
effCo2 := co2 * (1 - greenShare)
return &effCo2
}
return nil
}
func (site *Site) publishTariffs(greenShareHome float64, greenShareLoadpoints float64) {
site.publish(keys.GreenShareHome, greenShareHome)
site.publish(keys.GreenShareLoadpoints, greenShareLoadpoints)
if gridPrice, err := site.tariffs.CurrentGridPrice(); err == nil {
site.publishDelta(keys.TariffGrid, gridPrice)
}
if feedInPrice, err := site.tariffs.CurrentFeedInPrice(); err == nil {
site.publishDelta(keys.TariffFeedIn, feedInPrice)
}
if co2, err := site.tariffs.CurrentCo2(); err == nil {
site.publishDelta(keys.TariffCo2, co2)
}
if price := site.effectivePrice(greenShareHome); price != nil {
site.publish(keys.TariffPriceHome, price)
}
if co2 := site.effectiveCo2(greenShareHome); co2 != nil {
site.publish(keys.TariffCo2Home, co2)
}
if price := site.effectivePrice(greenShareLoadpoints); price != nil {
site.publish(keys.TariffPriceLoadpoints, price)
}
if co2 := site.effectiveCo2(greenShareLoadpoints); co2 != nil {
site.publish(keys.TariffCo2Loadpoints, co2)
}
}
func (site *Site) update(lp Updater) {
site.log.DEBUG.Println("----")
// update all loadpoint's charge power
var totalChargePower float64
for _, lp := range site.loadpoints {
lp.UpdateChargePower()
totalChargePower += lp.GetChargePower()
site.prioritizer.UpdateChargePowerFlexibility(lp)
}
// prioritize if possible
var flexiblePower float64
if lp.GetMode() == api.ModePV {
flexiblePower = site.prioritizer.GetChargePowerFlexibility(lp)
}
var smartCostActive bool
if tariff := site.GetTariff(PlannerTariff); tariff != nil && tariff.Type() != api.TariffTypePriceStatic {
rates, err := tariff.Rates()
var rate api.Rate
if err == nil {
rate, err = rates.Current(time.Now())
}
if err == nil {
limit := site.GetSmartCostLimit()
smartCostActive = limit != 0 && rate.Price <= limit
site.publish(keys.SmartCostActive, smartCostActive)
} else {
site.log.ERROR.Println("smartCost:", err)
}
}
if sitePower, batteryBuffered, batteryStart, err := site.sitePower(totalChargePower, flexiblePower); err == nil {
// ignore negative pvPower values as that means it is not an energy source but consumption
homePower := site.gridPower + max(0, site.pvPower) + site.batteryPower - totalChargePower
homePower = max(homePower, 0)
site.publish(keys.HomePower, homePower)
// add battery charging power to homePower to ignore all consumption which does not occur on loadpoints
// fix for: https://github.com/evcc-io/evcc/issues/11032
nonChargePower := homePower + max(0, -site.batteryPower)
greenShareHome := site.greenShare(0, homePower)
greenShareLoadpoints := site.greenShare(nonChargePower, nonChargePower+totalChargePower)
lp.Update(sitePower, smartCostActive, batteryBuffered, batteryStart, greenShareLoadpoints, site.effectivePrice(greenShareLoadpoints), site.effectiveCo2(greenShareLoadpoints))
site.Health.Update()
site.publishTariffs(greenShareHome, greenShareLoadpoints)
if telemetry.Enabled() && totalChargePower > standbyPower {
go telemetry.UpdateChargeProgress(site.log, totalChargePower, greenShareLoadpoints)
}
} else {
site.log.ERROR.Println(err)
}
if batMode := site.GetBatteryMode(); site.batteryDischargeControl {
if mode := site.determineBatteryMode(site.Loadpoints(), smartCostActive); mode != batMode {
if err := site.updateBatteryMode(mode); err != nil {
site.log.ERROR.Println("battery mode:", err)
}
}
}
site.stats.Update(site)
}
// prepare publishes initial values
func (site *Site) prepare() {
if err := site.restoreSettings(); err != nil {
site.log.ERROR.Println(err)
}
site.publish(keys.SiteTitle, site.Title)
site.publish(keys.GridConfigured, site.gridMeter != nil)
site.publish(keys.PvConfigured, len(site.pvMeters) > 0)
site.publish(keys.BatteryConfigured, len(site.batteryMeters) > 0)
site.publish(keys.BufferSoc, site.bufferSoc)
site.publish(keys.BufferStartSoc, site.bufferStartSoc)
site.publish(keys.PrioritySoc, site.prioritySoc)
site.publish(keys.BatteryMode, site.batteryMode)
site.publish(keys.BatteryDischargeControl, site.batteryDischargeControl)
site.publish(keys.ResidualPower, site.ResidualPower)
site.publish(keys.Currency, site.tariffs.Currency)
site.publish(keys.SmartCostActive, false)
site.publish(keys.SmartCostLimit, site.smartCostLimit)
if tariff := site.GetTariff(PlannerTariff); tariff != nil {
site.publish(keys.SmartCostType, tariff.Type())
} else {
site.publish(keys.SmartCostType, nil)
}
site.publishVehicles()
vehicle.Publish = site.publishVehicles
}
// Prepare attaches communication channels to site and loadpoints
func (site *Site) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Event) {
// https://github.com/evcc-io/evcc/issues/11191 prevent deadlock
// https://github.com/evcc-io/evcc/pull/11675 maintain message order
// infinite queue with channel semantics
ch := chanx.NewUnboundedChan[util.Param](context.Background(), 2)
// use ch.In for writing
site.uiChan = ch.In
// use ch.Out for reading
go func() {
for p := range ch.Out {
uiChan <- p
}
}()
site.lpUpdateChan = make(chan *Loadpoint, 1) // 1 capacity to avoid deadlock
site.prepare()
for id, lp := range site.loadpoints {
lpUIChan := make(chan util.Param)
lpPushChan := make(chan push.Event)
// pipe messages through go func to add id
go func(id int) {
for {
select {
case param := <-lpUIChan:
param.Loadpoint = &id
site.uiChan <- param
case ev := <-lpPushChan:
ev.Loadpoint = &id
pushChan <- ev
}
}
}(id)
lp.Prepare(lpUIChan, lpPushChan, site.lpUpdateChan)
}
}
// loopLoadpoints keeps iterating across loadpoints sending the next to the given channel
func (site *Site) loopLoadpoints(next chan<- Updater) {
for {
for _, lp := range site.loadpoints {
next <- lp
}
}
}
// Run is the main control loop. It reacts to trigger events by
// updating measurements and executing control logic.
func (site *Site) Run(stopC chan struct{}, interval time.Duration) {
site.Health = NewHealth(time.Minute + interval)
if max := 30 * time.Second; interval < max {
site.log.WARN.Printf("interval <%.0fs can lead to unexpected behavior, see https://docs.evcc.io/docs/reference/configuration/interval", max.Seconds())
}
loadpointChan := make(chan Updater)
go site.loopLoadpoints(loadpointChan)
ticker := time.NewTicker(interval)
site.update(<-loadpointChan) // start immediately
for {
select {
case <-ticker.C:
site.update(<-loadpointChan)
case lp := <-site.lpUpdateChan:
site.update(lp)
case <-stopC: