Add attribute to device programmatically with attribute tags

We are trying to create a device dynamically, starting with a base recipe and then adding attributes as needed for a final configuration. We use attribute tags for things such as units so need to be able to programmatically create an attribute that has attribute tags. The workflow node Device:update doesn’t seem to support this with the ‘individual fields’ option, so we are using the payload path option. Our payload is as shown below, and we use data.patch as our payload path to the Device:update node.

{
"deviceId": "5ee3d2ca1a9f6d00067f7292",
"patch": {
	"attributes": [{
		"name": "Hopper-Level-1",
		"dataType": "number",
		"description": "West Silo HI Hopper Level",
		"attributeTags": {
			"typeInt": "1",
			"sourceAttribute": "ch0mv",
			"eu": "Level",
			"displayMin": "LO",
			"displayMax": "HI"
			}
		}
		]
	}
}

This works without error, but results in all existing attributes in the device being deleted. Is there somewhere this is specified in the documentation, and how can we add an attribute with tags without wiping out all other attributes already defined. We need this to be bulletproof, can’t have any possibility of attributes being deleted.

Any solution you have is appreciated…

Hi @Edward_Cline,

To use the Device: Update node, you will need to provide all of the attributes and attribute tags, along with the new ones you’d like to add, to the payload path.

You also have the option of using the Losant API Node with the Devices: Patch Resource & Action.

I am currently working on an example for you, and will update when I have that ready!

Thank you,
Heath

@Edward_Cline,

Here’s a workflow example that is able to programmatically add attributes (and attribute tags) to a device without removing the already defined attributes.

The workflow is triggered by a Virtual Button which sends a JSON payload. Here’s what that payload looks like:

{
    "deviceIDToUpdate": "5ee413c062971c00072eb425",
    "addAttributes":
    [
        {
            "name": "attribute1",
            "dataType": "number",
            "attributeTags": 
            {
                "tag1": "tag1Value",
                "tag2": "tag2Value"
            }   
        },
        {
            "name": "attribute2",
            "dataType": "number",
            "attributeTags": 
            {
                "tag3": "tag3Value",
                "tag4": "tag4Value"
            }   
        }
    ]
}

The workflow then uses the Device: Get node to retrieve the device information that is passed in as deviceIDToUpdate. It then uses the Mutate node to add a field to the payload that will act as a sort of staging area for the updates, and then uses the Loop Node to loop through the addAttributes field. Finally, it ends with a Device: Update node where the attributes are then updated for the device.

I’ve included the workflow zip below. Please let me know if you have any questions!

export-5ee4134262971c00072eb421-1592156412923-updateAttributeAndAt.zip (3.9 KB)

Thank you,
Heath

Looks great, thanks. I’ll give it a try…

Hi, im sorry but how can insert this worfklow inside of my org. Im new developing in Losant, I tried but couldn´t because of their extension file

The .zip linked above is an application - not a workflow. This is because the inclusion of a test device is necessary to demonstrate this example.

To import an application:

  1. Go to your Organization’s overview page (go here and click on the name of your Organization)
  2. Under the Applications section, click the “Import …” button on the top right
  3. Select the .zip file

You can then test out the attribute tag functionality and copy over relevant components to your real application. Please give this a try and let us know how it goes!

1 Like

thank you so much sebas!