Your IoT Dashboard Shows Devices as Online. But Are They Actually Sending Fresh Data?
In an IoT application, seeing a device marked as online on a dashboard usually gives administrators confidence that everything is working correctly.
But is that really enough?
Imagine managing 500 IoT devices through an MQTT broker and a centralized backend. Your dashboard shows that 480 devices are online and only 20 are disconnected.
Everything looks healthy.
However, when you inspect the telemetry, you discover that some connected devices haven't published any data for several hours.
The MQTT connection is still active, but the application running on the device may have stopped working.
This raises an important engineering question:
Does an active MQTT connection actually mean that an IoT device is healthy?
In my opinion, treating connectivity and device health as the same thing is one of the mistakes that can make an IoT monitoring platform unreliable.
Let's explore how to design a better device-health monitoring system using MQTT, Python, FastAPI, Redis and PostgreSQL.
1. Why MQTT Connection Status Is Not Device Health
MQTT is widely used for communication between IoT devices and backend applications.
When a device establishes a connection with an MQTT broker, the broker knows that the client has connected.
Similarly, when the client disconnects or the broker detects a connection failure, connection-state information can be used to update the device's status.
MQTT also provides features such as Keep Alive and Last Will and Testament (LWT), which help applications detect unexpected disconnections.
However, there is an important limitation.
An active MQTT connection does not guarantee that the application running on the device is functioning correctly.
Consider a device with the following behavior:
- It connects successfully to the MQTT broker.
- It publishes temperature and humidity readings.
- Its sensor-reading process stops unexpectedly.
- The MQTT connection remains active.
- The dashboard continues showing the device as online.
From the broker's perspective, the connection may still be valid.
From the application's perspective, the device may no longer be providing useful data.
This is why connection status should be treated as one health indicator rather than the complete health state of a device.
2. Separate Connectivity From Telemetry Freshness
A better approach is to maintain separate measurements for device connectivity and telemetry freshness.
For example, consider these three devices:
| Device | MQTT connection | Last telemetry | Interpretation |
|---|
| NODE-101 | Connected | 20 seconds ago | Healthy |
| NODE-102 | Connected | 3 hours ago | Stale |
| NODE-103 | Disconnected | 5 minutes ago | Disconnected |
All three represent different conditions.
A production monitoring system should ideally track several dimensions.
Connectivity status
Indicates whether the device currently has an active communication connection, based on broker events and connection information.
Telemetry freshness
Measures how recently the backend received valid telemetry from the device.
Device-reported health
Provides information about the device's internal condition, such as sensor errors, memory pressure, firmware status or application failures.
These measurements should not be collapsed into one simplistic online/offline flag.
For example, a device could be connected and reporting regularly while its temperature sensor is returning invalid readings.
The communication layer is working, but the application-level functionality is not necessarily healthy.
3. Designing a Device Health Evaluation Service
Let's consider a practical implementation using Python and FastAPI.
Assume every device is expected to publish telemetry at a configured interval.
For example:
- Reporting interval: 60 seconds.
- Healthy threshold: Up to 2 reporting intervals.
- Stale threshold: Up to 5 reporting intervals.
- Beyond that: Offline or unresponsive, depending on connectivity information.
A simplified health evaluation function could look like this:
from datetime import datetime, timezone
def evaluate_device_health(
last_seen: datetime | None,
reporting_interval_seconds: int,
connected: bool,
):
if last_seen is None:
return "UNKNOWN"
now = datetime.now(timezone.utc)
age = (now - last_seen).total_seconds()
healthy_threshold = reporting_interval_seconds * 2
stale_threshold = reporting_interval_seconds * 5
if not connected:
return "DISCONNECTED"
if age <= healthy_threshold:
return "HEALTHY"
if age <= stale_threshold:
return "STALE"
return "UNRESPONSIVE"
This implementation is intentionally simple.
It separates communication state from the age of the latest telemetry message.
For example, if a device publishes every 60 seconds:
| Time since last telemetry | Connectivity | Health state |
|---|
| 30 seconds | Connected | Healthy |
| 100 seconds | Connected | Stale |
| 400 seconds | Connected | Unresponsive |
| 30 seconds | Disconnected | Disconnected |
| No telemetry | Unknown | Unknown |
The thresholds are illustrative, not universal.
A device that intentionally sleeps for 30 minutes should not be considered unhealthy simply because it has not published for 60 seconds.
The evaluation policy should be configurable based on device type, reporting frequency and expected operating conditions.
Another important detail is that a production evaluator should use a trusted server-side ingestion timestamp for telemetry freshness rather than blindly trusting the device's clock.
4. Handling Delayed, Duplicate and Out-of-Order Telemetry
There is another problem that becomes important when monitoring hundreds or thousands of devices.
Telemetry messages do not always arrive in the order in which they were generated.
Imagine a device publishes two messages:
Message A: Sequence 100
Message B: Sequence 101
Because of network delays or message processing behavior, the backend receives message B first and message A afterward.
If the application blindly updates the latest device state whenever a message arrives, the older message could overwrite newer information.
This can make the dashboard display an incorrect device state.
One solution is to include a monotonically increasing sequence number in every telemetry message.
For example:
{
"device_id": "NODE-104",
"sequence": 1824,
"timestamp": "2026-12-04T08:15:30Z",
"temperature": 28.4,
"humidity": 62.1
}
The backend can compare the incoming sequence number with the latest accepted sequence number for that device.
Conceptually:
async def process_telemetry(message):
current = await device_repository.get_latest(
message.device_id
)
if current and message.sequence <= current.sequence:
return {
"status": "IGNORED",
"reason": "Duplicate or out-of-order message"
}
await device_repository.update_latest(
device_id=message.device_id,
sequence=message.sequence,
telemetry=message,
)
return {"status": "ACCEPTED"}
This is a simplified example. In production, comparing and updating the sequence number must be atomic to prevent concurrent workers from accepting conflicting messages.
Sequence numbers also require careful handling when devices reboot. They should be combined with a persistent counter or a boot/session identifier so that a legitimate device restart does not cause all subsequent messages to be rejected.
MQTT Quality of Service (QoS) can help with message-delivery guarantees, but it does not automatically solve application-level duplicate processing or telemetry ordering.
That responsibility belongs to the application.
5. Scaling Device Health Monitoring
For a small deployment, querying a database whenever the dashboard loads might be sufficient.
But imagine monitoring 10,000 devices.
If every dashboard refresh calculates the health status of every device by scanning historical telemetry records, database load can grow unnecessarily.
A more scalable design separates recent device state from historical telemetry.
One possible architecture is:
- MQTT broker for device communication.
- FastAPI ingestion service for telemetry validation.
- Redis for the latest device state.
- PostgreSQL for persistent device metadata and historical records.
- A background health evaluator for stale-device detection.
- WebSockets or Server-Sent Events (SSE) for dashboard updates.
The architecture could look like this:
IoT Devices
|
| MQTT
v
MQTT Broker
|
v
FastAPI Ingestion
|
+-------+-------+
| |
v v
Redis PostgreSQL
Latest State Historical Data
|
v
Health Evaluator
|
v
Monitoring Dashboard
|
WebSockets
Redis can provide fast access to the latest device state without repeatedly querying historical records.
PostgreSQL remains useful for persistent device information, historical telemetry and reporting.
The health evaluator can periodically inspect devices whose reporting deadlines have passed and update their health states.
However, there is an important architectural consideration.
A device should not necessarily be marked offline just because a background worker has not processed its latest event yet.
The health evaluator must account for ingestion delays, worker backlogs and broker event delays.
For large fleets, an event-driven approach combined with scheduled deadline checks can be more efficient than repeatedly scanning every device.
6. Building a More Meaningful Device Health Model
A useful IoT monitoring platform should avoid representing every device condition with just two values: online and offline.
I would recommend maintaining at least these separate fields:
| Field | Purpose |
|---|
| Connection status | Indicates MQTT connectivity |
| Last telemetry received | Tracks server-side telemetry freshness |
| Last device heartbeat | Tracks application-level heartbeat |
| Last sequence number | Helps detect duplicates and out-of-order messages |
| Firmware version | Identifies the running firmware version |
| Device-reported status | Captures internal device health |
| Health status | Represents the backend's evaluated health state |
This information makes troubleshooting much easier.
For example, if a device is connected but telemetry is stale, the engineering team can investigate the application publishing process rather than immediately blaming the network.
Similarly, if the device is disconnected and its Last Will message has been received, the platform has additional evidence of a communication failure.
The goal is to make the dashboard communicate what is actually happening rather than displaying a misleading green indicator.
Conclusion
An active MQTT connection does not necessarily mean that an IoT device is healthy.
A broker can report a connected client while the application running on that device has stopped publishing useful telemetry.
That is why production IoT platforms should separate connectivity, telemetry freshness and device-reported operational health.
Combining MQTT connection events, application-level heartbeats, server-side timestamps, sequence numbers and backend health evaluation provides a much more meaningful picture of a device fleet.
For me, the important engineering lesson is this:
A connected device is not necessarily a healthy device. A reliable IoT monitoring system must measure what the device is actually doing, not just whether it is connected.
As IoT deployments grow, this distinction becomes increasingly important for monitoring, troubleshooting, maintenance and operational reliability.
#IoT #MQTT #Python #FastAPI #DeviceFleetManagement #DistributedSystems #Telemetry #Redis #PostgreSQL #SoftwareArchitecture
Join the conversation! Your thoughts help the community grow.