Search code examples
azureazure-iot-hub

Is it possible to update a single nested property on a DeviceTwin such that the siblings lastUpdated property is not updated?


I have a device property that looks like this:

"{ Status : { Property1: "111", Property2: "222" } }"

How do I update just Property2 using the C# TwinCollection class ?

I I use the code below then "Property2" properties lastUpdated value changes even though it was not updated.

   var status = new TwinCollection();
   status["Property1"] = "111";
   
   var reportedProperties = new TwinCollection();
   reportedProperties["status"] = status;

   await deviceClient.UpdateReportedPropertiesAsync(reportedProperties);

Solution

  • This answer is made with the assumption that you mean "update Property1", because that's what your code sample states.

    I just created a new West Europe IoT hub, created a new device with Device Twin that you specified (with a lowercase 's' in 'Status', your code specifies a lower case, but your question mentioned an uppercase).

    I then ran your code, and the resulting metadata is:

    "$metadata": {
        "$lastUpdated": "2023-08-20T10:05:51.8902538Z",
        "status": {
            "$lastUpdated": "2023-08-20T10:05:51.8902538Z",
            "Property1": {
                "$lastUpdated": "2023-08-20T10:05:51.8902538Z"
            },
            "Property2": {
                "$lastUpdated": "2023-08-20T10:05:40.8567787Z"
            }
        }
    }
    

    Note that the $lastUpdated changed in all instances, but not for Property2.