Skip to content

ina2xx_v2: fix total energy doubling on every boot - #33

Merged
KrX3D merged 2 commits into
0_15from
fix/ina2xx-v2-energy-restore-race
Nov 18, 2026
Merged

KrX3D merged 2 commits into
0_15from
fix/ina2xx-v2-energy-restore-race

Conversation

@KrX3D

@KrX3D KrX3D commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner
  • Apply the retained MQTT restore from loop() only; the MQTT callback and loop() ran it concurrently, doubling total_energy_kWh each boot
  • Stop our own echo wiping a restore that is waiting for NTP
  • Reject implausible retained values and use double so small increments are not rounded away

Changelog

  • Energy totals could still reset to 0 on boot: the retained publish without energy fields overwrote the stored totals before the restore read them. Now publishes non-retained until restored, only a message with total_energy_kWh starts the restore, and after 30 s without one it counts from the current values.

🤖 Generated with Claude Code

The MQTT callback and loop() run on different tasks and both applied the
retained restore. Apply it from loop() only, keep a deferred restore from
being wiped by our own echo, reject implausible retained values, and use
double for the accumulators so small increments are not lost to rounding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Until the restore was done, every sensor publish was retained without the
energy fields. If one reached the broker before the retained state was read
back, it replaced the stored totals, and the restore then took the device's
own echo, found no energy values and restarted from zero (EleksTube lost
3.88 kWh this way).

- Publish non-retained until the restore is done
- Only a message containing total_energy_kWh starts the restore
- If none arrives within 30 s of MQTT connecting, count from current values

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KrX3D

KrX3D commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

localy added

@KrX3D
KrX3D merged commit 8de08db into 0_15 Nov 18, 2026
@KrX3D
KrX3D deleted the fix/ina2xx-v2-energy-restore-race branch November 18, 2026 16:31
KrX3D pushed a commit that referenced this pull request Nov 18, 2026
…tore

Brings in #33. This branch had converted ina2xx_v2.cpp from CRLF to LF,
so its diff rewrote the whole file and reverted #33. The merge keeps
0_15's line endings and re-applies only the template change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant