如何在Cumulocity中通过API为设备添加c8y_Address属性?
Hey there! Let’s figure out how to add the c8y_Address property to your device so it shows up in the asset info widget. I’ll cover both the reliable REST API approach and help troubleshoot your MQTT template issue since that wasn’t working for you.
c8y_Address This is a straightforward way to update your device’s inventory with the address property:
Get your device’s ID
You can find this in the Cumulocity UI (go to the device’s details page—it’s in the URL or under "Identifiers") or fetch it via the API with a GET request to/inventory/managedObjects(filter by your device’s name to narrow results).Update the device’s managed object
Use either aPUTorPATCHrequest—PATCHis safer if you don’t want to overwrite existing device properties:- PATCH Request (Recommended)
This only updates thec8y_Addressfield without touching other data:PATCH /inventory/managedObjects/{deviceId} Content-Type: application/json { "c8y_Address": { "region": "North America", "street": "123 Main St", "territory": "California" } } - PUT Request
Replaces the entire managed object, so include all existing properties you want to keep:PUT /inventory/managedObjects/{deviceId} Content-Type: application/json { "name": "My Industrial Sensor", "c8y_IsDevice": {}, "c8y_Address": { "region": "North America", "street": "123 Main St", "territory": "California" }, // Add any other existing device properties here to avoid losing them }
- PATCH Request (Recommended)
Authenticate your request
Use your Cumulocity credentials (basic auth with username/password, or an OAuth token) to authorize the API call.
Since your custom SMQRT template didn’t work, let’s go through common fixes:
Wrong HTTP method in the template
You mentioned mapping it to aPOSTinventory method—butPOSTcreates a new managed object, not updates an existing one. Change the template to usePUTorPATCHinstead, targeting the existing device’s ID.Incorrect payload structure
Thec8y_Addressneeds to be a nested object, not flat values. Your template’s payload should look like this (using placeholders for your variables):{ "c8y_Address": { "region": "${region}", "street": "${street}", "territory": "${territory}" } }MQTT publish syntax error
The publish command should send a single comma-separated string as the payload, not separate parameters. Correct your code to:client.publish('s/uc/TemplateName', '001,YourRegionValue,YourStreetValue,YourTerritoryValue');Double-check that
TemplateNamematches the exact name of your custom template in Cumulocity.Check device logs
Head to your device’s details in Cumulocity, go to the "Logs" tab, and look for errors related to MQTT message processing—this can tell you exactly where the template is failing.
内容的提问来源于stack exchange,提问作者Katherine Chen

