Repository navigation
Demo: Seed charging and energy history #36850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+158
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/evcc-io/evcc/core/metrics" | ||
| "github.com/evcc-io/evcc/core/session" | ||
| "github.com/evcc-io/evcc/db" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func seedDemoHistory(now time.Time) error { | ||
| return db.Instance.Transaction(func(tx *gorm.DB) error { | ||
| var count int64 | ||
| if err := tx.Model(&session.Session{}).Count(&count).Error; err != nil || count != 0 { | ||
| return err | ||
| } | ||
| seeded, err := metrics.SeedDemoHistory(tx, now) | ||
| if err != nil || !seeded { | ||
| return err | ||
| } | ||
|
|
||
| const historyDays = 40 | ||
| energy := [...]float64{12.5, 23, 16, 8.5, 19, 14} | ||
| var sessions []session.Session | ||
| for daysAgo := historyDays - 1; daysAgo >= 0; daysAgo-- { | ||
| if daysAgo%10 == 3 { | ||
| continue | ||
| } | ||
| day := now.AddDate(0, 0, -daysAgo) | ||
| finished := time.Date(day.Year(), day.Month(), day.Day(), 13+daysAgo%7, 0, 0, 0, day.Location()) | ||
| if finished.After(now) { | ||
| finished = now | ||
| } | ||
| duration := 2 * time.Hour | ||
| created := finished.Add(-duration) | ||
| if created.Day() != day.Day() { | ||
| continue | ||
| } | ||
| i := daysAgo % len(energy) | ||
| solar := float64(95 - 14*i) | ||
| s := session.Session{ | ||
| Created: created, Finished: finished, | ||
| Loadpoint: "Carport", Vehicle: "blue e-Golf", ChargedEnergy: energy[i], | ||
| SolarPercentage: &solar, ChargeDuration: &duration, | ||
| } | ||
| if i%2 == 1 { | ||
| s.Loadpoint, s.Vehicle = "Garage", "white Model 3" | ||
| } | ||
| sessions = append(sessions, s) | ||
| } | ||
| return tx.Create(&sessions).Error | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/evcc-io/evcc/core/metrics" | ||
| "github.com/evcc-io/evcc/core/session" | ||
| "github.com/evcc-io/evcc/db" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSeedDemoHistory(t *testing.T) { | ||
| require.NoError(t, db.NewInstance("sqlite", ":memory:")) | ||
| t.Cleanup(func() { require.NoError(t, db.Close()) }) | ||
|
|
||
| now := time.Date(2026, time.December, 10, 17, 0, 0, 0, time.UTC) | ||
| require.NoError(t, seedDemoHistory(now)) | ||
|
|
||
| var sessions int64 | ||
| require.NoError(t, db.Instance.Model(&session.Session{}).Count(&sessions).Error) | ||
| assert.Positive(t, sessions) | ||
|
|
||
| flow, err := metrics.QueryFlow(now.Add(-24*time.Hour), now) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, flow.Flows) | ||
|
|
||
| batteries, err := metrics.QueryEnergy(now.Add(-24*time.Hour), now, "15m", false, metrics.EnergyFilter{Group: metrics.Battery}) | ||
| require.NoError(t, err) | ||
| require.Len(t, batteries, 1) | ||
| require.NotEmpty(t, batteries[0].Data) | ||
| assert.NotNil(t, batteries[0].Data[0].SocTemp) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "math" | ||
| "time" | ||
|
|
||
| "github.com/evcc-io/evcc/tariff" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // SeedDemoHistory adds synthetic meter history if the transaction has none. | ||
| func SeedDemoHistory(tx *gorm.DB, now time.Time) (bool, error) { | ||
| var count int64 | ||
| if err := tx.Model(&meter{}).Count(&count).Error; err != nil || count != 0 { | ||
| return false, err | ||
| } | ||
|
|
||
| devices := []entity{ | ||
| {Group: PV, Name: "pv", Title: "pv"}, | ||
| {Group: Home, Name: Home, Title: Home}, | ||
| {Group: Grid, Name: "grid", Title: Grid}, | ||
| {Group: Battery, Name: "battery", Title: "battery"}, | ||
| {Group: Loadpoint, Name: "lp-1", Title: "Carport"}, | ||
|
Maschga marked this conversation as resolved.
Resolved
|
||
| } | ||
| for i := range devices { | ||
| d := &devices[i] | ||
| if err := tx.Where(entity{Group: d.Group, Name: d.Name}).FirstOrCreate(d).Error; err != nil { | ||
| return false, err | ||
| } | ||
| } | ||
|
|
||
| end := now.Truncate(tariff.SlotDuration) | ||
| var history []meter | ||
| for t := dayStart(now).AddDate(0, 0, -6); t.Before(end); t = t.Add(tariff.SlotDuration) { | ||
| hour := float64(t.Hour()) + float64(t.Minute())/60 | ||
| pv := 1.8 * max(0, math.Sin(math.Pi*(hour-6)/12)) | ||
| home := 0.12 | ||
| if hour >= 18 { | ||
| home = 0.32 | ||
| } | ||
| var charge, discharge, car float64 | ||
| if hour >= 9 && hour < 12 { | ||
| charge = 0.45 | ||
| } | ||
| if hour >= 19 && hour < 23 { | ||
| discharge = 0.25 | ||
| } | ||
| if hour >= 12 && hour < 15 { | ||
| car = 0.9 | ||
| } | ||
| grid := home + charge + car - pv - discharge | ||
| for i, value := range [][2]float64{{pv, 0}, {home, 0}, {max(0, grid), max(0, -grid)}, {charge, discharge}, {car, 0}} { | ||
| slot := meter{Meter: devices[i].Id, Timestamp: t.Unix(), Energy: value[0], ReturnEnergy: value[1], Recovered: slotMeasured} | ||
| if devices[i].Group == Battery { | ||
| charged := min(1, max(0, (hour-9)/3)) | ||
| discharged := min(1, max(0, (hour-19)/4)) | ||
| slot.SocTemp = new(35 + 45*(charged-discharged)) | ||
| } | ||
| history = append(history, slot) | ||
| } | ||
| } | ||
|
|
||
| return true, tx.CreateInBatches(history, 100).Error | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.