Peak Shaving Controller
ThePeakShavingController optimizes battery storage dispatch to reduce peak electricity demand, minimizing demand charges and energy costs. It uses a two-pass algorithm — discharge during peaks, recharge in the cheapest off-peak slots.
How It Works
Two-Pass Algorithm
Pass 1 — Discharge: Walk forward through each time slot. Wherever the net load exceeds the peak target, discharge the battery to bring it down. Respects SOC minimum, max discharge rate, and round-trip efficiency. Pass 2 — Charge: Total up the energy discharged in Pass 1. Schedule recharging in the cheapest available slots (sorted by tariff price) that have headroom below the peak target. This avoids creating new peaks during charging.Battery Specification
Battery Parameters
| Parameter | Description | Typical Range |
|---|---|---|
capacity_kwh | Nameplate energy capacity | 50–500 kWh |
max_charge_kw / max_discharge_kw | C-rate power limits | 0.5C–2C |
efficiency_charge / efficiency_discharge | One-way efficiency | 0.90–0.98 |
min_soc_percent / max_soc_percent | Usable SOC window | 10–90% |
degradation_cost_per_kwh | Wear cost per kWh cycled | $0.01–0.05 |
Configuration
Auto-Target Mode
Ifpeak_target_kw=None, the controller automatically sets the target to the 80th percentile of the forecast load, shaving the top 20% of peak demand:
Running an Optimization
Result Structure
Schedule Columns
| Column | Description |
|---|---|
load_forecast_kw | Original load forecast |
solar_forecast_kw | Solar generation forecast |
net_load_before_kw | Net load before battery (load - solar) |
battery_charge_kw | Battery charging power (from grid) |
battery_discharge_kw | Battery discharging power (to site) |
battery_soc_kwh | Battery state of charge in kWh |
battery_soc_percent | Battery state of charge as percentage |
net_load_after_kw | Final net load after battery dispatch |
peak_target_kw | Peak target threshold |
price_per_kwh | Energy price per slot |
slot_cost_before | Energy cost per slot (before optimization) |
slot_cost_after | Energy cost per slot (after optimization) |
Constraint Enforcement
SOC Bounds
SOC Bounds
The battery SOC never drops below
min_soc_percent or exceeds max_soc_percent. Both charge and discharge are clipped to stay within the usable energy window, accounting for round-trip efficiency losses.Charge / Discharge Rate Limits
Charge / Discharge Rate Limits
Power is capped at
max_charge_kw and max_discharge_kw at every time slot. The two-pass algorithm naturally respects these limits during both discharge (Pass 1) and charge (Pass 2) phases.Peak Target
Peak Target
The controller aims to keep net load at or below the target. If the battery is too small to fully shave the peak, the result status is “feasible” rather than “optimal”, and the peak is reduced as much as possible.
No New Peaks
No New Peaks
During Pass 2 (charging), the algorithm only allocates power up to the headroom between current load and the peak target. This prevents charging from creating new demand peaks.
Solar Integration
When a solar forecast is provided, the controller works with net load (load - solar), which:- Reduces the effective peak that needs shaving
- Creates more low-cost charging opportunities during solar hours
- Naturally coordinates battery and solar for maximum benefit
Demand Charge Savings
The controller calculates demand charge savings whendemand_charges are present in the tariff: