An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
A device twin in Azure IoT Hub is a JSON document that IoT Hub maintains for each device identity. It stores device-related information such as metadata, configuration, and state, and is used by device apps and back-end apps to synchronize device conditions and configuration and to query and target long-running operations.
Key characteristics:
- Automatically created and deleted with the device identity in the IoT Hub identity registry.
- Used to store and sync:
- Device-specific metadata (for example, location, device type).
- Configuration and runtime state (for example, connectivity method, firmware rollout state).
- Enables back-end apps to manage devices even when they are offline; the device reconciles state when it reconnects.
Structure of a device twin JSON document:
- Device identity properties (root)
- Read-only properties that mirror the corresponding device identity in the identity registry.
- Tags
- Cloud-only metadata.
- Read/write by back-end apps; not visible to device apps.
- Used to organize and query devices (for example, by location, function, or device type).
- Desired properties
- Set by back-end apps to express the desired configuration or state.
- Read by device apps; device apps can subscribe to change notifications.
- Used together with reported properties to synchronize configuration or conditions (for example, desired firmware version, telemetry interval).
- Reported properties
- Set by device apps to report current configuration or state.
- Read and queried by back-end apps.
- Used together with desired properties to track progress and actual state (for example, current firmware version, battery level, connectivity method).
- Metadata and versioning
- Desired and reported properties include
$versionvalues that are guaranteed to be incremental. - The twin has an
etagfor optimistic concurrency.
- Desired and reported properties include
Synchronization and offline behavior:
- Device twins support optimistic concurrency using
etagon the twin and$versionon desired/reported properties. - IoT Hub does not preserve desired property update notifications for disconnected devices.
- Recommended reconnection flow for a device app:
- Connect to IoT Hub.
- Subscribe for desired property update notifications.
- Retrieve the full desired properties document.
- The device can ignore notifications whose
$versionis less than or equal to the version of the retrieved document, because versions always increment. - This pattern ensures that devices converge to the latest desired configuration after being offline.
Designing desired vs reported properties:
- Desired properties:
- Model the configuration or target state that the back end wants devices to reach (for example,
desired.telemetryInterval,desired.firmware.version). - Back-end apps update desired properties; devices observe and apply them.
- Model the configuration or target state that the back end wants devices to reach (for example,
- Reported properties:
- Model the actual state and capabilities of the device (for example,
reported.telemetryInterval,reported.firmware.version,reported.batteryLevel). - Devices update reported properties; back-end apps read and query them.
- Model the actual state and capabilities of the device (for example,
- Together they allow the back end to compare expected vs actual state and drive workflows such as configuration rollout or firmware updates.
Using device twins at scale:
- Tags and reported properties can be queried by back-end apps to select subsets of devices (for example, all devices in a region with a specific capability or state) and to target long-running operations.
- Optimistic concurrency with
etagand$versionshould be used to ensure consistent updates when multiple actors update twins.
Programming model:
- Device apps and service (back-end) apps use Azure IoT SDKs (.NET, Node.js, Python, Java) to:
- Device apps: handle desired property updates and update reported properties.
- Service apps: update tags and desired properties and query devices based on twin values.
These behaviors and patterns are available only in the standard tier of IoT Hub.
References:
- Understand and use device twins in IoT Hub
- How to view and update devices based on device twin properties
- Get started with device twins (programming-language-csharp)
- Get started with device twins (programming-language-node)
- Get started with device twins (programming-language-python)
- Get started with device twins (programming-language-java)