fix(optimization): let the AC setpoint cap the total charge where the inverter does (#1334)

* fix(optimization): let the AC setpoint cap the total charge where the inverter does

Some hybrid inverters limit the battery's whole charge current to the grid
charge setpoint while grid charging is on. A Deye 12K in time-of-use grid
charging with max_grid_charge_current = 75 A charges at ~80 A even with 8 kW
of PV, and exports the rest - including the surplus of micro inverters on
the grid side. GENETIC modelled an AC slot as "PV surplus first, grid adds
ac_charge x the remaining charge power", so under PV surplus an AC slot
looked at least as good as a DC slot and the optimizer picked a low AC
factor, while the real system exported what the plan meant to store.

New inverter config option devices.inverters[].ac_charge_limits_total_charge
(GENETIC scope, default False, so the existing model is unchanged). When
True, an AC slot caps the battery's raw charge at ac_charge x
max_charge_power_w from all sources: PV surplus above the cap is exported,
the grid only fills what PV leaves of it. The option reaches the optimizer
through InverterCommonSettings.to_genetic_param, i.e. POST /v1/optimize and
the EMS run. GENETIC0 (POST /optimize) is left as it is.

The AC slot logic moves into Inverter (ac_charge_factor,
begin_ac_charge_slot, charge_battery_from_grid) so the simulation and the
tail value curve share it; Battery gets a per-slot charge cap that reset()
lifts again.

* test(optimization): narrow optional simulation arrays for mypy

The annotated fixture indexes GeneticSimulation arrays typed as Optional; assert them first so the locked mypy hook passes.
This commit is contained in:
Andreas
2026-09-21 16:15:00 +02:00
committed by GitHub
parent f5c6d2bc1e
commit 35f77c9b99
7 changed files with 423 additions and 43 deletions
@@ -494,7 +494,18 @@ class GeneticSimulation(PydanticBaseModel):
0.0
)
# AC charge factor of this slot, capped by max_ac_charge_power_w
ac_charge_factor = 0.0
if battery_fast and ac_charging_possible:
ac_charge_factor = ac_charge_hours_fast[hour]
if inverter_fast:
ac_charge_factor = inverter_fast.ac_charge_factor(ac_charge_factor)
if inverter_fast:
# Some inverters cap the total charge power with the AC setpoint;
# that cap has to be in place before PV charges the battery.
if ac_charge_factor > 0.0:
inverter_fast.begin_ac_charge_slot(hour, ac_charge_factor)
energy_produced = pv_prediction_wh_fast[hour]
hourly_feed_in_tariff = elect_revenue_per_hour_arr_fast[hour]
# bat_grid_export_hours carries the export level per slot:
@@ -522,39 +533,21 @@ class GeneticSimulation(PydanticBaseModel):
hourly_feed_in_tariff = elect_revenue_per_hour_arr_fast[hour]
# AC PV Battery Charge
if battery_fast:
hour_ac_charge = ac_charge_hours_fast[hour]
if hour_ac_charge > 0.0 and ac_charging_possible:
# Cap charge factor by max_ac_charge_power_w if set
effective_charge_factor = hour_ac_charge
if max_ac_charge_w_fast is not None and battery_fast.max_charge_power_w > 0:
# DC power = max_charge_power_w * factor
# AC power = DC power / ac_to_dc_eff
# AC power must be <= max_ac_charge_power_w
max_dc_factor = (
max_ac_charge_w_fast * ac_to_dc_eff_fast
) / battery_fast.max_charge_power_w
effective_charge_factor = min(effective_charge_factor, max_dc_factor)
if effective_charge_factor > 0:
battery_charged_energy_actual, battery_losses_actual = (
battery_fast.charge_energy(
None, hour, charge_factor=effective_charge_factor
)
)
# DC energy entering the battery (before battery internal efficiency)
dc_energy = battery_charged_energy_actual + battery_losses_actual
# AC energy consumed from grid (accounts for AC→DC conversion loss)
ac_energy = dc_energy / ac_to_dc_eff_fast
# Inverter AC→DC conversion losses
inverter_charge_losses = ac_energy - dc_energy
consumption += ac_energy
energy_consumption_grid_actual += ac_energy
losses_wh_per_hour[hour_idx] += (
battery_losses_actual + inverter_charge_losses
)
if ac_charge_factor > 0.0 and battery_fast:
if inverter_fast:
ac_energy, ac_charge_losses = inverter_fast.charge_battery_from_grid(
hour, ac_charge_factor
)
else:
# Without an inverter the grid charges the battery losslessly
# (AC-to-DC efficiency 1.0).
stored, ac_charge_losses = battery_fast.charge_energy(
None, hour, charge_factor=ac_charge_factor
)
ac_energy = stored + ac_charge_losses
consumption += ac_energy
energy_consumption_grid_actual += ac_energy
losses_wh_per_hour[hour_idx] += ac_charge_losses
# Update hourly arrays
if (
@@ -50,11 +50,15 @@ def _simulate_action(
bat.soc_wh = float(energy_wh)
bat._charged_raw_wh_per_slot.fill(0)
bat._discharged_raw_wh_per_slot.fill(0)
bat._charge_limit_raw_wh_per_slot.fill(np.inf)
ac_enabled = inv.ac_to_dc_efficiency > 0 and (
inv.max_ac_charge_power_w is None or inv.max_ac_charge_power_w > 0
)
bat.charge_array[0] = ac_rate if ac_rate > 0 and ac_enabled else dc
bat.discharge_array[0] = discharge if export == 0 or tariff > 0 else 0
rate = inv.ac_charge_factor(ac_rate)
if rate > 0:
inv.begin_ac_charge_slot(0, rate)
sold, bought, losses, _ = inv.process_energy(
pv,
load,
@@ -64,18 +68,11 @@ def _simulate_action(
)
ac_grid_charge_wh = 0.0
if ac_rate > 0 and inv.ac_to_dc_efficiency > 0:
rate = ac_rate
if inv.max_ac_charge_power_w is not None and bat.max_charge_power_w > 0:
rate = min(
rate,
inv.max_ac_charge_power_w * inv.ac_to_dc_efficiency / bat.max_charge_power_w,
)
bat.charge_array[0] = rate
if rate > 0:
stored, loss = bat.charge_energy(None, 0, charge_factor=rate)
ac_grid_charge_wh = (stored + loss) / inv.ac_to_dc_efficiency
ac_grid_charge_wh, ac_losses = inv.charge_battery_from_grid(0, rate)
bought += ac_grid_charge_wh
losses += loss + max(ac_grid_charge_wh - stored - loss, 0.0)
losses += ac_losses
if direct_marketing and tariff < 0:
sold = 0.0
discharged_wh = bat.discharged_energy_wh(0)