Skip to content

Fix asset status page crash on old sensors_to_show format#2103

Merged
Flix6x merged 13 commits intomainfrom
copilot/fix-asset-status-page-crash
Apr 13, 2026
Merged

Fix asset status page crash on old sensors_to_show format#2103
Flix6x merged 13 commits intomainfrom
copilot/fix-asset-status-page-crash

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

The asset status page crashed with KeyError: 'sensors' for assets whose sensors_to_show contained the old singular-key format (e.g. {"title": "Prices", "sensor": 709}).

Root cause

get_asset_sensors_metadata() accessed sensor["sensors"] directly on items returned by validate_sensors_to_show(). That method normalises all input formats into a "plots"-based structure:

{"title": "Prices", "plots": [{"sensor": 709}]}

so the top-level "sensors" key never exists, causing the crash on any sensors_to_show entry.

Changes

  • flexmeasures/data/services/sensors.py — replace the broken direct key access with SensorsToShowSchema.flatten(), which already handles every sensors_to_show variant (singular "sensor", plural "sensors", and "plots"-wrapped structures). Add SensorsToShowSchema import at module level.

  • flexmeasures/api/common/schemas/tests/test_sensor_data_schema.py — add regression test test_asset_sensors_metadata_old_sensors_to_show_format covering the exact mixed format from the bug report:

    asset.sensors_to_show = [
        {"title": "Group", "sensors": [wind_sensor.id, temperature_sensor.id]},
        {"title": "Solo", "sensor": wind_sensor.id},  # old singular format
    ]

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community bot commented Apr 13, 2026

Documentation build overview

📚 flexmeasures | 🛠️ Build #32242981 | 📁 Comparing af58c6d against latest (91f16f1)

  🔍 Preview build  

Show files changed (5 files in total): 📝 4 modified | ➕ 0 added | ➖ 1 deleted
File Status
changelog.html 📝 modified
index.html 📝 modified
api/v3_0.html 📝 modified
dev/scripting.html ➖ deleted
host/installation.html 📝 modified

Copilot AI changed the title [WIP] Fix asset status page crash on old sensors_to_show format Fix asset status page crash on old sensors_to_show format Apr 13, 2026
Copilot AI requested a review from Flix6x April 13, 2026 14:08
@Flix6x Flix6x marked this pull request as ready for review April 13, 2026 14:11
Copy link
Copy Markdown
Contributor

@Flix6x Flix6x left a comment

Choose a reason for hiding this comment

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

Seems like a reasonable fix, but the pipeline is failing.

@Flix6x Flix6x added this to the 0.32.0 milestone Apr 13, 2026
@Flix6x Flix6x added bug Something isn't working UI labels Apr 13, 2026
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 13, 2026

Seems like a reasonable fix, but the pipeline is failing.

Fixed in commit 6980c7a. The test was failing because wind_sensor.id and temperature_sensor.id were None when the add_weather_sensors module-scoped fixture hadn't been flushed yet (IDs are only assigned after a DB flush). Added an explicit db.session.flush() before reading the IDs, and a finally block to reset sensors_to_show = [] so the test doesn't pollute the shared fixture state for the subsequent test_asset_sensors_metadata test.

Copilot AI requested a review from Flix6x April 13, 2026 18:04
Flix6x and others added 9 commits April 13, 2026 20:43
…ors_to_show after test"

This reverts commit 6980c7a.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ors_to_show after test"

This reverts commit 2b53bf4.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x merged commit 7dee659 into main Apr 13, 2026
12 of 13 checks passed
@Flix6x Flix6x deleted the copilot/fix-asset-status-page-crash branch April 13, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: asset status page crashed on old sensors_to_show format.

2 participants