From b07cb970462d73084aa82521c6f940221d895fb2 Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 19 Mar 2026 12:08:06 +0200 Subject: [PATCH] Add design doc for NordPool compact price attributes Made-with: Cursor --- ...26-03-19-nordpool-compact-prices-design.md | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 docs/plans/2026-03-19-nordpool-compact-prices-design.md diff --git a/docs/plans/2026-03-19-nordpool-compact-prices-design.md b/docs/plans/2026-03-19-nordpool-compact-prices-design.md new file mode 100644 index 0000000..d4f1b5a --- /dev/null +++ b/docs/plans/2026-03-19-nordpool-compact-prices-design.md @@ -0,0 +1,112 @@ +# NordPool Compact Price Attributes Design + +**Date:** 2026-03-19 +**Status:** Approved +**Issue:** `sensor.nordpool_today_tomorrow_prices` state attributes exceed HA's 16,384-byte limit, causing database performance warnings and attributes not being stored. + +## Problem + +NordPool provides electricity prices in 15-minute intervals (96 per day). The `sensor.nordpool_today_tomorrow_prices` entity merges today's and tomorrow's prices into a single attribute list of up to 192 entries. Each `NordPoolDayPrice` serializes as: + +```json +{"start":"2026-03-19T00:00:00","end":"2026-03-19T00:15:00","price":12.35} +``` + +At ~78 bytes per entry, 192 entries produce ~15,000+ bytes — exceeding the 16,384-byte HA recorder limit. + +## Constraints + +- Full 15-minute granularity must be preserved (boiler scheduling depends on it). +- ApexCharts Lovelace card reads `attributes.prices` for dashboard visualization. +- Price history should be stored in the HA database (recorder). +- Internal code (boiler control, cheapest intervals) uses `NordPoolDayPrice` with `DateTime` fields. + +## Solution: Ultra-Compact Array Serialization + +Convert prices to a compact `[unix_start, unix_end, price]` array format at the HA attribute serialization boundary only. Internal code continues using `NordPoolDayPrice`. + +### Compact Format + +```json +[1742342400, 1742343300, 12.35] +``` + +~30 bytes per entry. 192 entries = ~5,760 bytes + attribute wrapper = **~6,000 bytes** (well under 16,384). + +### Compact Price Model + +A new `CompactNordPoolDayPrice` readonly struct in `NordPool.Models`: + +```csharp +[JsonConverter(typeof(CompactNordPoolDayPriceConverter))] +public readonly struct CompactNordPoolDayPrice +{ + public long Start { get; init; } // Unix epoch seconds + public long End { get; init; } // Unix epoch seconds + public double? Price { get; init; } + + public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice) => ... +} +``` + +- Custom `JsonConverter` serializes as `[start, end, price]` array. +- Extension method `ToCompact()` on `IEnumerable` for batch conversion. + +### Affected Entities + +| Entity | Current Size (approx) | Compact Size (approx) | +|--------|----------------------|----------------------| +| `sensor.nordpool_today_prices` | ~8,500 bytes | ~3,300 bytes | +| `sensor.nordpool_tomorrow_prices` | ~8,500 bytes | ~3,300 bytes | +| `sensor.nordpool_today_tomorrow_prices` | ~16,500 bytes | ~6,000 bytes | + +### Code Changes + +**NordPoolEntityService** — 6 methods updated (3 update + 3 reset): + +- `UpdateTodayPricesAsync`: `prices = ctx.TodayNordPoolPrice?.Prices.ToCompact()` +- `UpdateTomorrowPricesAsync`: `prices = ctx.TomorrowNordPoolPrice?.Prices.ToCompact()` +- `UpdateTodayTomorrowPricesAsync`: `prices = mergedPrices.ToCompact()` +- `ResetTodayPricesAsync`: `prices = Array.Empty()` +- `ResetTomorrowPricesAsync`: `prices = Array.Empty()` +- `ResetTodayTomorrowPricesAsync`: `prices = Array.Empty()` + +**No changes to:** + +- `NordPoolDayPrice` (preserved for internal use) +- `NordPoolPriceService` (fetching, caching, current price) +- `CheapestIntervalsFinder` (boiler scheduling) +- `BoilerControlService` (boiler switching) +- `PowerMonitor` (reads `sensor.nordpool_current_price`, not the price lists) + +### Edge Cases + +1. **Null DateTime**: `FromDayPrice` uses `0L` (Unix epoch) as sentinel for null Start/End. +2. **Null price**: Serialized as `[start, end, null]`. +3. **Empty lists**: `Array.Empty()` serializes to `[]`. +4. **Future granularity changes**: A `MaxAttributePriceEntries` constant with warning log if exceeded. No silent truncation. + +### Testing + +1. **`CompactNordPoolDayPriceTests`**: + - Conversion correctness (DateTime to epoch, price preserved) + - Null handling (null Start, End, Price) + - `ToCompact()` extension on collections + - JSON serialization produces `[s, e, p]` array + - JSON round-trip deserialization + - **Size regression test**: 192 entries serialized < 16,384 bytes + +2. **`NordPoolEntityServiceTests`** (updated): + - Verify `SetEntityAttributes` receives `CompactNordPoolDayPrice` arrays + - Realistic 192-entry integration test asserting JSON size < 16,384 bytes + +### Dashboard Impact + +ApexCharts card configuration needs updating to parse the new `[start, end, price]` array format instead of `{start, end, price}` objects. This is a HA dashboard YAML change. Example `data_generator` adjustment: + +```yaml +data_generator: | + return entity.attributes.prices.map(p => [p[0] * 1000, p[2]]); +``` + +(Previously: `return entity.attributes.prices.map(p => [new Date(p.start).getTime(), p.price]);`)