Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Projects and Practice

Project: Sensor Monitoring with Anomaly Alerts

Build a monitoring pipeline for hourly sensor data: remove the daily cycle, flag spikes with a robust score, catch a stuck sensor and a level shift, and score the alerts against known incidents.

A factory temperature sensor reports once per hour. Operators want an alert when something goes wrong, but not a flood of false alarms. In this project you build the whole pipeline: simulate data with known incidents, remove the normal daily pattern, detect three kinds of faults with three simple detectors, and finally score the alerts the way you would in a real evaluation (precision, recall and delay).

The incidents we plant (so we can check ourselves):

  1. Spikes: single readings that jump by about 15 degrees (hours 300, 650).
  2. Stuck sensor: the value freezes for 12 hours (from hour 500).
  3. Level shift: after hour 800 the baseline permanently rises by 4 degrees.
flowchart LR
    R["Raw readings"] --> S["Remove daily cycle<br/>(STL remainder)"]
    S --> D1["Spike detector<br/>(robust score)"]
    S --> D2["Level shift detector<br/>(CUSUM)"]
    R --> D3["Stuck detector<br/>(rolling std)"]
    D1 --> A["Alerts"]
    D2 --> A
    D3 --> A

Each fault type gets the cheapest detector that can see it.

Step 1: simulate the data

Python Hourly sensor data with planted incidents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: remove the daily cycle

Alarm thresholds on raw temperature would trigger every afternoon. Use STL with a 24-hour period and examine the remainder.

Python STL remainder and a robust score

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two spikes at hours 300 and 650 are found. The list also contains hours 502 to 511 and 518 (the frozen sensor no longer follows the daily cycle, so its remainder grows) and hour 801 (right after the level shift). So this detector reacts to every fault type, which is why real systems merge flags that lie within a few hours into one incident and let the specialised detectors below say what kind of incident it is.

Step 3: the stuck sensor

A frozen sensor repeats the same value, so the rolling standard deviation of the raw readings collapses to zero. Normal noise never produces 6 identical readings in a row.

Python Rolling standard deviation detects the frozen value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The alarm fires after the sixth identical reading, which is the detection delay: the price of requiring evidence.

Step 4: the level shift

A shift is not a spike, so a single large value will not show it. CUSUM on the remainder accumulates small, persistent deviations; when the running sum leaves a tolerance band, raise an alarm.

Python One-sided CUSUM alarm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The slack k ignores small noise; the threshold h sets how much accumulated evidence is needed. Lower h means faster alarms and more false ones. The residual here comes from a fixed seasonal profile learned on clean history rather than from a fresh STL fit. A decomposition refitted on the whole series has a flexible trend that partly absorbs a shift (and drifts slowly even before it), so it can raise a false alarm; a frozen profile keeps "normal" fixed. The alarm fires at hour 801, one hour after the shift, because a 4-degree jump is about six standard deviations of the noise.

Step 5: score the alerts

Real evaluations compare alerts with labelled incidents. Count an alert as a true positive if it lands within a tolerance window of an incident, and report:

  • Precision: of the alerts raised, how many were real.
  • Recall: of the real incidents, how many were caught.
  • Delay: hours from the start of the incident to the alert.
Python Precision, recall and delay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The alert list above is an illustration of how the scoring works, not output from the earlier detectors. Plug your own detector output into the same code.

Hardening for production

  • Deduplicate alerts: merge flags closer than a few hours into one incident.
  • Per-sensor baselines: each sensor gets its own seasonal profile and scale.
  • Maintenance windows and holidays: suppress alerts or add them as regressors.
  • Adaptive thresholds: refit baselines on a schedule, but never on data flagged as anomalous.
  • Missing data: a gap is itself an alert type; do not let it flow into rolling statistics as zero.
  • Review loop: log operator feedback ("false alarm") and use it to tune thresholds; see anomaly detection and change point detection.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why remove the daily cycle before thresholding?

    Anomalies are deviations from the expected pattern; the pattern must be subtracted first.
  2. 2. Which signal best reveals a frozen sensor?

    Identical consecutive readings have zero variance, which normal noisy readings never show.
  3. 3. A CUSUM alarm with a lower threshold h will:

    Less accumulated evidence is required, trading false alarms for speed.
  4. 4. Which metrics evaluate an alert system with labelled incidents?

    Incidents are rare, so accuracy is meaningless; precision, recall and delay describe usefulness.

Summary

  • Build detectors by fault type: robust residual scores for spikes, rolling variance for stuck values, CUSUM for level shifts.
  • Subtract the expected seasonal pattern first, and learn normal behaviour from clean history only.
  • Score alerts with precision, recall and delay against labelled incidents.
  • Production needs deduplication, per-sensor baselines, maintenance windows and a feedback loop.

Next: Time Series Practice Set