In Azure IoT (e.g., Azure IoT Hub) , a device twin is a JSON document stored in the cloud that represents the state of a physical device.
Think of it as a digital "shadow" or virtual copy of a device that lets the cloud and the device stay in sync.
🧠 What a Device Twin Is (Simple View)
A device twin = cloud-side model of a device, containing:
-
✅ Device configuration (what the cloud wants the device to do)
-
✅ Device state (what the device is currently doing)
-
✅ Metadata and properties
🧩 Structure of a Device Twin
A device twin has three key parts:
1. Tags (cloud-only metadata)
-
Used by backend apps
-
Not visible to the device
-
Example:
tags": {
"location": "factory1",
"deviceType": "sensor"
}
2. Desired Properties (set by the cloud)
-
What you want the device to be
-
Cloud → Device direction
Example:
"desired": {
"telemetryInterval": 10
}
👉 The cloud is saying:
"Device, send data every 10 seconds."
3. Reported Properties (set by the device)
-
What the device is actually doing
-
Device → Cloud direction
Example:
"reported": {
"telemetryInterval": 10,
"firmwareVersion": "1.2.3"
}
👉 The device replies:
"I'm now sending data every 10 seconds."
🔄 How It Works (Flow)
-
Cloud updates desired properties
-
Device receives the update
-
Device applies changes
-
Device updates reported properties
This keeps both sides synchronized.
🚀 Why Device Twins Are Useful
✅ 1. Device management at scale
Control thousands/millions of devices without direct connection:
-
Change configs remotely
-
Update firmware settings
✅ 2. Works even if devices are offline
-
Changes are stored in the twin
-
Device picks them up when it reconnects
✅ 3. Query devices like a database
You can run queries like:
SELECT * FROM devices WHERE properties.reported.firmwareVersion = '1.2.3'
✅ 4. No need for direct communication
You don't need a constant connection to each device
→ Twin acts as a state cache
🆚 Device Twin vs Direct Messaging
| Feature | Device Twin | Direct Message |
|---|
| Feature | Device Twin | Direct Message |
|---|---|---|
| Type | Persistent state | Real-time communication |
| Offline support | ✅ Yes | ❌ No |
| Use case | Config/state sync | Commands/events |
| Storage | Stored in cloud | Not stored |
🧠 Real-World Example
Imagine a smart thermostat system:
-
Cloud sets:
"desired": { "targetTemp": 22 }
-
Device reports:
"reported": { "currentTemp": 21 }
👉 You can monitor and control all thermostats without directly contacting each one.
🧾 Key Takeaway
A device twin is a persistent JSON document in Azure IoT Hub that keeps device state and configuration synchronized between cloud and device.