# Using state messages to update multiple tags

**URL:** <https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825>\
**Category:** Help\
**Tags:** workflow, tags\
**Created:** [April 14, 2020, 4:09pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825 "2020-04-14T16:09:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zach\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@Zach\_Jacobs](https://forums.losant.com/u/Zach_Jacobs)\
**Post date:** [April 14, 2020, 4:09pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/1 "2020-04-14T16:09:33Z")

</div>

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.

 ![firefox_Q3qoHiPOj9](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/f/f6f1e45245e7b4825500d3b1d4241b648a0e1c68.png).

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](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/7/72f7b6490a2c50d6409e53c3f473d91dee4a6341.png)

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [April 14, 2020, 5:32pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/3 "2020-04-14T17:32:16Z")

</div>

Hi @Zach_Jacobs,

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

Let’s dive in! Are you currently [reporting state to your device](https://docs.losant.com/devices/state/)? Are you using [device attributes](https://docs.losant.com/devices/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](https://docs.losant.com/mqtt/overview/#publishing-device-state). You can see what the state payload should look like [here](https://docs.losant.com/mqtt/overview/#state-payload-form). Are you reporting state on this topic and looking for a way to additionally update the tags?

Thanks!  
Julia

---

<div class="post-metadata">

**Author:** ![Zach\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@Zach\_Jacobs](https://forums.losant.com/u/Zach_Jacobs)\
**Post date:** [April 14, 2020, 7:13pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/5 "2020-04-14T19:13:38Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [April 15, 2020, 2:12pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/7 "2020-04-15T14:12:13Z")

</div>

@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](https://docs.losant.com/workflows/logic/loop/). Within the Loop Node, you would likely do a device lookup with the [Device: Get Node](https://docs.losant.com/workflows/data/get-device/) in order to retrieve your devices matching your tag query. You would then use the [Device: Update Node](https://docs.losant.com/workflows/data/update-device/) to update the tags for your devices.

**Schema**  
The [Losant API Node](https://docs.losant.com/workflows/data/losant-api/) includes the [Device PATCH resource](https://docs.losant.com/rest-api/devices/#patch) which allows you to build a schema to update tags in bulk. The schema looks like so:

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![Zach\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@Zach\_Jacobs](https://forums.losant.com/u/Zach_Jacobs)\
**Post date:** [April 15, 2020, 2:17pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/8 "2020-04-15T14:17:44Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![Zach\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@Zach\_Jacobs](https://forums.losant.com/u/Zach_Jacobs)\
**Post date:** [April 15, 2020, 4:11pm UTC](https://forums.losant.com/t/using-state-messages-to-update-multiple-tags/2825/9 "2020-04-15T16:11:55Z")

</div>

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