Search code examples
azure-digital-twins

Create a sensor with SensorDataUnitType


I'm trying to create a new device with sensor(s).

This is the payload I'm sending to create the new device:

{"Name":"DeviceABC","HardwareId":"D4xxx425","SpaceId":"xxxx-xxx-xx-xx-xxx","Status":"Provisioned","CreateIoTHubDevice":false,"Properties":[{"Name":"VendorName","Value":"MyVendor"},{"Name":"VendorDeviceId","Value":"D4xxx19425"},{"Name":"VendorDeviceType","Value":"electricity"}],"Sensors":[{"pollRate":0,"id":null,"dataType":"Json","dataUnitType":"KilowattHourEnergy","deviceId":null,"portType":null,"port":"electricity","spaceId":null,"type":"Classic"}]}

In this case I want to use a built in type. KWh

 {
    "id": 186,
    "category": "SensorDataUnitType",
    "name": "KilowattHourEnergy",
    "disabled": false,
    "logicalOrder": 0,
    "friendlyName": "kWh"
}

The SensorDataType is :

{
    "id": 314,
    "spaceId": "xxxx-xx-xxx-xx-xxxx",
    "category": "SensorDataType",
    "name": "Json",
    "disabled": false,
    "logicalOrder": 0
}

When I do this for other devices without specifying a dataUnitType in the sensor object, it works fine. But as soon as I include it i get this:

{
"error": {
    "code": "400.600.000.000",
    "message": "Invalid datatype/dataunittype combination used on sensor."
}}

Solution

  • There seems to be a naming convention between SensorDataType and SensorDataUnitType. The convention is that the SensorDataUnitType needs to end with the full SensorDataType. In your example you have:

    "dataType":"Json",
    "dataUnitType":"KilowattHourEnergy",
    

    But according to the convention it should be

    "dataType":"Json",
    "dataUnitType":"KilowattHourEnergyJson",
    

    Of course you can't just change KilowattHourEnergy because it's in a system ontology. So to get it working I had to create KilowattHourEnergyJson as a new type.

    I guess the most complete answer would include that in your case the SensorDataType probably shouldn't be JSON but should be Energy.