How do I update multiple digital twin components using Azure Function in python ?

Vishwajit Vipat Dalve 20 Reputation points
2023-05-06T14:54:47.45+00:00

I am fetching the data from IoT hub and updating the the digital twin using the azure event grid trigger. I am able to update all the components of the digital twin but these updates are not fast it gives me a latency of 18-25 seconds. I am using the update_component() function to update one component at a time, since I was not able to find any function that can update the entire digital twin, because of this latency I am unable to do real-time analytics. Please help.

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
219 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,265 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
314 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde 28,386 Reputation points MVP
    2023-05-06T18:19:45.7333333+00:00

    Hello @Vishwajit Vipat Dalve ,

    this latency seems very slow.

    Can you confirm the same latency using Azure Logic Apps or Azure Functions (in this case, written in C#)?

    Another problem could be that there are failures while ingesting messages.

    Please check if there are failures or other anomalies:

    User's image


    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.


1 additional answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 27,386 Reputation points
    2023-05-19T07:11:00.0066667+00:00

    @Vishwajit Vipat Dalve Greetings!

    Did you get a chance to see Sander's suggestions? In addition to it, please see below information and let us know if that helps.

    I am able to update all the components of the digital twin but these updates are not fast it gives me a latency of 18-25 seconds. I am using the update_component() function to update one component at a time

    I understand that you would like to update multiple digital twin components. After creating or updating a twin, there may be a latency of up to 10 seconds before the changes will be reflected in queries.

    Did you try updating multiple components of a digital twin by making multiple calls to the update_component() function? If not, Please try with the suggestion and see if that helps.

    from azure.digitaltwins.core import DigitalTwinsClient
    from azure.identity import DefaultAzureCredential
    
    # Create a DigitalTwinsClient instance
    credential = DefaultAzureCredential()
    client = DigitalTwinsClient("<your-digital-twins-url>", credential)
    
    # Define the digital twin ID and components to update
    digital_twin_id = "<your-digital-twin-id>"
    components = {
        "Component1": {
            "Property1": "Value1",
            "Property2": "Value2"
        },
        "Component2": {
            "Property3": "Value3",
            "Property4": "Value4"
        }
    }
    
    # Update the digital twin components
    for component_id, component in components.items():
        client.update_component(digital_twin_id, component_id, component)
    
    

    Reference: Update digital twin components

    By using a loop to make multiple calls to the update_component() function, you can update multiple components of a digital twin with the Azure Digital Twins Python SDK.

    If you are ok to use API, please check update a component in a digital twin (multiple updates)

    Sample request:

    PATCH https://digitaltwins-hostname/digitaltwins/myTwinId/components/myComponent?api-version=2023-02-27-preview
    
    [
      {
        "op": "add",
        "path": "/property1",
        "value": 1
      },
      {
        "op": "replace",
        "path": "/$metadata/property1/sourceTime",
        "value": "2022-05-31T12:00:01.000125009Z"
      },
      {
        "op": "remove",
        "path": "/property2"
      },
      {
        "op": "replace",
        "path": "/property3/subProperty1",
        "value": "new value"
      }
    ]
    

    I hope this helps! Let us know if you have any further questions.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.