Azure IoT module twin synchronization

Malav Vakharia 0 Reputation points
2024-02-02T05:41:13.6166667+00:00

It is possible to synchronize desired properties based on reported properties using .NET SDK? (Scenario will get the reported properties first from the device and then need to synchronize both desired and reported properties.)

Another question is in .NET SDK there is code to update reported properties right so does iotedge runtime automatically synchronize the twin or explicitly have to write code using SDK? @Sander van de Velde , Do you have any suggestions?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,134 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 28,221 Reputation points
    2024-02-02T08:14:59.5366667+00:00

    Malav Vakharia Greetings & Welcome to Microsoft Q&A forum!

    It is possible to synchronize desired properties based on reported properties using .NET SDK? (Scenario will get the reported properties first from the device and then need to synchronize both desired and reported properties.)

    Yes, it is possible to synchronize desired properties based on reported properties using the .NET SDK in Azure IoT Hub. Azure IoT Hub uses device twins to synchronize property values between the device and the Azure IoT Hub application. Device property values use device twin reported properties.

    I would suggest you, check Understand Azure IoT Hub device twins and Azure IoT Hub device twins SDK for .NET for more details.

    These docs provides detailed information on device twins, including how to use them to synchronize desired and reported properties. It also includes code samples and step-by-step instructions on how to work with device twins.

    Another question is in .NET SDK there is code to update reported properties right so does iotedge runtime automatically synchronize the twin or explicitly have to write code using SDK? In Azure IoT Edge, the IoT Edge runtime automatically synchronizes the module twin with the IoT Hub cloud twin. When a module updates its reported properties, the IoT Edge runtime automatically updates the module twin in the IoT Hub cloud twin. Similarly, when the IoT Hub cloud twin is updated with desired properties, the IoT Edge runtime automatically updates the module twin.

    You can use the Azure IoT Hub Device SDK for .NET to update the reported properties of a module twin. When you update the reported properties using the SDK, the IoT Edge runtime automatically synchronizes the module twin with the IoT Hub cloud twin.

    Please refer DeviceClient.UpdateReportedPropertiesAsync Method and Sample for more information.

    Do let me know if that helps or have any further queries.


  2. Sander van de Velde 29,461 Reputation points MVP
    2024-02-02T08:38:47.8266667+00:00

    Hello @Malav Vakharia ,

    You need to write code so you can react on Desired property changes using the SDK by listening to the related event.

    This is true for both Azure IoT Edge modules (having their own module twin) and direct internet-connected devices (using the device twin).

    If you want to 'mirror' desired properties in the reported properties, you also need to do this yourself using code. This what I do always.

    Keep in mind, when the device was disconnected during a property change, this change will not trigger the event later on.

    So, it's also a best practice to actively read the latest desired properties on start up!

    Check out this Python / C# sample for a connected device, explaining all the SDK capabilities.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.