Tariffs: persist tariff values in 15min intervals#31845
Merged
Conversation
Adds a new tariff type that fetches hourly outdoor temperatures from
Open-Meteo (free, no API key) and exposes them as api.Rates where
Rate.Value is the temperature in °C.
The tariff fetches 7 days of past actuals + 3 days of forecast in a
single API call (past_days=7&forecast_days=3&timezone=UTC), so consumers
have access to both historical and future temperature data.
Files changed:
- tariff/temperature.go: new Temperature tariff type, registered as
'open-meteo' in the tariff registry
- api/tariff.go: TariffTypeTemperature, TariffUsageTemperature
- api/globalconfig/types.go: Temperature config.Typed in Tariffs struct
- tariff/tariffs.go: Temperature api.Tariff field + Get() case
- cmd/setup.go: wire up conf.Temperature in configureTariffs()
Configuration:
tariffs:
temperature:
type: open-meteo
latitude: 48.137
longitude: 11.576
- INFO on startup: configured lat/lon - DEBUG on each hourly fetch: slot count and time range - WARN on timestamp parse errors (was already present) - ERROR on fetch failures (was already present) - Remove stray comment at end of file
- Use 'for ; true; <-time.Tick(interval)' pattern (same as solcast.go) so the first fetch happens immediately on startup instead of after 1h - Replace (0,0) guard with proper range validation: lat in [-90,90], lon in [-180,180], allowing the valid Gulf of Guinea coordinate
- Add Temperature field to TariffRefs struct - Update configureTariff call to use new signature (conf, deviceName, target) - Move Temperature configuration to 'resolve tariff roles' section
- Delete tariff/temperature.go (custom Go implementation) - Add templates/definition/tariff/temperature.yaml (template-based) - Add special handling in tariff.go to keep 7 days of historical data for TariffTypeTemperature - Uses same pattern as other forecast tariffs with jq transformation - Simpler, more maintainable, and consistent with project conventions Addresses code review feedback: the temperature tariff can be implemented using the existing template system with yaml/jq instead of custom Go code.
The template system only supports 'price', 'solar', and 'co2' device groups. Temperature forecasting is conceptually similar to solar/CO2 forecasting (all provide forecast data for optimization), so 'solar' group is appropriate. Fixes CI/CD error: 'could not find devicegroup definition: temperature'
- Renamed file: temperature.yaml → open-meteo-temperature.yaml - Updated template name to match filename - Changed brand to 'Open-Meteo Temperature' for uniqueness Fixes CI/CD error: 'product titles must be unique' (conflict with open-meteo.yaml)
- Changed from hourly to minutely_15 (15-minute intervals) - Updated jq transformation: 3600s → 900s (15 minutes) - Provides 960 data points (10 days × 96 intervals/day) - Better alignment with evcc's 15-minute optimization slots - Updated descriptions and comments to mention 15-minute intervals This provides more accurate temperature data that matches evcc's optimization granularity.
…perature template
…ependent-household-load
…thub.com/daniel309/evcc into feature/weather-dependent-household-load
- introduce template group temperature, move Open-Meteo template - add demo temperature forecast template, enable in demo mode - config card with temperature values, range and bar chart - forecast page card; merge co2/temperature charts into ValueChart - support negative temperatures in chart, co2 capped at 0 - fix stale temperature refs on boot and device deletion - e2e: cover UI and evcc.yaml configuration
Replace temperature-as-meter logging with a dedicated tariffs table. Grid, feedin, co2 and temperature values are stored once per quarter hour at the slot boundary, driven by the site update loop.
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- PersistTariffs assumes the caller always passes a 15-minute boundary; consider validating the timestamp alignment inside the function to guard against accidental misuse.
- PersistTariffs uses multiple *float64 parameters in a fixed order; consider introducing a struct or named type to make call sites less error-prone and more self-documenting.
- The OnConflict DoNothing behavior means updated tariff values for an existing slot are silently ignored; if later corrections are expected, consider using an upsert or explicit update instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- PersistTariffs assumes the caller always passes a 15-minute boundary; consider validating the timestamp alignment inside the function to guard against accidental misuse.
- PersistTariffs uses multiple *float64 parameters in a fixed order; consider introducing a struct or named type to make call sites less error-prone and more self-documenting.
- The OnConflict DoNothing behavior means updated tariff values for an existing slot are silently ignored; if later corrections are expected, consider using an upsert or explicit update instead.
## Individual Comments
### Comment 1
<location path="core/metrics/tariffs_test.go" line_range="11" />
<code_context>
+ "github.com/stretchr/testify/require"
+)
+
+func TestPersistTariffs(t *testing.T) {
+ require.NoError(t, db.NewInstance("sqlite", ":memory:"))
+ require.NoError(t, db.Instance.AutoMigrate(new(tariffValue)))
</code_context>
<issue_to_address>
**suggestion (testing):** Extend `TestPersistTariffs` to cover additional non-nil combinations and temperature/feedin fields
To strengthen coverage, add subtests (e.g. via `t.Run`) that exercise cases where:
- `feedin` is non-nil and persisted correctly.
- `temperature` is non-nil and persisted correctly.
- Mixed combinations (e.g. `grid=nil` with non-nil `co2` and `temperature`) confirm each column is mapped and stored independently.
This ensures all `tariffs` columns are covered by tests and field-to-column mapping is fully validated.
Suggested implementation:
```golang
func TestPersistTariffs(t *testing.T) {
require.NoError(t, db.NewInstance("sqlite", ":memory:"))
require.NoError(t, db.Instance.AutoMigrate(new(tariffValue)))
t.Run("omit nil values", func(t *testing.T) {
// ensure a clean table for this case
require.NoError(t, db.Instance.Exec("DELETE FROM tariff_values").Error)
slot := time.Date(2026, 4, 15, 16, 15, 0, 0, time.UTC)
grid, co2 := 0.3, 250.0
// nil values omitted
require.NoError(t, PersistTariffs(slot, &grid, nil, &co2, nil))
var res tariffValue
require.NoError(t, db.Instance.First(&res).Error)
require.Equal(t, slot.Unix(), res.Timestamp)
// verify non-nil fields are persisted and mapped correctly
require.NotNil(t, res.Grid)
require.Equal(t, grid, *res.Grid)
require.NotNil(t, res.Co2)
require.Equal(t, co2, *res.Co2)
// verify nil fields are not persisted
require.Nil(t, res.FeedIn)
require.Nil(t, res.Temperature)
})
t.Run("persist feedin", func(t *testing.T) {
require.NoError(t, db.Instance.Exec("DELETE FROM tariff_values").Error)
slot := time.Date(2026, 4, 15, 17, 0, 0, 0, time.UTC)
grid, feedin := 0.25, 0.18
require.NoError(t, PersistTariffs(slot, &grid, &feedin, nil, nil))
var res tariffValue
require.NoError(t, db.Instance.First(&res).Error)
require.Equal(t, slot.Unix(), res.Timestamp)
require.NotNil(t, res.Grid)
require.Equal(t, grid, *res.Grid)
require.NotNil(t, res.FeedIn)
require.Equal(t, feedin, *res.FeedIn)
require.Nil(t, res.Co2)
require.Nil(t, res.Temperature)
})
t.Run("persist temperature", func(t *testing.T) {
require.NoError(t, db.Instance.Exec("DELETE FROM tariff_values").Error)
slot := time.Date(2026, 4, 15, 17, 30, 0, 0, time.UTC)
co2, temperature := 260.0, 22.5
require.NoError(t, PersistTariffs(slot, nil, nil, &co2, &temperature))
var res tariffValue
require.NoError(t, db.Instance.First(&res).Error)
require.Equal(t, slot.Unix(), res.Timestamp)
require.Nil(t, res.Grid)
require.Nil(t, res.FeedIn)
require.NotNil(t, res.Co2)
require.Equal(t, co2, *res.Co2)
require.NotNil(t, res.Temperature)
require.Equal(t, temperature, *res.Temperature)
})
t.Run("mixed combinations", func(t *testing.T) {
require.NoError(t, db.Instance.Exec("DELETE FROM tariff_values").Error)
slot := time.Date(2026, 4, 15, 18, 0, 0, 0, time.UTC)
co2, temperature, feedin := 275.0, 19.3, 0.21
// grid=nil; co2 + temperature + feedin non-nil
require.NoError(t, PersistTariffs(slot, nil, &feedin, &co2, &temperature))
var res tariffValue
require.NoError(t, db.Instance.First(&res).Error)
require.Equal(t, slot.Unix(), res.Timestamp)
// each column mapped and stored independently
require.Nil(t, res.Grid)
require.NotNil(t, res.FeedIn)
require.Equal(t, feedin, *res.FeedIn)
require.NotNil(t, res.Co2)
require.Equal(t, co2, *res.Co2)
require.NotNil(t, res.Temperature)
require.Equal(t, temperature, *res.Temperature)
})
```
These edits assume the following:
- `tariffValue` has pointer fields `Grid`, `FeedIn`, `Co2`, and `Temperature` of type `*float64`.
- The underlying table name is `tariff_values` (used in the `DELETE` statements).
If the struct uses different field names or types (e.g. non-pointer floats) or a different table name, you should:
1. Adjust the assertions to match the actual field names and types (e.g. drop `NotNil`/`Nil` checks for non-pointer fields and use direct equality).
2. Update the `DELETE FROM tariff_values` SQL to use the correct table name for `tariffValue`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
4 tasks
andig
marked this pull request as draft
July 17, 2026 09:45
andig
marked this pull request as ready for review
July 17, 2026 12:43
naltatis
changed the base branch from
feature/weather-dependent-household-load
to
master
July 17, 2026 12:52
naltatis
enabled auto-merge (squash)
July 17, 2026 12:57
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
builts upon #27780
Sketch for the dedicated tariff log schema discussed in the PR review. Instead of logging outside temperature like a regular meter, tariff values get their own lean table.
tariffstable: one row per quarter hour withts,grid,feedin,co2andtemperaturecolumns, unconfigured tariffs stay NULLtariff.At, so the stored timestamp is always x:00/x:15/x:30/x:45soc_tempcollector path), UI publishing is unchanged