1
votes

I am using Azure Device Twin in Azure IOT Hub and this issue is regarding the Device Twin behevior.

I have a DeviceTwin Structure as follows and I am using pure MQTT protocal to publish data into it.

The topic which I used to publish twin data is : $iothub/twin/PATCH/properties/reported/?$rid=c1a12cc8-4168-4e16-a1bb

The payload I sent is :

{
  "deviceId": "34aa078e",  
  "properties": {
    "desired": {

    },
    "reported": {
      "notifications": {
        "notification1": {
          "primaryCode": "crprim1",          
          "statusChangeTimestamp": 1507115005615
        },
        "notification2": {
          "primaryCode": "crprim2",          
          "statusChangeTimestamp": 1507117507027
        }
      },
      "location": {

      }      
    }
  }
}

All the functionalities are working properly as documented in the DeviceTwin documentation but, I have one clarification to be cleared with respect to the behevior of this DeviceTwin.

When I send a message payload containing one new notification (named as notificaion3) through MQTT to update above DeviceTwin, it just add notification3 into the notifications object instead of just replacing entire notifications content with notification3.

MQTT payload which I sent:

{
    "notifications": {
        "notification3": {
          "primaryCode": "crprim3",          
          "statusChangeTimestamp": 1607115005615
        }
    }
}

So I will be ultimately have following in the DeviceTwin structure,

{
  "deviceId": "34aa078e",  
  "properties": {
    "desired": {

    },
    "reported": {
      "notifications": {
        "notification1": {
          "primaryCode": "crprim1",          
          "statusChangeTimestamp": 1507115005615
        },
        "notification2": {
          "primaryCode": "crprim2",          
          "statusChangeTimestamp": 1507117507027
        },
        "notification3": {
          "primaryCode": "crprim3",          
          "statusChangeTimestamp": 1607115005615
        }
      },
      "location": {

      }      
    }
  }
}

Instead of following,

{
  "deviceId": "34aa078e",  
  "properties": {
    "desired": {

    },
    "reported": {
      "notifications": {        
        "notification3": {
          "primaryCode": "crprim3",          
          "statusChangeTimestamp": 1607115005615
        }
      },
      "location": {

      }      
    }
  }
}

But Device Twin should contains the latest snapshot of a given device and it should not keep the previous data (with respect to a object level). Is this the usual behavior of Azure Device Twin ? Or is it some kind of a bug ?

2

2 Answers

0
votes

Your device twin behavior based on the above description is not correct. You should see all notifications properties (1,2,3).

You can use 3rd party tools such as MQTTBox Client, iotdevtool.com, Azure IoT Hub Tester, etc. to see the device twin behavior.

The following screen snippets show this test in my Azure IoT Hub region:

Step 1: adding the notification1 and notification2 into the device2 twin reported notifications properties:

mqtt1

Step 2: Get the device2 twin and add the notification3 into the reported notifications properties:

mqtt2

Step 3: Get the device2 twin to see all notifications properties enter image description here

0
votes

It is not a bug. For detailed information about this behavior of Azure Device Twin, you can reference this document.

Based on your description, you want to update one reported property continuously. Here we call it as "notification1". So your MQTT payload should like this:

{
    "notifications": {
        "notification1": {
          "primaryCode": "crprim3",          
          "statusChangeTimestamp": 1607115005615
        }
    }
}

not this:

{
    "notifications": {
        "notification3": {
          "primaryCode": "crprim3",          
          "statusChangeTimestamp": 1607115005615
        }
    }
}

That is keeping the property name unchanged and only update its values.

To remove the other redundant properties you can set its value to NULL like this:

{
    "notifications": {
        "notification2": null,
        "notification3": null
    }
}