DEV Community

freelancing
freelancing

Posted on

Processing Pool Sensor Readings with Python

IoT applications often collect readings from sensors and turn them into information that people can understand. A pool-monitoring application is one example: it might collect water temperature, pH readings, and equipment status.

The first challenge is not creating a dashboard. It is deciding whether incoming readings are valid and how to process them consistently.

In this tutorial, we'll create a small Python data-processing pipeline using simulated readings.

1. Create sample data

readings = [
    {"temperature": 27.4, "ph": 7.3},
    {"temperature": 28.1, "ph": 7.4},
    {"temperature": 27.8, "ph": 7.2},
    {"temperature": None, "ph": 7.3},
    {"temperature": 29.0, "ph": 7.5},
]
Enter fullscreen mode Exit fullscreen mode

These readings are illustrative, not live sensor data or a recommendation for pool water chemistry.

2. Validate each reading

Sensors may produce missing values, malformed data, or readings outside expected physical ranges.

import math

def is_valid_reading(reading):
    temperature = reading.get("temperature")
    ph = reading.get("ph")

    if not isinstance(temperature, (int, float)):
        return False

    if not isinstance(ph, (int, float)):
        return False

    if not math.isfinite(temperature) or not math.isfinite(ph):
        return False

    return True
Enter fullscreen mode Exit fullscreen mode

This example validates data types and rejects non-finite numbers. A real deployment should define acceptable ranges based on sensor specifications and the intended use.

3. Filter valid records

valid_readings = [
    reading
    for reading in readings
    if is_valid_reading(reading)
]

print(f"Received: {len(readings)}")
print(f"Valid: {len(valid_readings)}")
print(f"Rejected: {len(readings) - len(valid_readings)}")
Enter fullscreen mode Exit fullscreen mode

Filtering lets the application distinguish between missing data and real measurements rather than silently treating both as zero.

4. Calculate summary statistics

def average_temperature(records):
    if not records:
        return None

    values = [r["temperature"] for r in records]
    return sum(values) / len(values)

average = average_temperature(valid_readings)

if average is not None:
    print(f"Average temperature: {average:.1f} °C")
else:
    print("No valid temperature readings available.")
Enter fullscreen mode Exit fullscreen mode

The function also handles an empty dataset, which is an important edge case in data processing.

5. Design a more reliable pipeline

A production IoT system needs more than averages. Consider this flow:

  1. Receive a sensor message.
  2. Validate its schema and timestamp.
  3. Check whether the reading is duplicated.
  4. Store the original reading.
  5. Flag suspicious values for review.
  6. Generate summaries for the dashboard.

It is also useful to track sensor identifiers, calibration dates, units, and communication failures. A missing reading should not automatically be interpreted as a safe reading.

Conclusion

Reliable dashboards begin with reliable data. By separating validation, filtering, and aggregation, developers can make monitoring systems easier to test and maintain.

The same design can be applied to weather stations, greenhouse monitoring, energy meters, and other sensor-based applications.

Top comments (0)