Quick overview:
goodwe-manager.mp4
Needs Python 3.12 or newer (the pinned numpy and matplotlib don't install on older ones). Tested only on a GW8KN-ET (with a Pylontech Force H2), connected locally over UDP.
- Create venv
python3 -m venv venvand activate it. venv/bin/activate - Install dependencies
pip install -r requirements.txt - Copy
.env.exampleto.envand fill in the values (all configuration lives there) - Run with
python main.py(TODO: instructions for gunicorn) - Run with
--dry-runparameter to disable communication with the inverter (used for testing)
The PV forecast uses Solcast with two sites (SOLCAST_SITE_EAST_ID,
SOLCAST_SITE_WEST_ID) and assumes an east-west array, see the caveats at
the end of this file.
On the author's Raspberry Pi it runs as a systemd user service. Save as
~/.config/systemd/user/goodwe_manager.service (adjust the paths):
[Unit]
Description=Goodwe PV inverter manager
[Service]
WorkingDirectory=/home/pi/goodwe_manager/
ExecStart=/home/pi/goodwe_manager/venv/bin/python main.py
Restart=on-failure
[Install]
WantedBy=default.targetthen systemctl --user daemon-reload && systemctl --user enable --now goodwe_manager
and sudo loginctl enable-linger $USER, so it keeps running without a login session.
Logs: journalctl --user -u goodwe_manager -f.
Pulling a checkout that's behind may require a one-off migration step - see
CHANGELOG.md for what changed and why. Most notably: if
you have old data-*.csv files from before the switch to SQLite, run
python _migrate_csv_to_sqlite.py once to import them into data.db
(safe to re-run; already-migrated files are skipped).
_calculate_income.py and the MQTT bridge (see PR #35) can price
imported energy using a real time-of-use tariff schedule
instead of a flat rate. Copy an example from tariff_examples/ (e.g.
g12w_pge.yaml) to tariff_import.yaml (already gitignored), fill in
your actual rates, and set TARIFF_IMPORT_CONFIG=tariff_import.yaml in
.env. Without this set, both keep using the flat IMPORT_PRICE_KWH
constant, unchanged from today's behavior. See TARIFF_SCHEMA.md for
the full YAML schema.
/stats (the 💰 Savings button) shows, per day, month and year, what
the grid operator settles and what PV and the battery saved:
- Net import / net export: the meter's import and export offset within each hour, as the operator's meter does. Net import is priced at the import tariff; net export is the prosument deposit credit at the hourly average RCE (x1.23 from 2025-02-01, 0 for a negative hour).
- Without PV: the house load bought from the grid at each hour's
import price, less
PV_EXTRA_LOAD_FACTOR(default 0.10) of the PV used in the house - load that exists only because PV made it cheap. - Savings = without PV - net import cost + deposit credit. Grid energy stored at a cheap hour and used at an expensive one counts as savings.
- Load shift: grid energy charged into the battery (net charge the PV
surplus didn't cover), how much of it the house used later and the profit
(value at the import price of the hour of use - its cost when charged;
already part of Savings). The battery's grid part loses 10 % round trip, is
used before stored PV (Predbat's
pv_originrule), carries over between days and is capped at the energy in the battery (BATTERY_CAPACITY_KWH, default 7.1, x the hour's max SoC). - A cumulative savings chart against
INVESTMENT_NET_PLN(net of subsidies and tax relief) with a payback date projected from the last 12 months./stats/daily.csvdownloads every day.
The data is the inverter's grid meter, so expect a few % difference from
the bill; fixed monthly fees aren't included. Rows live in data.db's
daily_settlement table; today's and yesterday's are recomputed after
every new hourly_summary hour.
Import prices that changed over time go in a tariff history
(TARIFF_HISTORY_CONFIG, see tariff_examples/tariff_history.yaml): each
period is a flat price or a tariff YAML as above. Without it,
TARIFF_IMPORT_CONFIG prices every day. Fill past days once with
python _backfill_daily_settlement.py (it fetches missing RCE days from
PSE into rce_prices.db, ~1 s per day the first time).
Set MQTT_HOST (and MQTT_USERNAME/MQTT_PASSWORD if your broker
requires auth) in .env to publish live telemetry, daily energy
counters, RCE export prices, tariff-based import prices (if
TARIFF_IMPORT_CONFIG is also set), and the combined PV forecast to your
MQTT broker. Unset MQTT_HOST (the default) disables this entirely - no
behavior change from before this feature existed. See MQTT_TOPICS.md
for the full topic/payload list and Home Assistant/Predbat setup.
The daily-reset energy counters (e_day_exp, e_day_imp, e_load_day,
e_bat_charge_day, e_bat_discharge_day) are published live but
deliberately not written to data.db - they reset every midnight,
so a lifetime of DB rows for them adds nothing that the lifetime
counters (e_total, etc., which are persisted) don't already give
historical analysis. See sensors.py's DB_SENSORS.
RCE export prices publish at 15-minute granularity by default (the
market's own settlement period). Set RCE_EXPORT_GRANULARITY=hourly to
average each hour's four quarters first instead, matching how PGE
settles prosument export credit on an hourly basis.
A negative RCE price publishes as 0.0 by default (RCE_EXPORT_NEGATIVE_PRICES=zero),
matching net-billing paying nothing for it. Set RCE_EXPORT_NEGATIVE_PRICES=raw
to publish the true negative value instead - note the 23% VAT bonus is
never applied to a negative price either way, since the law doesn't
define a bonus on a loss.
The bridge publishes prices in zł/kWh, but Predbat works in the minor
unit: rate lists with start/end/value keys (the shape of
prices/import and prices/export) are multiplied by 100 when Predbat
reads them (fetch_octopus_rates in its octopus.py), so its plan,
costs and the absolute threshold settings are all in grosze, like
pence in its own docs. Set currency_symbols to zł / gr.
The defaults are therefore already on a sensible scale and there is no need to shrink them:
metric_min_improvement(default0.0) - minimum cost improvement worth lowering the battery SoC for.metric_min_improvement_export(default0.1) - minimum cost improvement worth a forced export for.metric_min_improvement_export_freeze(default0.1) - minimum cost improvement worth freezing an export for.metric_min_improvement_plan(default2.0) - minimum cost improvement worth replacing the current plan for. Raised to5(gr) on the author's installation: plans flipping back and forth over 2-5 gr differences weren't worth the inverter writes.
_estimate_battery_efficiency.py is a one-off, manually-run offline
analysis (not part of the live app - see "Documentation conventions"
below on the _-prefix convention) that estimates Predbat's
battery_loss/battery_loss_discharge/inverter_loss config values
from this household's actual inverter_history, instead of guessing at
generic li-ion defaults. Run it against data.db (or a bounded extract
of it - see --db-path) and read the printed report; nothing writes to
Predbat's config automatically. See GOODWE_SENSOR_NOTES.md for the
methodology and the sensor-level quirks (BMS self-consumption offset,
grid phase sign disagreement, cross-register poll timing skew) it
depends on and works around.
With the MQTT bridge enabled, CONTROL_MODE lets an optimizer drive the
battery through MQTT commands (control/set, see MQTT_TOPICS.md for the
payloads, the Home Assistant script and the Predbat apps.yaml example).
off (default) changes nothing; shadow computes and publishes what it
would do in control/state but never writes; on writes to the inverter.
CONTROL_CHARGE_CURRENT_A, CONTROL_DISCHARGE_CURRENT_A and
CONTROL_MIN_SOC are your normal battery current limits and on-grid
minimum SoC - required when not off, and restored whenever the battery
isn't frozen.
| Predbat status | Mode | Inverter settings written | What the inverter does (measured on a GW8KN-ET) |
|---|---|---|---|
| Demand, Hold exporting | auto |
EMS 1 (Auto), normal charge/discharge currents and on-grid minimum SoC | normal self-use |
| Charging | charge P |
EMS 11 (Charge Battery), EMS power limit P | battery charges at exactly P W; shortfall from the grid |
| Exporting | export P |
EMS 3 (Discharge PV), EMS power limit P | battery discharges at exactly P W; PV not curtailed, the rest is exported |
| Freeze charging | freeze_charge |
EMS 1 (Auto), on-grid minimum SoC raised to the current SoC | no discharge; surplus PV still charges |
| Hold charging | freeze_charge with target_soc below the SoC |
as auto until the SoC is down to the target, then as Freeze charging |
discharges to the target, then holds it |
| Freeze exporting | freeze_export |
EMS 1 (Auto), charge current 0 | no charging; surplus exported, battery covers the deficit |
The Predbat column follows the apps.yaml services in MQTT_TOPICS.md.
Hold exporting is Predbat stopping an export once its target is reached (stop: export). The
freeze modes aren't separate EMS modes: they are Auto with one limit changed. EMS mode numbers
and names are the goodwe library's EMSMode.
Fail-safe rules: every command expires (at most 60 min ahead) and falls
back to auto; a restart falls back to auto; a grid outage switches to
auto with the normal limits until 60 s after the grid is back. There is
no inverter-side watchdog on this firmware, so if the Pi dies the inverter
keeps the last mode.
Before on: disable the eco mode slots (they still act in auto; enabled
ones show up as warnings), and while control is on, change the on-grid
minimum SoC through CONTROL_MIN_SOC, not SolarGo - the executor rewrites
it. To change other settings in SolarGo, set a dashboard override to auto
first, otherwise an active command is re-applied within ~10 s.
OFF_GRID_SHADOW_SCAN_GUARD=on turns the inverter's shadow scan (PV global
MPPT scan) off as soon as the inverter goes off-grid - it misbehaves there -
and puts the previous value back once the grid has stayed up for
OFF_GRID_SHADOW_SCAN_COOLDOWN_MIN (default 15): outages come in bursts and
the grid often drops again minutes after returning. If shadow scan was
already off, nothing is written. Independent of CONTROL_MODE. The pending
restore is kept in shadow_scan_guard.json (git-ignored), so a restart
mid-outage still puts it back. A manual change on the /config page during
an outage or the cooldown is replaced by the saved value when it's restored.
Data is pulled every few seconds from the inverter in the local network using the Goodwe API
and streaming data to the frontend using Server Sent Events (SSE) for real-time updates.
Data is written into a SQLite database (data.db, inverter_history table) and can be processed later
by some scripts calculating the cost savings. Older data-xxxx-xx-xx_xx-xx-xx.csv files are legacy/historical
only - the app no longer writes to them (see "Upgrading" above for importing them).
See GOODWE_SENSOR_NOTES.md before writing anything that computes energy/power
balances across PV, battery, grid, and load fields - several of the raw sensor
names are easy to misread (which field is actually the grid meter, sign
conventions the goodwe library leaves undocumented, real hardware noise/quirks).
Design docs for a feature under development live in docs/superpowers/{specs,plans,notes}/
(dated filenames, e.g. docs/superpowers/notes/2026-09-11-diagram-mockup-checklist.md).
Once the feature is implemented and merged, its spec/plan get deleted in a follow-up
commit - the code and tests are the source of truth for current behavior, and the
design rationale/trade-offs stay recoverable via that PR's commit history rather than
bloating main's tree with docs that will otherwise silently go stale. notes/ (recorded
investigations/decisions, e.g. bug root-causes, calibration data) are longer-lived than
specs/plans and are kept unless truly obsolete.
Caveat: if you delete a spec/plan, grep the repo for links to it first (e.g. this file used to link straight to a since-deleted sqlite storage spec) - a dangling reference is easy to miss and won't fail any build.
Scripts starting from underscore _ are not used by the main application, they are some drafts, experiments or utils.
Dates for script input can be specified in formats: YYYY-MM-DD or DD.MM.YYYY.
There are a lot of poor-man's solutions, quick hacks and bad conventions in the code that should be fixed. Some of them include:
- mixing async (Goodwe API) and sync (Flask) code - async code runs in a separate thread, this might cause unexpected problems, although a special care has been taken to ensure proper synchronization between them and finalizing on exit - a solution would be to use an alternative to Flask that supports async (and also supports Server Sent Events)
- PV forecast assumes that there are two PV strings with the same peak power and tilt but with a different orientation angle (basically east-west configuration) and that logic is quite hard-coded even though it looks like it's configurable
- too much code in main.py - needs to be extracted for readability and maintainability
- Jinja templates use a lot of code repetition - needs to extract meta-templates and macros