Hi @Juanhe Shen I understand that we are still waiting on getting more feedback on this issue. I am providing the below response outlining the general steps to be followed for updating a digital twin to help other community members looking running into a similar issue.
You can find the detailed simulation sample on how to update a digital twin from IoT Hub data on this link Tutorial: Build out an end-to-end solution. The tutorial walks you through the steps of updating a digital twin from an Auzre function using .Net DigitalTwinsClient Class. The class offers a method UpdateDigitalTwinAsync which accepts the Digital twin Id that needs to be updated and the JSON patch document contains the overwritten values. Below is a sample from Azure Event Grid function which picks data from Event Grid and pushes a telemetry value to a property of Digital twin.
try
{
log.LogInformation(eventGridEvent.Data.ToString());
JObject deviceMessage = (JObject)JsonConvert.DeserializeObject(eventGridEvent.Data.ToString());
string deviceId = (string)deviceMessage["systemProperties"]["iothub-connection-device-id"];
var temperature = deviceMessage["body"]["Level_of_Material"];
log.LogInformation($"Device:{deviceId} Level_of_Material is:{temperature}");
//Update twin using device temperature
var updateTwinData = new JsonPatchDocument();
updateTwinData.AppendReplace("/Level_of_Material", temperature.Value<double>());
await client.UpdateDigitalTwinAsync("deviceId", updateTwinData);
}
catch(Exception ex){
Console.WriteLine($"Error : {ex.Message}");
}
Please make sure you are targeting the correct device Id and property name of the Digital Twin for the update to succeed.
Once you decide to go with the updating a digital twin with the relationship and still experience issues, please feel free to tag us in the comments below and we would be happy to assist you.
If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.