Mangle MQTT Messages

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.

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.

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.

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:

[{"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"]]}]


Johannes

Spend some time reading and digesting the docs page working with messages, it will give you a lot of useful information including the answer to this question I think.

1 Like

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

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.

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.

1 Like

suggestions for improvements to docs are always appreciated.

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