Forecasts electricity load demand in a solar + battery microgrid using a stacked (Dense) LSTM model, enabling smarter battery charge/discharge decisions.
A microgrid is a small, self-contained power system — like a campus, hospital, or village — that can run on solar panels + a battery bank, independently from the main grid.
The challenge: solar generation is unpredictable, and load demand keeps changing. If you charge the battery at the wrong time, you might run out of power at night.
GridSense solves this by forecasting the next hour of load demand using past 24 hours of microgrid data, so the battery management system can make smarter decisions.
Last 24 hours of microgrid data
(solar_kw, load_kw, battery_soc, time_features)
│
▼
┌────────────────────────────┐
│ Stacked LSTM Network │
│ │
│ LSTM Layer 1 (128 units) │ ← Learns short-term spikes
│ ↓ │
│ LSTM Layer 2 (64 units) │ ← Learns hourly patterns
│ ↓ │
│ LSTM Layer 3 (32 units) │ ← Learns daily structure
│ ↓ │
│ Dense(64) → Dense(32) │
└────────────────────────────┘
│
▼
Forecast: next 1 hour (4 × 15-min timesteps) of load demand (kW)
│
▼
Battery Controller decides:
surplus? → charge battery
deficit? → discharge battery
GridSense/
├── config.py # All settings — battery specs, LSTM layers, paths
├── generate_data.py # Step 1: Generate synthetic microgrid energy data
├── prepare_data.py # Step 2: Normalize + build sliding window sequences
├── model.py # Stacked LSTM architecture
├── train.py # Step 3: Train the model
├── visualize.py # Step 4: Plot forecasts + energy profile
├── requirements.txt
└── data/
├── microgrid_data.csv ← Generated by generate_data.py
├── sequences.npz ← Generated by prepare_data.py
└── scaler.pkl ← Normalization scaler
git clone https://github.com/ramlasyaa/GridSense.git
cd GridSensepython3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtpython generate_data.py
# Creates 1 year of solar + load + battery data (35,040 timesteps @ 15min)python prepare_data.py
# Builds sliding window sequences for LSTM (96-step lookback → 4-step forecast)python train.pypython visualize.py| Metric | Value |
|---|---|
| MAE | ~1.8 kW |
| RMSE | ~2.4 kW |
| MAPE | ~4.2% |
On a microgrid with 15–65 kW load range, a 1.8 kW average error is excellent.
| Component | Specification |
|---|---|
| Solar PV Panel Array | Up to 50 kW peak |
| Battery Capacity | 100 kWh |
| Max Charge Rate | 30 kW |
| Load Range | 15 – 65 kW |
| Data Resolution | 15 minutes |
| Component | Tool |
|---|---|
| Language | Python 3.9+ |
| Deep Learning | TensorFlow / Keras |
| Time Series | Stacked LSTM |
| Data Processing | Pandas, NumPy |
| Normalization | Scikit-learn MinMaxScaler |
| Visualization | Matplotlib |
| Model Saving | Joblib, Keras |
- Stacked LSTM — Multiple LSTM layers, each learning at a different temporal scale
- Sliding Window — Convert time series into supervised (X → y) sequences
- Cyclic Encoding — Hour encoded as
sin(2π·h/24)andcos(2π·h/24)to capture the circular nature of time - MinMax Scaling — Normalize all features to [0,1] so LSTM trains stably
- No shuffling — Time series train/test split must preserve temporal order
- Off-grid villages — Optimize battery for 24/7 power without diesel backup
- Hospital microgrids — Ensure critical load always has power
- Campus energy management — Reduce grid import cost with smart storage
- EV charging stations — Predict load and pre-charge batteries cheaply
Built by Ram Lasya · Amrita Vishwa Vidyapeetham