2

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}')
Mehdi Benmoha
  • 3,694
  • 3
  • 23
  • 43
  • are you seeing valid properties in the portal ? can you try via iot hub rest api https://learn.microsoft.com/en-us/rest/api/iothub/service/devices/get-twin – Aravind Aug 04 '21 at 09:26
  • yes they are valid and I am able to get the properties through the device client with python code. I don't mind using the API because I need to handle the authentication, and credentials, the module client is what I really hope using because the authorization is "automagically" handled by azure with the `create_from_edge_environment` method which is not present in device client – Mehdi Benmoha Aug 04 '21 at 09:58

1 Answers1

2

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

  • oh ok, so the documentation is wrong ? because it says you can get both device and module twins. Thanks for you answer, that helped me see where I can find the module twin properties in the portal. Device twins may be good however to configure device scoped configuration.. – Mehdi Benmoha Aug 04 '21 at 12:18
  • 1
    Well. I am the one who's wrong, because I didn't read the whole sentence. The documentation says "Gets the device *or* module twin from the Azure IoT Hub *or* Azure IoT Edge Hub service." So, device if you're in Iot Hub and module if you're in Edge Hub. TBH it's very confusing @Microsoft – Mehdi Benmoha Aug 04 '21 at 12:34
  • Good to add this clarification. – Aravind Aug 04 '21 at 13:24