chore: fix slot length

This commit is contained in:
andig 2025-07-26 14:26:46 +02:00
parent 8eb2759950
commit 4cba60457c
4 changed files with 45 additions and 6 deletions

View file

@ -4,7 +4,6 @@ import (
"time"
"github.com/benbjohnson/clock"
"github.com/jinzhu/now"
"github.com/samber/lo"
)
@ -15,10 +14,12 @@ type meterEnergy struct {
Accumulated float64 `json:"accumulated"` // kWh
}
// AccumulatedEnergy returns the accumulated energy in kWh
func (m *meterEnergy) AccumulatedEnergy() float64 {
return m.Accumulated
}
// AddMeterTotal adds the difference to the last total meter value in kWh
func (m *meterEnergy) AddMeterTotal(v float64) {
defer func() {
m.updated = m.clock.Now()
@ -32,6 +33,7 @@ func (m *meterEnergy) AddMeterTotal(v float64) {
m.Accumulated += v - *m.meter
}
// AddEnergy adds the given energy in kWh
func (m *meterEnergy) AddEnergy(v float64) {
defer func() { m.updated = m.clock.Now() }()
@ -42,10 +44,7 @@ func (m *meterEnergy) AddEnergy(v float64) {
m.Accumulated += v
}
// AddPower adds the given power in W, calculating the energy based on the time since the last update
func (m *meterEnergy) AddPower(v float64) {
m.AddEnergy(v * m.clock.Since(m.updated).Hours() / 1e3)
}
func beginningOfDay(t time.Time) time.Time {
return now.With(t).BeginningOfDay()
}

View file

@ -784,7 +784,7 @@ func (site *Site) updateHouseholdConsumption(totalChargePower float64) {
return
}
slotDuration := time.Minute
slotDuration := 15 * time.Minute
slotStart := now.Truncate(slotDuration)
if slotStart.After(site.householdSlotStart) {

View file

@ -10,6 +10,7 @@ import (
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/tariff"
"github.com/jinzhu/now"
"github.com/samber/lo"
)
@ -172,3 +173,7 @@ func (site *Site) isDynamicTariff(usage api.TariffUsage) bool {
tariff := site.GetTariff(usage)
return tariff != nil && tariff.Type() != api.TariffTypePriceStatic
}
func beginningOfDay(t time.Time) time.Time {
return now.With(t).BeginningOfDay()
}

View file

@ -2,10 +2,16 @@ package core
import (
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/metrics"
"github.com/evcc-io/evcc/server/db"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGreenShare(t *testing.T) {
@ -155,3 +161,32 @@ func TestRequiredBatteryMode(t *testing.T) {
assert.Equal(t, tc.res, res, "expected %s, got %s", tc.res, res)
}
}
func TestUpdateHouseholdConsumption(t *testing.T) {
clock := clock.NewMock()
require.NoError(t, db.NewInstance("sqlite", ":memory:"))
metrics.Init()
s := &Site{
log: util.NewLogger("foo"),
gridPower: 4e3,
householdEnergy: &meterEnergy{clock: clock},
}
clock.Add(5 * time.Minute)
s.updateHouseholdConsumption(1e3)
require.False(t, s.householdEnergy.updated.IsZero())
clock.Add(5 * time.Minute)
s.updateHouseholdConsumption(1e3)
require.Equal(t, 0.25, s.householdEnergy.AccumulatedEnergy())
clock.Add(5 * time.Minute)
s.updateHouseholdConsumption(1e3)
require.Equal(t, 0.0, s.householdEnergy.AccumulatedEnergy()) // accumulator reset after 15 minutes
clock.Add(15 * time.Minute)
s.updateHouseholdConsumption(1e3)
require.Equal(t, 0.0, s.householdEnergy.AccumulatedEnergy()) // accumulator reset after 15 minutes
}