Add device attribute using Device: patch

How can I set device attributes without loosing my current device attributes using the API Node Device: patch (or Devices: patch)?
I did this:

But its not working when the attribute doesn’t exists a priori.

let { key } = payload.value;

let indicatorsQuery = {
    "email": payload.experience.user.email,
    "query": {
        "id": payload.data.params.machineId,
    },
    "updateOperations": [
        {
            "operation": "set",
            "target": "attributeTag",
            "value": {
                "key": 'type',
                "value": 'indicator'
            },
            "attributeName": key
        }
    ],
}

payload.indicators = indicatorsQuery;

“indicators” is the input of Devices: patch node.

Two ways to add, edit, or remove attributes from a device via the REST API:

  1. Device: Patch - This method requires passing up the entire array of attributes with the request - as in, you must first fetch the device, operate on the correct array item, and then make the patch request with the full edited array in the body.
  2. Devices: Patch - This is the bulk update endpoint (for updating multiple devices at once), but you can use it for your case as well.

I just tested out your example and it worked for me; can you verify the value of “key” is the name of an attribute on the device with the ID found at “data.params.machineId”? What is the response you are getting from the operation (if you put a payload path in the “Response” section and add a Debug Node after)?

1 Like

No, the value of “key” isnt the name of an attribute, but that is the point. I want to add the attribute when the attribute doesn’t already exists.

I know that what I was doing was setting attribute tags, but my idea is to add attributes. I was just trying to do this using the updateOperations because I was thinking: what if i want to add attribute with attribute tags when the attribute doesnt exists a priori?

In that case, the issue is you are using the wrong operation; in your code sample, you are attempting to add an attribute tag to an existing attribute, not adding a new attribute to the device.

Instead, the item you pass in the “updateOperations” array should look like the following:

{
  "operation": "add",
  "target": "attribute",
  "value": {
    "name": "nameOfNewAttribute",
    "dataType": "number|string|gps|bool|blob",
    "attributeTags": {
    }
  }
}

Nice Dylan, that will not overwrite/remove my current device attributes, right?

No, following that syntax it will only add the attribute and will leave the remaining ones in place.

When it comes to building these bulk update queries, I like to create the operation first in the user interface, submit the request, and then view how it is shaped in the browser’s “Network” tab. That’s what I did in this case as well. Just a tip for working with this endpoint in the future.

1 Like

This attributeTags object have the entries key and value?

For example:

"attributeTags": {
    "key": key,
    "value": value
}

Yepp, that is exactly what i’m doing.
My interface have a form that the user set this attributes and values, so firstly the attributes doesnt exists.

No, attributeTags should be shaped as:

{
  "attributeTags": {
    "key1": "value1",
    "key2": "value2"
  }
}