Skip to content

Pad HA price arrays with null to clear stale future-price sensors (#1188)#1222

Open
gskjold wants to merge 1 commit into
mainfrom
fix/1188-stale-ha-price-sensors
Open

Pad HA price arrays with null to clear stale future-price sensors (#1188)#1222
gskjold wants to merge 1 commit into
mainfrom
fix/1188-stale-ha-price-sensors

Conversation

@gskjold

@gskjold gskjold commented Jun 25, 2026

Copy link
Copy Markdown
Member

Fixes #1188

Problem

Home Assistant payload type reports wrong future prices: after midnight, the per-hour price sensors freeze on the last known value and repeat it for the rest of the (now expired) window — what the reporter saw as "the last point before midnight reused" for ~22 hours.

Root cause

The /prices JSON itself is correct — it only carries the real future points. The issue is the HA discovery sensors:

  • publishPriceSensors() creates one sensor per future hour (prices.import[N]), and the count only ever grows (priceImportInit / priceExportInit are high-water marks). Once tomorrow's prices are published around 13:00, ~34 sensors exist permanently.
  • After midnight the array shrinks back to the points remaining until next midnight. Sensors for the now-missing tail index past the end of the array.
  • Jinja raises IndexError on out-of-range list access, so the value template fails to render and HA keeps the sensor's previous state — the stale repeated price.

Fix

Pad the import/export arrays in publishPrices() with null up to the discovery high-water mark, so every existing sensor's index resolves. Out-of-data hours render null | is_definedNone, showing as unknown in HA instead of a stale value. The raw JSON is also more honest for non-HA consumers (trailing nulls rather than a silently shrinking array).

Pure HomeAssistant-handler change; no decoder impact.

Verification

  • pio run -e esp32dev builds clean.
  • Buffer is safe: ≤38 entries × ~8 chars per array, well under the 2048 BufferSize.
  • Existing trailing-comma trims (pos--) still hold since padding always leaves a trailing ,.
  • Device-level confirmation: after tomorrow's prices expire at midnight, the "Import price in N hours" sensors beyond the available window should report unknown rather than the last known price.

🤖 Generated with Claude Code

)

Home Assistant price sensors (prices.import[N]) are created once and never
removed, so the count only grows to the high-water mark (~34 future hours
once tomorrow's prices are published). After midnight the price array
shrinks back to the points remaining until next midnight, leaving the
trailing sensors indexing past the end of the array. Jinja raises an
IndexError on out-of-range list access, so HA fails to render those
templates and freezes each sensor on its last value -- the "last point
before midnight reused" reported in #1188.

Pad the import/export JSON arrays with null up to priceImportInit /
priceExportInit (the discovery high-water mark) so every existing sensor's
index resolves. Out-of-data hours now render `null | is_defined` -> None,
showing as unknown in HA instead of a stale repeated price.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: 4ed22c7

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong future prices

1 participant