fix: prevent race condition causing dashboard SOC to show wrong value during plan calculation#3909
Merged
Merged
Conversation
…o prevent race condition with web UI The web server polls self.base.soc_kw every 5 seconds to display the battery percentage. During calculate_yesterday(), soc_kw and soc_max were temporarily modified to run historical simulations, causing the web UI to transiently display wrong SOC values (e.g. yesterday's final SOC of ~50% instead of the real current 98%). Fix: Add optional soc_kw and soc_max override parameters to Prediction.__init__ so the simulations can use the correct starting values without needing to modify the base object's attributes. Agent-Logs-Url: https://github.com/springfall2008/batpred/sessions/63349b86-5d73-4473-97e2-3249c796dd33 Co-authored-by: springfall2008 <48591903+springfall2008@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix incorrect SOC reporting on dashboard
fix: prevent race condition causing dashboard SOC to show wrong value during plan calculation
May 12, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a dashboard SOC race during calculate_yesterday() by moving temporary simulation SOC values into Prediction construction instead of mutating the shared PredBat state.
Changes:
- Added optional
soc_kwandsoc_maxoverrides toPrediction.__init__. - Updated yesterday and no-battery simulations to pass SOC overrides directly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
apps/predbat/prediction.py |
Adds SOC override parameters when copying state from the base object. |
apps/predbat/output.py |
Removes direct SOC mutations during yesterday/no-battery simulations and passes overrides to Prediction. |
|
|
||
| # Simulate yesterday | ||
| self.prediction = Prediction(self, yesterday_pv_step, yesterday_pv_step, yesterday_load_step, yesterday_load_step) | ||
| self.prediction = Prediction(self, yesterday_pv_step, yesterday_pv_step, yesterday_load_step, yesterday_load_step, soc_kw=soc_yesterday) |
Comment on lines
+103
to
+104
| self.soc_kw = soc_kw if soc_kw is not None else base.soc_kw | ||
| self.soc_max = soc_max if soc_max is not None else base.soc_max |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The dashboard battery SOC header showed a stale/incorrect value (e.g. 50% instead of actual 98%) transiently after a plan calculation cycle. The plan rows themselves showed the correct SOC, only the header percentage was wrong.
Root cause
calculate_yesterday()temporarily mutatedself.soc_kwandself.soc_maxon the basePredBatobject to bootstrap historical simulations:The web server polls
self.base.soc_kwevery 5 seconds to render the battery icon. Any request landing during the several-second window ofcalculate_yesterday()would read the temporary value.soc_yesterdayis typically the battery's end-of-day level (often ~50%), which matches what users were seeing.Fix
prediction.py— added optionalsoc_kwandsoc_maxoverride parameters toPrediction.__init__. When provided, these override the values copied frombase.output.py— removed the direct mutations ofself.soc_kw/self.soc_maxincalculate_yesterday(). The desired starting SOC is now passed directly to eachPredictionconstructor:The base object's
soc_kw/soc_maxare never modified duringcalculate_yesterday(), eliminating the race window entirely.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.octopus.energy/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� --global ame it(dns block)/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� unset --global /home/REDACTED/.config/composer/vendor/bin/git user.email(dns block)/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� get --local /usr/bin/git credential.helpe/home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/bin/rustc(dns block)gitlab.com/usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED(dns block)/usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED bash -c mkcert --CAROOT(dns block)https://api.github.com/repos/springfall2008/batpred/contents/apps/predbat/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� --global ame it(http block)/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� unset --global /home/REDACTED/.config/composer/vendor/bin/git user.email(http block)/home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --quick git conf�� get --local /usr/bin/git credential.helpe/home/REDACTED/.rustup/toolchains/stable-x86_64-REDACTED-linux-gnu/bin/rustc(http block)If you need me to access, download, or install something from one of these locations, you can either: