Skip to content

iot

The iot module provides types and functions for IoT device monitoring, sensor data processing, anomaly detection, and alert management. It is organized into sub-modules for types, anomaly detection, and simulation-based monitoring.

from std::iot::types use {
SensorReading, DeviceState, Alert, ThresholdConfig, MonitoringConfig,
sensor_reading, create_alert, threshold_config, symmetric_thresholds, check_thresholds
}
from std::iot::anomaly use { zscore_anomalies, spike_detection, combined_anomaly_detection }
from std::iot::simulation use { monitor_device, monitoring_report }
FieldTypeDescription
device_idstringUnique device identifier
timestamptimestampReading timestamp
valuenumberPrimary sensor value
unitstringUnit of measurement
qualitynumberSignal quality 0.0-1.0

Tracks device operational state during monitoring:

FieldTypeDescription
device_idstringDevice identifier
statusstring"online", "offline", "degraded", or "maintenance"
last_readingnumberLast recorded value
readings_countnumberTotal readings processed
anomaly_countnumberAnomalies detected
alert_countnumberAlerts generated
FieldTypeDescription
severitystring"info", "warning", "critical", or "emergency"
device_idstringSource device
alert_typestring"threshold", "anomaly", or "offline"
messagestringHuman-readable message
valuenumberValue that triggered alert
thresholdnumberThreshold that was crossed

Four-level threshold configuration with hysteresis:

FieldTypeDescription
high_criticalnumberCritical high threshold
high_warningnumberWarning high threshold
low_warningnumberWarning low threshold
low_criticalnumberCritical low threshold
deadbandnumberHysteresis to prevent oscillation
  • sensor_reading(device_id, value, unit?, quality?) — Create a sensor reading with current timestamp.
  • create_alert(severity, device_id, alert_type, message, value?, threshold?) — Create a generic alert.
  • threshold_alert(device_id, value, threshold, is_high?) — Create a threshold alert with auto-severity.
  • offline_alert(device_id, last_seen_seconds) — Create an offline alert.
  • anomaly_alert(device_id, value, expected, deviation) — Create an anomaly alert.
  • threshold_config(high_critical, high_warning, low_warning, low_critical, deadband?) — Create threshold configuration.
  • symmetric_thresholds(center, warning_delta, critical_delta, deadband?) — Create symmetric thresholds around a center value.
  • check_thresholds(value, config) -> string — Check value against thresholds. Returns "ok", "warning", or "critical".

Statistical and pattern-based anomaly detection for sensor data:

  • zscore_anomalies(series, threshold?) — Z-score based (default threshold: 3.0 standard deviations).
  • mad_anomalies(series, threshold?) — Modified Z-score using Median Absolute Deviation (more robust to outliers).
  • iqr_anomalies(series, k?) — Interquartile range based (default k: 1.5).
  • rolling_zscore_anomalies(series, window?, threshold?) — Adaptive Z-score in a sliding window.
  • spike_detection(series, window?, spike_threshold?) — Detect sudden spikes/drops relative to recent baseline.
  • flatline_detection(series, window?, epsilon?) — Detect stuck sensors (no variance).
  • oscillation_detection(series, window?, threshold?, min_oscillations?) — Detect rapid oscillation patterns.
  • drift_detection(series, baseline_window?, drift_threshold?) — Detect gradual drift from baseline.
  • time_aware_anomalies(readings, day_thresholds, night_thresholds, day_start_hour?, day_end_hour?) — Different thresholds for day/night periods.
  • combined_anomaly_detection(series) — Flags anomaly if any method detects one (Z-score OR IQR OR spike).
  • consensus_anomalies(series, min_agreement?) — Only flags if multiple methods agree.
  • count_anomalies(anomaly_flags) -> int — Count true values.
  • get_anomaly_indices(anomaly_flags) -> Array<int> — Get indices of anomalies.

Simulation-based device monitoring using the core simulation engine:

Monitor a single device’s sensor readings. Uses Welford’s online algorithm for running statistics and Z-score anomaly detection.

let result = monitor_device(temperature_readings, {
device_id: "sensor-001",
thresholds: symmetric_thresholds(25.0, 5.0, 10.0),
offline_timeout: 300,
anomaly_threshold: 3.0,
collect_alerts: true
})

monitor_sensors(sensors, config, correlation_check?)

Section titled “monitor_sensors(sensors, config, correlation_check?)”

Monitor multiple correlated sensors with optional cross-sensor correlation checks.

Generate a summary report from monitoring results.