2026G1

Pipeline, cleaning decisions, model and detection rules (KAN-142, KAN-103)

This page explains, in plain terms, how we go from the four raw Excel files to alerts with a € and CO₂ value, and why we made each choice. The experiments behind the choices are in the experiment log; every column is described in the data dictionary.

1. The pipeline at a glance

4 raw Excel files (one per zone)
   │  1. combine and unify           src/save_parquet.py (loader.py, unify.py)
   │  2. build one hourly table      ** script missing in the repo **
   │  3. clean (rules R1–R12)        src/clean.py
   │  4. add calendar                src/build_calendar.py
   │  5. add outdoor weather         src/download_weather.py
   │  6. add electricity prices      src/download_price_omie.py
   │  7. merge everything            src/merge_external.py
   │  8. freeze dataset v1           src/freeze_dataset.py  → data/processed/cedint_hourly_v1.parquet
   ▼
Notebook 01  Exploration: what drives electricity use?
Notebook 02  Expected use per circuit (baselines → Ridge → LightGBM)
Notebook 03  Alerts, chronic waste, € and CO₂   → reports/*.csv (input for the dashboard)
Notebook 04  Model search (7 model types, month-by-month check) → final LightGBM settings
             (04_model_search, uses the cleaned zone files data/zone_*_hourly.parquet via src/zones.py)
Notebook 04  90 % prediction band per circuit     → reports/interval_bands.parquet
             (04_prediction_interval)
Notebook 05  Prophet compared with LightGBM
   ▼
Dashboard    dashboard/app.py (Streamlit, 7 pages) reads data/processed/ and reports/

Two notebooks share the number 04 because they were written in parallel.

Each step writes a file that the next step reads, so any step can be re-run on its own. The frozen dataset comes with a MANIFEST.json that stores a fingerprint (SHA-256) of every input and output file, so anyone can check that they are using exactly the same data. pytest runs automatic checks on it (no duplicate hours, correct grid, no leakage in the features).

Known gap: step 2 (turning the 2025 minute readings into hours and building the hourly table) was done, but its script is not in the repo yet. Until it is pushed, the pipeline cannot be rebuilt from the raw files.

2. Cleaning decisions (KAN-103)

Principles

The two data periods

  Period A Period B
Dates 2 Apr 2020 – 28 Feb 2023 18 Aug – 13 Oct 2025
Readings one reading per hour up to one per minute, averaged to one value per hour
Hours 25,489 1,341

There is no data at all between March 2023 and August 2025 (901 days). This gap is in the source files. Because of it, the two periods are never joined: features that look back in time (e.g. “last week”) stop at the gap. Period A is used to build the models, period B to check them on recent data.

Rules applied

Rule Problem found Decision Why Values touched
R1 Timestamps have no time zone Treated as UTC; Madrid local time added No jumps at the daylight-saving changes, and light sensors match sunrise and sunset in UTC (src/check_utc.py) all rows
R2 Some hours are missing Complete hourly grid; missing hours left empty So every analysis sees the gaps instead of skipping them 524 empty hours
R3 Two circuits measure nothing useful SE Projector (always 0) and SW RACK (stops 3 Dec 2021) removed No information 2 circuits
R4 NW Emergency Lighting is negative 97.7 % of the time Sign flipped The meter is wired in reverse; a lamp cannot produce energy 24,230
R5 Small negative values (−10 to 0 W) Set to 0 Meter noise around zero 31,870
R6 Larger negative values (below −10 W) Set to empty Not noise; a real error 4
R7 Same non-zero value for 6+ hours Set to empty (period A) A stuck sensor 0 found
R8 6+ hours of exact 0 on a circuit that normally uses power Set to empty Sensor off, not real zero use 3,694
R9 Temperature, humidity and light all 0 together Set to empty Sensor failure safeguard
R10 Indoor temperature of 739–1,411 °C (SW, 1–7 Mar 2022) Values outside the allowed range set to empty Impossible values see note below
R11 2025 hours built from few minute readings Flagged low_coverage (kept) The hourly average is less reliable 611
R12 Totals in the raw file include bad values Zone and storey totals recalculated from clean circuits, only when all circuits are present A total with a missing part would look like a drop in use all totals

What we checked and did not change

Finding Decision Reason
Energy counters (Ea, Er) reset and jump Not used at all; energy per hour is taken from the average power W Where the counters work, both give the same result (ratio 1.00), and W has no resets
Presence sensors stuck at 1.0 for weeks (NW, SE, SW) Kept, but not used in models or conclusions Presence cannot be 100 % for 83 days; this is a sensor fault
SW humidity identical for up to 71 days Kept, not used Stuck or insensitive sensor
NE Plugs drops from about 190 W to 25 W for whole days (10 periods) Kept Plausible (load switched off) but cannot be confirmed
NW Plugs peaks up to 8.25 kW in January 2023 Kept Neighbouring hours are consistent, so they look real (heaters)
COVID lockdown (spring 2020) Kept in the data, flagged, left out of model training The building was not used normally

Open points (to settle in the group)

  1. R10 threshold differs between two versions. src/clean.py removes indoor temperatures outside 5–45 °C. The cleaned Excel file uses 0–45 °C and keeps 70 hours of 3.8–5 °C in January 2021 (a real cold wave). We should agree on one value.
  2. Extra rule in the Excel file only: it also blanks 934 hours where NW Emergency Lighting reads about 0 W (Dec 2021, Feb–Apr 2022). This rule is not in src/clean.py. Agree whether to add it.
  3. Calendar dates: the Christmas break (until 6 January) and the whole-August closure look wrong in places.
  4. Missing script for step 2 (see above).

3. The model: what is “normal” use?

To say that something is wasted, we first need to know how much each circuit normally uses at a given hour. The model predicts this expected use; the gap between actual and expected is what we look at.

   
What we predict energy used per hour per circuit (Wh), one day ahead
Circuits 9 “focus” circuits: Plugs & Sockets (all 4 zones), Lighting (NW, SW, SE), HVAC (NW, SE)
Information the model uses hour, day of the week, working day / holiday / break, season, outdoor temperature (and how far it is below or above a comfortable level), sunlight, humidity, and the circuit’s own use 1 day, 2 days, 1 week and 2 weeks earlier
Information it may not use anything from the same hour (that would be cheating), indoor sensors (they are effects of the use, not causes), anything across the 2023–2025 gap
Final model LightGBM (gradient-boosted decision trees), one model per circuit, all with the same settings (reports/final_model_settings.json)
Why this model out of seven model types (linear, Ridge, Lasso, decision tree, random forest, LightGBM, neural network), LightGBM has the lowest error in a month-by-month check over 15 months and wins most months. A neural network and a random forest come close, and Ridge is not far behind, which shows the model is not over-complicated. Prophet, a well-known forecasting tool, is worse on every circuit.
Why one day ahead predicting one hour ahead is more accurate, but such a model quickly “gets used to” a heater left on and stops warning; a model that only knows yesterday keeps warning
How we tested trained on Jun 2020 – Aug 2022, settings chosen on Sep – Dec 2022, then month by month from Sep 2021 to Dec 2022, tested once on Jan – Feb 2023, then checked on Aug – Oct 2025
Uncertainty a 90 % band around the prediction (quantile LightGBM with a rolling 28-day correction); on the test months 89 % of the hours fall inside it. Shown in the dashboard’s “Expected vs actual” page.
Main metric MAE in kWh: the average miss, in the unit the client thinks in

Honest limitation: hour by hour, the model is still off by more than the 30 % that the industry guideline ASHRAE 14 asks for, on most circuits. The main reason is people: heaters and lights are switched on and off by hand. For finding large waste this is good enough (see the synthetic test below), but it should be said in the presentation.

All experiments behind these choices, with their numbers, are in the experiment log (EXP-03 to EXP-05 and EXP-10 to EXP-13). Still open: notebook 03 and the dashboard export still use the per-circuit settings of notebook 02 (reports/model_params.json). The difference is very small (0.181 against 0.182 kWh per hour on the test months), but it should be aligned.

4. Detection rules: from “unusual” to “alert”

We use two signals, because one model cannot see everything.

Signal 1 — Events (something unusual today)

  1. For every hour: miss = actual − expected.
  2. An hour is flagged when its miss is far above the normal spread of misses for that circuit and that type of hour (working hours or off-hours). Normal misses are about twice as big during working hours, so one threshold for all hours would either miss night waste or flood the manager with daytime alarms.
  3. Flagged hours close together (gap ≤ 1 h) become one event. An event must last at least 2 hours and be at least 0.5 kWh above expected (smaller events are worth less than €0.07).
  4. How strict? For each circuit we chose the threshold that gives about one event per week at most. A building manager stops reading alarms if there are too many.

Tested with fake loads: we added 40 fake “forgotten devices” at night (1 kW on plug circuits, 300 W on lights and HVAC) and checked whether the detector found them. It found 100 % on 7 of the 9 circuits. On NW Plugs, the noisiest circuit, one heater is found only 30 % of the time, but 2 kW is found 92 % of the time.

Signal 2 — Chronic off-hours waste (the same thing every week)

A model that looks at last week’s use slowly learns repeated waste (heaters on a timer every weekend) as “normal”. So we add a simple, transparent rule:

Severity

Severity Excess energy per event
Low under 2 kWh
Medium 2 – 10 kWh
High over 10 kWh

Thresholds are in params.yaml and can be changed without touching the code.

Value in € and CO₂

  How Source
€ (lower bound) excess kWh × hourly wholesale price OMIE (energy only, no network costs or taxes)
€ (all-in) excess kWh × €0.142/kWh Eurostat non-household price
CO₂ excess kWh × hourly CO₂ intensity of the Spanish grid (column co2_intensity_g_kwh) Red Eléctrica ESIOS indicator 10355, from the cleaned zone files. Alternative in params.yaml: yearly CNMC supplier-mix factor (259–273 g/kWh), about twice as high

Events during working hours are shown as “to investigate” but not counted as savings, because part of that use is legitimate.

Outputs (input for the dashboard)

File Content
reports/anomaly_events.csv one row per event: circuit, start, end, excess kWh, €, kg CO₂, severity, message
reports/offhours_waste_daily.csv chronic waste per circuit per day
reports/savings_summary.csv potential savings per zone, circuit and year

5. Main results

Year Potential savings (9 focus circuits) € CO₂
2021 7.2 MWh (32 % of their use) €750 – 1,020 0.8 t
2022 6.1 MWh (27 % of their use) €860 – 1,010 0.8 t

Biggest lever: electric heaters on the NW plug circuits at night, at weekends and in breaks (3–4 MWh per year), then the NW lights that are on every night 22:00–07:00 (about 1 MWh per year).

6. Limitations