file_path = 'csv_-29.815268_30.946439_fixed_23_0_PT5M.csv'
df = pd.read_csv(file_path) df['period_end'] = pd.to_datetime(df['period_end']) # Original timezone df.set_index('period_end', inplace=True)
df.index = df.index + pd.Timedelta(hours=2)
df = df[(df.index >= '2024-01-01') & (df.index < '2025-01-01')]
Figure 1: Schematic of Solar PV and BESS system with energy management-control logic unit.
A Python-based simulation of solar PV, battery storage and grid interaction for residential or microgrid applications.
The model operates on time-series irradiance and load data, prioritizing battery charging before any grid export.
Surplus PV power is exported only when the battery is fully charged, thereby maximizing self-consumption.
- Imports irradiance data (GHI, DNI, DHI) and computes PV generation.
- Implements battery-first logic: PV charges the battery before exporting to grid.
- Balances power among PV, load, battery, and grid with SOC (State of Charge) constraints.
- Visualizes PV generation, battery SOC, and grid imports/exports.
- Supports full-year or daily performance visualization.
- Load weather and timestamped PV data.
- Compute PV output power (DC → AC) using OSM-MEPS code.
- Compare PV generation to load → calculate net power.
- Decision Logic:
- If PV > load:
- Priority 1: Charge battery (until
SOC_max). - Priority 2: Export remaining PV to grid (only if battery is full).
- Priority 1: Charge battery (until
- If PV < load:
- Discharge battery (until
SOC_min). - Import remaining power from grid.
- Discharge battery (until
- If PV > load:
- Update SOC based on charge/discharge efficiency and record system state.
- Plot results showing PV, load, battery, and grid power dynamics.
- PV power (kW)
- Battery SOC (%)
- Grid import/export (kWh)
- Energy balance and self-consumption ratio
A high-resolution (5-minute) simulation of PV generation, battery dispatch, and smart grid interaction under South African 2024 conditions. Implements dynamic load modeling, time-of-use strategy, and strategic import/export control to minimize grid dependency and maximize self-consumption.
- Multi-configuration PV modeling (tilt, azimuth, derating)
- Realistic load profile with day/night/peak patterns
- Battery operation under SOC and power constraints
- Strategic export control.
- Input Data: Import 5-min irradiance and weather CSV (2024)
- PV Model: Compute AOI, POA, temperature-corrected DC → AC power
- Load Modeling: Dynamic + noise with min/max limits
- Battery Management:
- Charge when PV > load
- Discharge when PV < load
- Strategic export during evening peak hours
- Grid Interaction: Import/export according to SOC and thresholds
- Visualization: Plot daily/weekly/yearly PV, load, SOC, and grid flow
- PV power, load demand, SOC, grid exchange
- Energy KPIs (PV, import, export, load, SCR)
This Python project simulates a residential PV-battery system with grid interaction, using high-resolution (5-minute) weather and load data. It calculates power flows, battery operation and grid import/export.
- Multi-segment PV modeling and simulation: Accounts for tilt, azimuth, temperature, irradiance, cloud cover, and losses.
- Realistic residential load profile: Morning/evening peaks with stochastic variation.
- Battery energy management: Maximum self-consumption strategy with SOC limits.
- Grid interaction: Tracks import/export separately from battery operation.
- Energy metrics: Computes total PV energy, load consumption, grid import/export, self-consumption ratio.
- Financial impact: Estimates annual savings based on electricity costs and export revenue.
- High-quality plots: Power flow and SOC plots with negative spectrum, suitable for reports.
This code implements a maximum self-consumption strategy that prioritizes using solar energy on-site rather than exporting to the grid. The algorithm follows a strict operational sequence: first, the battery discharges to power home loads whenever solar generation is insufficient; second, any excess solar energy charges the battery for later use; and only as a last resort does the system import from the grid. Critically, the system eliminates strategic grid export - unlike previous versions that discharged batteries to the grid during peak hours for revenue, this configuration reserves all stored energy exclusively for self-consumption. The financial benefits are calculated as savings that would otherwise be incurred from consuming grid energy for most of the load profile. The battery parameters are optimized for this purpose with 92% efficiency and conservative state-of-charge limits (15-90%) to maximize lifetime performance.
INPUTS: PV_power, Load_power, Battery_SOC SOC_min, SOC_max, Battery_capacity Max_charge_rate, Max_discharge_rate Battery_efficiency, Time_step
OUTPUTS: Grid_power, Battery_power, Updated_SOC
ALGORITHM: Power_balance = PV_power - Load_power
IF Power_balance < 0 AND Battery_SOC > SOC_min:
# MODE 1: BATTERY DISCHARGE TO SUPPLY LOAD
Power_needed = -Power_balance
Energy_available = (Battery_SOC - SOC_min) * Battery_efficiency
Max_discharge_power = min(Max_discharge_rate, Energy_available / Time_step)
Discharge_power = min(Power_needed, Max_discharge_power)
Battery_SOC = Battery_SOC - (Discharge_power * Time_step / Battery_efficiency)
Battery_power = -Discharge_power
Grid_power = Power_balance + Discharge_power
ELSE IF Power_balance > 0 AND Battery_SOC < SOC_max:
# MODE 2: BATTERY CHARGE FROM EXCESS PV
Available_capacity = (SOC_max - Battery_SOC) / Battery_efficiency
Max_charge_power = min(Max_charge_rate, Available_capacity / Time_step)
Charge_power = min(Power_balance, Max_charge_power)
Battery_SOC = Battery_SOC + (Charge_power * Time_step * Battery_efficiency)
Battery_power = Charge_power
Grid_power = Power_balance - Charge_power
ELSE:
# MODE 3: DIRECT GRID INTERACTION
Battery_power = 0
Grid_power = Power_balance
This section calculates the financial benefit of using the PV + Battery system compared to relying entirely on grid electricity.
Assumptions:
- Grid electricity import cost: R 3.00 per kWh
- Grid electricity export revenue: R 1.50 per kWh
Let:
E_import= Total energy imported from the grid (kWh)E_export= Total energy exported to the grid (kWh)E_load= Total household load energy (kWh)
-
Cost of imported electricity:
Import Cost = E_import × 3.00 -
Revenue from exported electricity:
Export Revenue = E_export × 1.50 -
Net cost of electricity with PV + Battery:
Net Cost = Import Cost − Export Revenue -
Cost without PV + Battery system:
Cost without system = E_load × 3.00 -
Annual savings:
Savings = Cost without system − Net Cost
-
Total PV Generation: 146,413 kWh
-
Total Load Consumption: 92,706 kWh
-
Grid Import: 11,526 kWh
-
Grid Export: 59,919 kWh
-
Self-Consumption Ratio: 87.6%
-
Battery Performance:
- Energy Charged: 34,710 kWh
- Energy Discharged: 29,396 kWh
- Round-trip Efficiency: 84.7%
-
Financial Impact (R 3.00/kWh):
- Import Cost: R 34,578
- Export Revenue: R 89,879
- Net Cost: R -55,302
- Annual Savings: R 333,418
Figure 2: Schematic illustrating battery reconfiguration for fast charging (Parallel connection absorbs more current from solar PV during high irradiance and avoids charge limits related energy curtailment).
pip install pandas numpy matplotlib pvlib pytz
```bash
pip install pandas numpy matplotlib.