Can a group label be set for a device with Cloud API?

With the Cloud API, a name can be set for a specific device. Is it possible to set a group label?

I could not find it in the doc or the community, but maybe there is a trick to it?

Yes, it’s a POST to the product device endpoint.

This is confusing to do with curl because the syntax for sending the array is weird. It’s more logical from JSON because the groups field of the device is a JSON array.

Thanks, I tried assigning af group name “new” to a device with a webhook, in a way that works when assigning a name, but I get an error response.

{
    "event": "SETgroup",
    "url": "https://api.particle.io/v1/devices/{{PARTICLE_DEVICE_ID}}?access_token=0987654321",
    "requestType": "PUT",
    "noDefaults": true,
    "rejectUnauthorized": true,
    "json": {
        "groups": "{{PARTICLE_EVENT_VALUE}}"
    }
}

Response

particle-internal hook-response/SETgroup/0 "data":"{\"error\":\"Nothing to do?\"}

I tried replacing `“{{PARTICLE_EVENT_VALUE}}” with “new” directly. And used the webhook reference for writing the one item as an array, but it gets compressed to an item as above anyway?

Groups needs to be an array, so:

{
    "event": "SETgroup",
    "url": "https://api.particle.io/v1/devices/{{PARTICLE_DEVICE_ID}}?access_token=0987654321",
    "requestType": "PUT",
    "noDefaults": true,
    "rejectUnauthorized": true,
    "json": {
        "groups": [{{PARTICLE_EVENT_VALUE}}]
    }
}

However, it sets the value, does not add to it, so it will replace any previously set groups.

Also, make sure you double-quote the value in the publish string. You could also send multiple groups, comma-separated, with that webhook:

"group1","group2"

When copying the above to the “custom webhook” field, I get a “bad string”, on the “groups” line. So I added quotes, but still get the “nothing to do” response:

{
    "event": "HLP1group",
    "url": "https://api.particle.io/v1/devices/{{PARTICLE_DEVICE_ID}}?access_token=d509846f7obfuscated3f1b5bd9f4050azp6ga52",
    "requestType": "PUT",
    "noDefaults": true,
    "rejectUnauthorized": true,
    "json": {
        "groups": "[{{PARTICLE_EVENT_VALUE}}]"
    }
}
    Particle.publish("HLP1group", "\"new\"", PRIVATE, WITH_ACK);

Event

{
  "name": "HLP1group",
  "data": "\"new\"",
  "published_at": "2022-06-23T09:36:58.374Z",
  "coreid": "e00fremoved",
  "userid": "58d1removed",
  "version": 0,
  "public": false,
  "productID": 1234
}

Response

HTTP/1.1 200 OK
Date: Thu, 23 Jun 2022 09:36:58 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 26
Connection: keep-alive
X-Request-Id: 928b6ce72e4509d8bddd47881515024a
Access-Control-Allow-Origin: *

{"error":"Nothing to do?"}

The single item array seems to dismissed?

I went over the webhook reference again, and can’t find a way around enclosing the array (mustache or not) in quotes. I guess that makes it a string, where the server expects an array?

It should be like this, without the quotes. The editor will flag is as an error, but it will still allow you to save it, and it should work. The problem is that the syntax as-is is not valid JSON, but it is once the mustache template is resolved.

"json": {
        "groups": [{{PARTICLE_EVENT_VALUE}}]
    }

The custom template editor does not actually let me save it.

Removing quotes and clicking save, get a “succesful” message, but that view shows the quotes are back. Going back into the custom template, the quotes are indeed back.

Making a new webhook, pasting correct code into the custom template, the create webhook button is grayed out and cannot be pressed.

Instead I then chose to edit with web builder, advanced and removed quotes there, it saves ok and looks fine there, but running it, I got a 400 back with

{"ok":false,"error":"Unexpected token & in JSON at position 15"}

Looking in custom template, the line in question reads

    "json": "{\n  \"groups\": [{{PARTICLE_EVENT_VALUE}}]\n}"

So in the background that element has been enclosed in quotes and existing quotes have been escaped.

In webbuilder edit I removed the new lines by placing all the code in one line and click save. It looks ok there:

{"groups": [{{PARTICLE_EVENT_VALUE}}]}

But still get the same fault (now position 12?), and in the custom template it looks like this:

    "json": "{\"groups\": [{{PARTICLE_EVENT_VALUE}}]}"

When edited it in the custom template editor, to remove the extra quotes and escaping, again it will appear to be saved, but going back in, the editor kept the version without the edits.

Pasting correct code in a new webhook in the web builder editor, results in the same.

(I thought to use CLI for this, but according to docs, creating is only for user webhooks. And as the intended code is not correct json at the outset, I guess it would go wrong anyway.)

So as a work-around I tried hardcoding the group name “new”, but then get a 500:

{"ok":false,"code":500,"errors":["Cannot read property 'getGroupNames' of undefined"]}

Viewing in custom template editor, it looks like this:

{
    "event": "HLP1group",
    "url": "https://api.particle.io/v1/devices/{{PARTICLE_DEVICE_ID}}?access_token=d50removed",
    "requestType": "PUT",
    "noDefaults": true,
    "rejectUnauthorized": true,
    "json": {
        "groups": [
            "new"
        ]
    }
}

To me it is valid json with an array with one item as expected?

The request was:

PUT /v1/devices/e00fremoved?access_token=d509removed HTTP/1.1
User-Agent: ParticleBot/1.1 (https://docs.particle.io/webhooks)
host: api.particle.io
accept: application/json
content-type: application/json
content-length: 18
Connection: keep-alive

I can work with hardcoding for now, if this could be made to work.

Oh, sorry, now I see what’s wrong. You’re using the individual device endpoint. Groups are only a feature of products, not individual devices, so you can only set it on the product endpoint:

https://api.particle.io/v1/products/1234/devices/{{PARTICLE_DEVICE_ID}}

Ah, yes I see now. Makes sense.

Thanks @rickkas7 that works well now with “new” directly in the webhook :+1:

1 Like