Using state messages to update multiple tags

I currently use a workflow to receive Device:State messages that are filtered by an attribute named “request”. This looks for incoming messages with a “request” in the payload and depending on the type of request, it triggers a “Device: Update” block to update an individual DeviceTag or responds to the request with a “Device: Command” block to send data back to the Device. This works quite well but I would like to expand the functionality a bit. I would like the device itself to be able to update it’s own DeviceTags. I can already do this with individual tags but I want to expand to be able to support a wider range of incoming DeviceTags. I am currently trying to do this with the following incoming state message.

.

As seen in the image, I receive a “request”:“device_update_tags_req” with a “tag” object that includes all of the keys that I’d like to update. I’m not able to decode this in a way that’s usable and I really don’t understand the jsonEncode or stringify functions. I tried the jsonEncode blocks and it gets encoded as:

“json_object”:""[object Object]""

I have two questions:

  1. How can I parse this [object Object] into a usable JSON object?
  2. How can I generically update tags using a “Device: Update” block? Something like this where it pulls all the key names from the decoded json object and uses that key name as a template variable:
    image

Hi @Zach_Jacobs,

I just have a couple of clarifying questions and comments to allow me to assist you further. :smile:

Let’s dive in! Are you currently reporting state to your device? Are you using device attributes or are you exclusively utilizing device tags?

It’s also important to know the /state topic is used exclusively for publishing device state. You can read a bit more about this within our documentation. You can see what the state payload should look like here. Are you reporting state on this topic and looking for a way to additionally update the tags?

Thanks!
Julia

@JuliaKempf,
Thanks for the quick response!

Yes, I am successfully using the state topic to update device attributes as well. I am looking for an additional way to update the tags. The state topic seemed to be the only way for me to figure out how to ingest messages into a workflow block for me to use. Is the best way to do this to create a custom topic and ingest using an MQTT block? If that’s the case I’d still like to be able to auto add incoming key value pairs as tags.

1 Like

@Zach_Jacobs awesome, thank you for the information!

You have two options here for bulk updating device tags. You can either:

  • Construct a loop within the workflow and utilize Device nodes
  • Build a schema and update using the API and PATCH

Let’s jump into the specifics of each.

Loop
To create this logic in a workflow you would need to use a Loop Node. Within the Loop Node, you would likely do a device lookup with the Device: Get Node in order to retrieve your devices matching your tag query. You would then use the Device: Update Node to update the tags for your devices.

Schema
The Losant API Node includes the Device PATCH resource which allows you to build a schema to update tags in bulk. The schema looks like so:

{
  "updateFields": {
    "tags": [
      {
        "key": "TagKey",
        "value": "TagValue"
      }
    ],
  },
  "deviceIds": [
    "575ecec57ae143cd83dc4a9f",
    "575ecec57ae143cd83dc4a9e",
    "575ecec57ae143cd83dc4a9a"
  ]
}

This would allow you to update in bulk, but you will have to create the logic to build the schema properly.

Let me know if I can assist further or if you have any additional questions.

Thanks!
Julia

@JuliaKempf ,
That’s great! I was just typing out a response. I’m now ingesting the MQTT messages on a custom topic and decoding that into a proper JSON object. I have triggered a Device: Get block to get the device based on the incoming relayID so I have a reference to the actual device tags. I just need to ripple through the incoming “tags” and look for those existing keys on my device object and update them. I try both methods that you mentioned to see which works best for me

Thanks!

I got it working with the loop node. That is a huge help! Thanks so much!

1 Like