Skip to content

Off-grid shadow scan guard - #48

Merged
piomar123 merged 2 commits into
mainfrom
off-grid-shadow-scan
Oct 4, 2026
Merged

piomar123 merged 2 commits into
mainfrom
off-grid-shadow-scan

Conversation

@piomar123

@piomar123 piomar123 commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

What

Shadow scan (the PV global MPPT scan) misbehaves while the inverter is off-grid. With OFF_GRID_SHADOW_SCAN_GUARD=on:

  • Going off-grid: the current shadow_scan value is read and remembered, then 0 is written. If it was already 0, nothing is written.
  • Grid back: once the grid has stayed up for OFF_GRID_SHADOW_SCAN_COOLDOWN_MIN (default 15), the remembered value is written back. A new drop during the cooldown restarts it.
  • Off by default: nothing changes unless the env key is set. It's independent of CONTROL_MODE, so it works with battery control off.

Why a 15 min cooldown

In the 1-year history, outages come in bursts. On 2025-09-23 the grid dropped again 123–904 s after returning, and on 2026-05-05 after 182 s. The battery control's 60 s off-grid release is fine for currents, but would toggle shadow scan on every flap.

How

  • shadow_scan_guard.py: a small state machine stepped from the 1 Hz poll loop with the current goodwe object, so reconnects are handled.
    • Every write is verified by read-back.
    • A register that acks but never takes the value gets 3 attempts, then a back-off from 5 min doubling up to 1 h.
    • Once the inverter holds the wanted value, the guard stops reading the register.
    • It never raises into the poll loop.
  • shadow_scan_guard.json (git-ignored): saves the pending restore, so a restart mid-outage still puts the value back. After a restart the cooldown starts over.
  • control.is_off_grid(): off-grid detection moved out of Sample.from_runtime, so the guard and the battery control use the same rule.
  • Test isolation: tests/__init__.py blanks the new key, and the env-isolation test covers it, so a test run in the Pi's live checkout can't touch the real inverter.

Test plan

  • python -m unittest discover -s tests: 584 pass, 20 of them new in tests/test_shadow_scan_guard.py. They cover:
    • config parsing;
    • turning it off once;
    • restoring only after the cooldown, and the cooldown restarting on a flapping grid;
    • leaving it off when it was already off;
    • the next outage picking up a value changed in between;
    • restarts mid-outage and mid-cooldown;
    • loading the state on the first step rather than at import, before logging is set up;
    • an unreadable state file;
    • read and write failures;
    • a register that never takes the value;
    • an inverter reconnect.
  • On the Pi: set OFF_GRID_SHADOW_SCAN_GUARD=on and restart. Check the next outage in the log (Off-grid: holding shadow scan off … shadow scan back to 1).

🤖 Generated with Claude Code

piomar123 and others added 2 commits October 4, 2026 10:07
Shadow scan misbehaves while the inverter is off-grid. With
OFF_GRID_SHADOW_SCAN_GUARD=on it is turned off as soon as the inverter
goes off-grid, and the previous value is put back once the grid has
stayed up for OFF_GRID_SHADOW_SCAN_COOLDOWN_MIN (default 15): outages
come in bursts, and in the history the grid dropped again up to ~15 min
after returning. Independent of CONTROL_MODE.

The pending restore is persisted in shadow_scan_guard.json so a restart
mid-outage still restores it. Writes are verified by read-back, retried,
and back off when the register never takes the value; the guard never
raises into the poll loop. Off-grid detection moved to
control.is_off_grid() so both features use the same rules. Tests blank
the new env key like the other production-only ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main.py builds the guard at import, before logging is configured, so the
state load's "resumed" line was lost. Load it on the first step instead,
like ControlRuntime does in attach(). "turning shadow scan off" was
logged even when it was already off; say "holding" instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piomar123
piomar123 merged commit 2892397 into main Oct 4, 2026
1 check passed
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