API access to device tags

Is it possible to create and edit the tags of a device using the API? Can this be done from an analysis code?

We are trying to maintain a limited set of state variables for each device and the method of using a dummy device seems a bit cumbersome.

Thanks.

Yes, it is possible.

account.devices.edit(device_id, { tags: [{key: "mytag", value: "myvalue" }] });

You can also get the previous tags of the device, so you don’t erase them:

const device_info = await account.devices.info(device_id);
const tags = device_info.tags;
tags.push({ key: "mytag", value: "myvalue" });
account.devices.edit(device_id, { tags });
1 Like

@vitor Great! Thanks. Quick question, do you see any issues in using tags to set a few state values on the devices?

Thanks.

I don’t see any problem.

In the devices you have two options to setup state values, that is the Tags and the Configuration Parameters of the device.

The tags are more useful to use for blueprint dashboards or when you need to list devices in an analysis by it’s tags. It also shows up the tag in your devices page.

The configuration parameters don’t have these options, but is more managable as each parameter has its own ID for editing, and the information is also available in the payload parser.
Take a look, maybe the configuration parameter can serve well for your purposes.

To edit configuration parameters use this:

account.devices.paramList(device_id); // get all paramaters for the device
account.devices.paramList(device_id, true/false); // get parameters with sent false or true only.

 account.devices.paramSet(device_id, { id: "any_id_you_want", key: "parameterkey", value: "parametervalue", sent: true }); // create or edit the parameter.

 account.devices.paramRemove(device_id, parameter_id); // delete the parameter
1 Like

@vitor The config parameters are good but I am using tags because they can filter actions. It looks like I can only use one tag for a device selector for action. Would be good to be able to use multiple.

Thanks!