Search code examples
azureazure-iot-hubazure-iot-edge

Use Iot hub edge module client to get device twin properties


I am trying to fetch device twin properties within an IoT Edge module, and while I am already connected with a module client (aka IoTHubModuleClient), I've found a method called get_twin() in the documentation that says that we can grab device or module twin properties. However, I am getting "empty" properties, here's the result:

{'desired': {'$version': 1}, 'reported': {'$version': 1}}

Which is not what I configured in the azure portal in device twin section. But using the IotHubDeviceClient with a device connection string (that's why I don't want to use that client), I am able to get the right device twin properties.

The code is pretty basic:

    client = IoTHubModuleClient.create_from_edge_environment()
    twin_properties = await client.get_twin()
    logger.debug(f'Twin properties: {twin_properties}')

Solution

  • That is correct Mehdi, when you use the IoTHubModuleClient's get_twin method you are getting the module twin properties, you can add/modify/delete module twin properties independently of device twin properties.

    Please take a look of this article