Skip to content

fix(battery): read battery_temperature_history per inverter in the rate grid scan - #6222

Open
tico24 wants to merge 1 commit into
springfall2008:mainfrom
tico24:fix/rate-grid-temperature-index
Open

tico24 wants to merge 1 commit into
springfall2008:mainfrom
tico24:fix/rate-grid-temperature-index

Conversation

@tico24

@tico24 tico24 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

What and why

Since v9.8.0 the rate grid scan reads battery_temperature_history with no index. If apps.yaml gives it as a per-inverter list, for example battery_temperature_history: [sensor.modbus_bms_cell_temp_low], the whole list is passed to get_history and every plan run fails:

TypeError: cannot use 'list' as a dict key (unhashable type: 'list')
  fetch.py battery_rate_grid_update
  battery_curve.py find_rate_grid_samples
  battery_curve.py load_battery_temperature_minutes
  predbat.py get_history_wrapper
  ha.py get_history

I hit this on my own system with the list form above. load_battery_temperature_minutes now passes index=self.id, as the grid_power lookup beside it does. A single sensor string still works: inverter 0 reads it with no warning.

Other get_arg calls checked

Every other get_arg in battery_curve.py already passes an index. battery_rate_grid.py and the rate grid code in fetch.py and execute.py have none to fix. Two reads of the same key are left alone on purpose:

  • fetch_battery_temperature() hands the key to minute_data_import_export, which takes a string or a list and reads every entity.
  • check_battery_temperature_history_stuck() only uses the value in a warning, and it checks for a string before it matches the GivTCP name. With the list form the warning shows the list and leaves out the rename hint, but it doesn't crash.

Testing

  • Test 9 in test_battery_rate_grid_scan.py sets the list form and scans a 25-minute charge. It expects three segments at the sensor's temperature. On main it raises TypeError: unhashable type: 'list', from the test's mock history where the real get_history would raise. With this change it passes.
  • battery_rate_grid_scan, battery_rate_grid, battery_rate_grid_update, battery_curve_keys, battery_temperature_fetch and the three battery_temperature_stuck_warning* tests pass. pre-commit passes on the changed files. I haven't run the full suite.

…te grid scan

load_battery_temperature_minutes read battery_temperature_history with no index. With the per-inverter list form in apps.yaml the whole list reached get_history, and every plan run failed with "TypeError: cannot use 'list' as a dict key (unhashable type: 'list')". It now passes index=self.id, as the grid_power lookup next to it does.

test_battery_rate_grid_scan gets a case with the list form. It fails on main and passes with this change.
@tico24
tico24 marked this pull request as ready for review November 16, 2026 06:08
@github-actions github-actions Bot added the BOT_REVIEW Trigger an autotriage label Nov 16, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated comment from the triage bot.

Reviewed f60c1638: 0 findings inline, 1 in the body.

The fix is right for the reported crash. resolve_arg() takes value[index] before any template, join-list or entity resolution, so a per-inverter list now resolves to this inverter's sensor, and it does so for the {...}-templated and +[...] forms of a list entry too. The string form is unchanged: the indexed-read warning in resolve_arg() only fires when the index is truthy, so inverter 0's read of a plain string still returns it silently, and battery_temperature_history is not in CONFIG_ITEMS so get_ha_config() cannot return a value that bypasses resolve_arg(). Production only ever scans inverter 0 (execute.py passes self.inverters[0] to battery_rate_grid_update()), so self.id is 0 on every real call. The other readers of the key (fetch_battery_temperature(), check_battery_temperature_history_stuck()) never pass it to get_history_wrapper() themselves, so none is left crashing. Test 9 is genuinely red without the change: the unindexed read returns the list from resolve_arg(), get_history_wrapper() hands it to the mock get_history(), and the dict membership test raises TypeError: unhashable type: 'list'.

apps/predbat/fetch.py:2855 — a multi-entry list is summed into one temperature

The PR body's "Other get_arg calls checked" section accepts fetch_battery_temperature() because minute_data_import_export() "takes a string or a list and reads every entity". It does read every entity, but it adds them together: it passes the running series as accumulate= to minute_data(), which does mdata[minute] += accumulate.get(minute, 0) for each entity in turn.

Failure scenario: two inverters with battery_temperature_history: [sensor.inv0_temp, sensor.inv1_temp]. minute_data_import_export() is called with no required_unit and scale=1.0, so the raw degrees are added: two packs at 15 °C and 16 °C give a battery_temperature_history of 31 °C for the whole window. predict_battery_temperature() then produces a battery_temperature_prediction of ~2× the real temperature across the 48-hour horizon, and prediction.py picks the plan's rate-grid row and manual temperature cap from it — a warmer row and a looser cap than the packs allow, which is the over-permissive cold-weather plan that the 0 °C work (#6193, #6189) set out to stop. It also disagrees with the curve scan this PR fixes, which now uses inverter 0's sensor alone as the evidence's temperature dimension.

This is not a regression from the PR — the same config crashed the scan every cycle before it — but the change turns a loud failure into a silently wrong temperature once the list has more than one entry, which is the next symptom of the list form being supported. Fixing it in the same shape as the line this PR changes, reading one index in fetch_battery_temperature() too (the key is site-wide in the docs, discovery_contract's SITE_SETTINGS, and every integration's auto-config), would also make the evidence and the plan's lookup agree.

What I could not confirm: whether a multi-entry list is a config users actually write. The docs and the shipped example give one sensor, and the reporter's own list has one entry, so this may never be hit; if so, a line in docs/apps-yaml.md saying the setting is one sensor would close it from the other side.

@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Nov 16, 2026

This branch has not been deployed

No deployments
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.

2 participants