Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions core/loadpoint_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"errors"
"math"
"time"

"github.com/evcc-io/evcc/api"
Expand All @@ -12,6 +13,20 @@ import (
"github.com/jinzhu/now"
)

func roundFloat64(value float64, digits int) float64 {
factor := math.Pow10(digits)
return math.Round(value*factor) / factor
}

func roundFloat64Ptr(value *float64, digits int) *float64 {
if value == nil {
return nil
}

rounded := roundFloat64(*value, digits)
return &rounded
}

func (lp *Loadpoint) chargeMeterTotal() float64 {
m, ok := api.Cap[api.MeterEnergy](lp.chargeMeter)
if !ok {
Expand Down Expand Up @@ -55,7 +70,9 @@ func (lp *Loadpoint) createSession() {

if lp.site != nil {
lp.session.ReferencePricePerKWh = tariff.AverageRate(lp.site.GetTariff(api.TariffUsageGrid), 24*time.Hour)
lp.session.ReferenceCo2PerKWh = tariff.AverageRate(lp.site.GetTariff(api.TariffUsageCo2), 24*time.Hour)
lp.session.ReferenceCo2PerKWh = roundFloat64Ptr(
tariff.AverageRate(lp.site.GetTariff(api.TariffUsageCo2), 24*time.Hour), 4,
)
}

// energy
Expand All @@ -70,11 +87,11 @@ func (lp *Loadpoint) applyEnergyMetrics(s *session.Session) {
s.MeterStop = &meterStop
}

s.SolarPercentage = new(lp.energyMetrics.SolarPercentage())
s.SolarPercentage = roundFloat64Ptr(new(lp.energyMetrics.SolarPercentage()), 4)
s.Price = lp.energyMetrics.Price()
s.PricePerKWh = lp.energyMetrics.PricePerKWh()
s.Co2PerKWh = lp.energyMetrics.Co2PerKWh()
s.ChargedEnergy = lp.energyMetrics.TotalWh() / 1e3
s.PricePerKWh = roundFloat64Ptr(lp.energyMetrics.PricePerKWh(), 5)
s.Co2PerKWh = roundFloat64Ptr(lp.energyMetrics.Co2PerKWh(), 4)
s.ChargedEnergy = roundFloat64(lp.energyMetrics.TotalWh()/1e3, 5)

lp.db.Persist(s)
}
Expand Down
4 changes: 3 additions & 1 deletion core/session/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package session

import (
"math"

"github.com/evcc-io/evcc/server/db"
"github.com/evcc-io/evcc/util"
"gorm.io/gorm"
Expand Down Expand Up @@ -88,7 +90,7 @@ func (s *DB) ClosePendingSessionsInHistory(chargeMeterTotal float64) error {
}

if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = *session.MeterStop - *session.MeterStart
session.ChargedEnergy = math.Round((*session.MeterStop-*session.MeterStart)*1e5) / 1e5
s.Persist(session)
}
Comment on lines 92 to 95
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Reuse the shared rounding helper instead of duplicating the math here

Since this matches roundFloat64 with digits = 5, please call roundFloat64(*session.MeterStop-*session.MeterStart, 5) instead of inlining the math.Round and 1e5 factor. This keeps rounding behavior consistent with other energy fields and avoids future divergence.

Suggested change
if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = *session.MeterStop - *session.MeterStart
session.ChargedEnergy = math.Round((*session.MeterStop-*session.MeterStart)*1e5) / 1e5
s.Persist(session)
}
if session.MeterStart != nil && session.MeterStop != nil {
session.ChargedEnergy = roundFloat64(*session.MeterStop-*session.MeterStart, 5)
s.Persist(session)
}

}
Expand Down
Loading