Files
HomeAssistant/docs/plans/2026-03-19-nordpool-compact-prices-design.md
T
2026-03-19 12:08:06 +02:00

4.6 KiB

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:

{"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

[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:

[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<NordPoolDayPrice> 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<CompactNordPoolDayPrice>()
  • ResetTomorrowPricesAsync: prices = Array.Empty<CompactNordPoolDayPrice>()
  • ResetTodayTomorrowPricesAsync: prices = Array.Empty<CompactNordPoolDayPrice>()

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<CompactNordPoolDayPrice>() 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:

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]);)