# Mangle MQTT Messages

**URL:** <https://discourse.nodered.org/t/mangle-mqtt-messages/24380>\
**Category:** General\
**Created:** [7 April 2020 07:52 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380 "2020-04-07T07:52:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsgottabered](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@itsgottabered](https://discourse.nodered.org/u/itsgottabered)\
**Post date:** [7 April 2020 07:52 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/1 "2020-04-07T07:52:42Z")

</div>

Hi all,

I am struggling to figure out a solution to my problem and I've looked through the forum and googled in general but I can't find the exact thing I need.

I have a power meter which has an internal MQTT broker and I'd like to get the data somehow into InfluxDB. Unfortunately, the format that the power meter outputs is not easily digestible by Influx. This is what I get out of the power meter:

[1586229942, 4405.360, "W"]

I would normally configure telegraf to consume these messages but it's not a nice key:value format. I'd like to use node-red to convert these messages then spit them back out to MQTT for telegraf to consume (I realise I could probably go straight to influx with node-red but it's a learning experience)

I have an MQTT-in node and MQTT-out, but I don't know what function to put in the middle to turn the 'array' that I have now into an 'object' more like:

{"timestamp":1586245760,"value":676,"unit":"W"}

If I set the MQTT input to output 'a parsed JSON object' it parses it as an array in the debug window, but I don't know how to manipulate those array values.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 April 2020 08:03 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/2 "2020-04-07T08:03:07Z")

</div>

Ok,

I don't use `telegraph` so I can't comment on that.

So, you have the `MQTT IN` node (from the meter) and you want to send it out.  
But I'm confused.

Do you want to send it out with `MQTT` or `telegraph`?

You access the array by their name.

On the right side of the screen click the little triangle to expand the array.

 ![Screenshot from 2020-04-07 17-59-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d8fb943e00edfe359d7b76a8c7c3c05913a35ae1.png)

 ![Screenshot from 2020-04-07 17-59-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fee43dd913c1933c1b4f27bcc334891236546fb9.png)

Click on the `copy path` and that will give you the _path_.

In the example shown it is `msg.payload`

The line above is `msg.topic`

Hope that helps.

You then may need to use javascript in a `function` node to get your desired result.

Or you could use a `change` node.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [7 April 2020 08:15 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/3 "2020-04-07T08:15:24Z")

</div>

You can use a change node to access array elements in the msg object and use the same node to assign those values to object keys:

```auto
[{"id":"48d5ba88.297b24","type":"inject","z":"ed7ded5f.9a395","name":"","topic":"","payload":"[1586229942,4405.36,\"W\"]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1070,"y":660,"wires":[["7aef1d54.408efc"]]},{"id":"cb4bfb19.226518","type":"debug","z":"ed7ded5f.9a395","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1430,"y":660,"wires":[]},{"id":"7aef1d54.408efc","type":"change","z":"ed7ded5f.9a395","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"original","tot":"msg"},{"t":"set","p":"payload.timestamp","pt":"msg","to":"original[0]","tot":"msg"},{"t":"set","p":"payload.value","pt":"msg","to":"original[1]","tot":"msg"},{"t":"set","p":"payload.unit","pt":"msg","to":"original[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1240,"y":660,"wires":[["cb4bfb19.226518"]]}]

```

 ![Screenshot 2020-04-07 at 10.13.44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a8fe5fb494a430223a94dcef4c0600292d3e2df.png)  
Johannes

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 April 2020 08:25 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/4 "2020-04-07T08:25:18Z")

</div>

Spend some time reading and digesting the docs page [working with messages](https://nodered.org/docs/user-guide/messages), it will give you a lot of useful information including the answer to this question I think.

---

<div class="post-metadata">

**Author:** ![itsgottabered](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@itsgottabered](https://discourse.nodered.org/u/itsgottabered)\
**Post date:** [7 April 2020 09:46 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/5 "2020-04-07T09:46:47Z")

</div>

@JGKK that's exactly what I needed. Armed with that knowledge I have got it doing exactly what I need now. As it would happen I did look at the change node earlier but I didn't know how to reference the array elements. Thank you!

Colin, I don't love that working with messages page, it doesn't adequately explain what I needed.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 April 2020 09:53 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/6 "2020-04-07T09:53:36Z")

</div>

> [@itsgottabered](#):
>
> it doesn't adequately explain what I needed.

You'd be pushed to find any document helping one person with a unique and individual requirement.  
Its more about understanding what a msg is, how to view it in debugger, how to select an element (and copy it's path)

You may not like it's format but it's essential info for helping you to help yourself.

I recommend you give it another read and try to digest it - there is enough there to help you solve this kind of issue.

Good luck.

---

<div class="post-metadata">

**Author:** ![itsgottabered](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@itsgottabered](https://discourse.nodered.org/u/itsgottabered)\
**Post date:** [7 April 2020 09:58 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/7 "2020-04-07T09:58:19Z")

</div>

> [@Steve-Mcl](#):
>
> You may not like it's format but it's essential info for helping you to help yourself.

Appreciated, I guess I should have said it didn't work 'for me'. Now that I've seen a working example, the documentation makes more sense.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 April 2020 11:28 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/8 "2020-04-07T11:28:57Z")

</div>

suggestions for improvements to docs are always appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 April 2020 11:28 UTC](https://discourse.nodered.org/t/mangle-mqtt-messages/24380/9 "2020-04-21T11:28:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
