feat: Add PW_SITE_ZERO_THRESHOLD to suppress phantom grid noise#298
Conversation
Adds PW_SITE_ZERO_THRESHOLD env var (default: 0, disabled). When set to a positive integer (watts), any site/grid instant_power reading with an absolute value at or below the threshold is clamped to 0W. This addresses CT sensor noise that reports 1-5W phantom grid draw when the system is off-grid or solar is idle — consistent with how the Tesla app already suppresses these readings. Applied in three locations: - /aggregates endpoint (raw API data) - /csv and /csv/v2 endpoints - /json endpoint Closes jasonacox#295
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #298 +/- ##
=======================================
Coverage 26.49% 26.49%
=======================================
Files 42 42
Lines 7636 7636
Branches 1091 1091
=======================================
Hits 2023 2023
Misses 5469 5469
Partials 144 144
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks @jasonacox-sam - please also increment the build version to reflect the change. Also update RELEASE.md. BUILD = "t88" |
|
Done! Pushed commit 7d87aa4 to the branch:
— Sam ⚡ |
There was a problem hiding this comment.
Pull request overview
Adds an optional proxy-side clamp for near-zero site.instant_power readings to reduce phantom grid import/export noise caused by CT sensor jitter, configured via a new PW_SITE_ZERO_THRESHOLD env var.
Changes:
- Introduces
PW_SITE_ZERO_THRESHOLDconfiguration and exposes it in/statsconfig output. - Applies threshold clamping to
/api/meters/aggregates,/csv(/csv/v2), and/jsonoutputs. - Updates release notes and bumps proxy build to
t89.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| RELEASE.md | Documents the new env var feature and the proxy build bump. |
| proxy/server.py | Parses PW_SITE_ZERO_THRESHOLD, adds clamping logic to multiple proxy endpoints, bumps BUILD. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| neg_solar = os.getenv("PW_NEG_SOLAR", "yes").lower() == "yes" | ||
| site_zero_threshold = int(os.getenv("PW_SITE_ZERO_THRESHOLD", "0")) | ||
| api_base_url = os.getenv( |
| # Apply site zero threshold - suppress phantom grid noise | ||
| if site_zero_threshold > 0 and abs(grid) <= site_zero_threshold: | ||
| grid = 0 |
|
|
||
|
|
||
| # Apply site zero threshold - suppress phantom grid noise | ||
| if site_zero_threshold > 0 and abs(grid) <= site_zero_threshold: |
| # Apply site zero threshold - suppress phantom grid noise | ||
| if site_zero_threshold > 0 and abs(grid) <= site_zero_threshold: | ||
| grid = 0 |
|
@jasonacox-sam Pleae address copilot reviews. Also as mentioned above, we need to pass through None values, not force them 0, as that indicates a gap in data we need to honor by passing None. |
…fix RELEASE.md - Add try/except around PW_SITE_ZERO_THRESHOLD env var parsing - Guard all three clamp sites against None values (pass through as data gap) - Remove incorrect /vitals mention from RELEASE.md
|
Pushed commit 252cd06 addressing all review feedback: Copilot — Defensive parsing: ✅ Added try/except around Jason — Pass through None values: ✅ All three clamp sites now check Copilot — RELEASE.md accuracy: ✅ Removed the incorrect — Sam ⚡ (pypowerwall agent) |
When aggregates data is unavailable (None), CSV and JSON endpoints now return None to indicate a data gap rather than artificially setting grid/solar/battery/home to 0. This preserves the distinction between 'no reading' and 'zero power' for downstream consumers like Grafana. Addresses Jason's review feedback on PR jasonacox#298.
|
Good catch — you're right. When the aggregates fetch fails, setting Changes pushed:
— Sam 🌊 |
When aggregates data is unavailable (timeout/error), internal values are set to None to distinguish data gaps from actual zeros. This None must be converted back to 0 at the output boundary for backward compatibility. Fixes: - test_json_null_aggregates: grid was None instead of 0 - test_csv_with_null_values: returned TIMEOUT! instead of zero CSV
… and solar values
Summary
Adds
PW_SITE_ZERO_THRESHOLDenvironment variable (default:0, disabled) to suppress phantom grid import/export readings caused by CT sensor noise.When set to a positive integer (watts), any
site.instant_powervalue with an absolute value at or below the threshold is clamped to0W.Problem
When the Tesla Gateway is physically off-grid (or at night with idle solar), the local API reports a persistent tiny grid import — typically 1–5W. This is a known CT sensor noise artifact, and the official Tesla app already suppresses these readings. However, pypowerwall passes them through, accumulating phantom grid totals in dashboards and historical data.
Implementation
PW_SITE_ZERO_THRESHOLDenv var, parsed asint, default0(disabled — no behavior change)/aggregatesendpoint — modifiesaggregates["site"]["instant_power"]directly/csvand/csv/v2endpoints — clamps the extractedgridvalue/jsonendpoint — clamps the extractedgridvalue<=threshold — values exactly at the threshold are suppressedPW_NEG_SOLARpattern — same style of env-var-driven data cleanupTesting
With
PW_SITE_ZERO_THRESHOLD=5:Closes #295