2

I have an IoT board that was hosting an application with a set of properties that it reported. I then loaded a new application onto that board. Now the twin properties update fails with a 400 error. Here are the existing properties from the previous application:

{
  "deviceId": "xxxxxx",
  "etag": "AAAAAAAAABI=",
  "deviceEtag": "ODkzNzc0Mjcx",
  "status": "enabled",
  "statusUpdateTime": "0001-01-01T00:00:00",
  "connectionState": "Connected",
  "lastActivityTime": "2019-07-25T14:49:57.9764857",
  "cloudToDeviceMessageCount": 0,
  "authenticationType": "sas",
  "x509Thumbprint": {
    "primaryThumbprint": null,
    "secondaryThumbprint": null
  },
  "version": 34,
  "properties": {
    "desired": {
      "pmt_mqttMon_sec": 120,
      "sup_badBootLimit": 5,
      "sup_badBootTimeout_sec": 1200,
      "sup_enabled": 1,
      "pmt_enabled": 1,
      "pub_enabled": 1,
      "pub_mqttMaxLen": 2500,
      "pub_maxWriteFails": 3,
      "ping_period": 60,
      "ping_state": 3,
      "storage_enabled": 1,
      "sys_pollPeriod_sec": 60,
      "sys_type": "2",
      "sys_numBatt": 0,
      "gen_manualControl": 0,
      "sys_rectStopChargeSoc_pct": "",
      "sys_genStartSoc_pct": 62.5,
      "sys_genStartVoltage_V": 44.5,
      "sys_rectFloatHigh_V": 57.7,
      "sys_rectFloatLow_V": 53.7,
      "sys_modbusBaudRate": "19200",
      "rlogger_level": 1,
      "$metadata": {
        "$lastUpdated": "2019-07-24T23:31:35.4433452Z",
        "$lastUpdatedVersion": 18,
        "pmt_mqttMon_sec": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sup_badBootLimit": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sup_badBootTimeout_sec": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sup_enabled": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "pmt_enabled": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "pub_enabled": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "pub_mqttMaxLen": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "pub_maxWriteFails": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "ping_period": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "ping_state": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "storage_enabled": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_pollPeriod_sec": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_type": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_numBatt": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "gen_manualControl": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_rectStopChargeSoc_pct": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_genStartSoc_pct": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_genStartVoltage_V": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_rectFloatHigh_V": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_rectFloatLow_V": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        },
        "sys_modbusBaudRate": {
          "$lastUpdated": "2019-07-24T23:31:35.4433452Z",
          "$lastUpdatedVersion": 18
        },
        "rlogger_level": {
          "$lastUpdated": "2019-07-24T23:22:14.8853676Z",
          "$lastUpdatedVersion": 17
        }
      },
      "$version": 18
    },
    "reported": {
      "sup_badBootLimit": 5,
      "sup_badBootTimeout_sec": 1200,
      "sup_enabled": 1,
      "rlogger_level": 1,
      "storage_enabled": 1,
      "pmt_enabled": 1,
      "pmt_mqttMon_sec": 180,
      "pub_enabled": 1,
      "pub_mqttMaxLen": 2500,
      "pub_maxWriteFails": 3,
      "ping_period": 60,
      "ping_state": 3,
      "sys_pollPeriod_sec": 60,
      "sys_type": 0,
      "sys_numBatt": 0,
      "gen_type": 1,
      "gen_manualControl": 0,
      "sys_rectStopChargeSoc_pct": 85,
      "sys_genStartSoc_pct": 62.5,
      "sys_genStartVoltage_V": 44.5,
      "sys_rectFloatHigh_V": 57.7,
      "sys_rectFloatLow_V": 53.7,
      "sys_modbusBaudRate": 9600,
      "sys_devices": "[{\"class\":\"ncu\",\"devId\":0,\"mbid\":1,\"ipAddr\":\"\",\"regOff\":0}]",
      "": "[{\"class\":\"ncu\",\"devId\":0,\"mbid\":1,\"ipAddr\":\"\",\"regOff\":0}]",
      "$metadata": {
        "$lastUpdated": "2019-04-25T00:13:19.9436642Z",
        "sup_badBootLimit": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sup_badBootTimeout_sec": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sup_enabled": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "rlogger_level": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "storage_enabled": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "pmt_enabled": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "pmt_mqttMon_sec": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "pub_enabled": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "pub_mqttMaxLen": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "pub_maxWriteFails": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "ping_period": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "ping_state": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_pollPeriod_sec": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_type": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_numBatt": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "gen_type": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "gen_manualControl": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_rectStopChargeSoc_pct": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_genStartSoc_pct": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_genStartVoltage_V": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_rectFloatHigh_V": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_rectFloatLow_V": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_modbusBaudRate": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "sys_devices": {
          "$lastUpdated": "2019-04-24T23:15:31.2402457Z"
        },
        "": {
          "$lastUpdated": "2019-04-25T00:13:19.9436642Z"
        }
      },
      "$version": 16
    }
  },
  "capabilities": {
    "iotEdge": false
  }
}

The device then tries to update the reported values via this JSON:

{
    "sup_badBootLimit": 5,
    "sup_badBootReset_s": 1200,
    "sup_enabled": 1,
    "rlogger_level": 1,
    "storage_enabled": 1,
    "pmt_enabled": 1,
    "pmt_mqttMon_sec": 120,
    "pub_enabled": 1,
    "pub_mqttMaxLen": 2500,
    "pub_maxWriteFails": 3,
    "ping_period": 60,
    "ping_state": 3,
    "sys_modbusBaudRate": 19200,
    "sys_readSensors": 0,
    "sys_pollPeriod_sec": 60,
    "sys_type": 2,
    "sys_devices": "[{\"class\":\"gridmeter\",\"deviceId\":0,\"modbusId\":6,\"ipAddress\":\"\",\"regOff\":0},{\"class\":\"gridmeter\",\"deviceId\":1,\"modbusId\":5,\"ipAddress\":\"\",\"regOff\":0},{\"class\":\"sun2k\",\"deviceId\":0,\"modbusId\":7,\"ipAddress\":\"\",\"regOff\":0}]",
    "sys_limit_power_padding_W": 100,
    "controller_loop_sec": 10
}

but this fails with error code 400, which per some comments indicates that a property name was invalid. When I load the application on a new device without an existing twin record it all works fine.

A few questions come to mind:

  1. How can I clean the twin record completely short of deregistering and re-registering the device? In the node SDK I only see methods to update desired properties.
  2. I can delete a property from desired set by setting its value to null. However, if the new on-device application doesn't know about what properties were used by the previous application, how can it delete the properties? Is there a way to clean out all reported properties?
  3. For some reason one of the reported properties seems to have an empty string as a key (which should be an invalid key, I would think). I wonder if this is causing the problem.
farhadf
  • 1,918
  • 3
  • 19
  • 27

1 Answers1

2

Updating the device twin properties (such as tags, desired and reported) from the service side is possible via the ImportDevices bulk job. You can use a SDK or REST API.

The following line shows an example of the updating a device twin on the Device23 in the import file:

{"id":"Device23", "importMode":"updateTwin", "status":"enabled", "tags":{},"properties":{"desired":{ "key1":123},"reported":{ "key2":null, "key3":"abcd"  } },"capabilities":{"iotEdge":false}}

In the case of wrong reported property name, I do recommend to delete this device and imported back with the same authentication keys and correct device twin properties.

Roman Kiss
  • 7,925
  • 1
  • 8
  • 21