How to only display part of msg.payload?

I am receiving a message in node red via MQQT and only wish to display part of the payload of the message and am not sure how to do it.

A typical message is;
{"topic":"sofar/status","payload":"State1 = DISCHARGE\nState2 = DISCHARGE\nState3 = DISCHARGE\n","qos":0,"retain":false,"_msgid":"d014745cacc84574"}

I wish to just display what State1 equals

Set the MQTT in node to parse Json as object the read the following...

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Yes, thanks I'd read that, but the part I want is within the payload along with other data.

Sorted by using node-red-contrib-string (node) - Node-RED

[{"id":"22e4ddb4fadf9914","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 2","methods":[{"name":"between","params":[{"type":"str","value":"\\n"},{"type":"str","value":"\\n"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":480,"wires":[["d09aa9c4fadd0e97"]]},{"id":"6c8914db442c25ea","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 1","methods":[{"name":"between","params":[{"type":"str","value":""},{"type":"str","value":"\\n"}]},{"name":"append","params":[{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":520,"wires":[["eefdac59326b8149"]]},{"id":"b2107428bb9504c4","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 3","methods":[{"name":"delLeftMost","params":[{"type":"str","value":"State2"}]},{"name":"between","params":[{"type":"str","value":"\\n"},{"type":"str","value":"\\n"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":560,"wires":[["3556bcc46f45afa2"]]}]

Sorry, i missed the point you were trying to extract key-value pairs from the payload.

I would probably skip the contrib node and do it in a function and generate a proper object...

FUNCTION: split out key-value pairs

const data = msg.payload.split('\n')
const entries = []
data.forEach(function(element) {
    element = element.trim()
    if(!element) { return } // skip empty
    const parts = element.split(" = ")
    entries.push([parts[0].trim(), parts[1].trim()])
});
msg.payload = Object.fromEntries(entries)
return msg;
1 Like

Thank you, that is very neat. (I'm still learning programming, used to do Basic and moved on to C and now continuing onwards ).

1 Like

You will get a lot more out of Node-RED by learning JavaScript, but in the meanwhile, if you explore the capabilities of the core nodes you will find that lots of problems (like yours) can be solved without writing any code.

[{"id":"fba3439d.857828","type":"function","z":"67af0149.01c99","name":"fake mqtt data","func":"msg.payload = \"State1 = DISCHARGE\\nState2 = DISCHARGE\\nState3 = DISCHARGE\"\nmsg.topic = \"sofar/status\"\nmsg.qos = 0\nmsg.retain = false\nmsg._msgid = \"d014745cacc84574\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":320,"wires":[["b2dd2e0e.1cf33"]]},{"id":"85183425.06efa","type":"inject","z":"67af0149.01c99","name":"start","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":320,"wires":[["fba3439d.857828"]]},{"id":"bc82f146.29b38","type":"debug","z":"67af0149.01c99","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":380,"wires":[]},{"id":"b2dd2e0e.1cf33","type":"split","z":"67af0149.01c99","name":"split on \\n","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":470,"y":320,"wires":[["aa8f86cd.a0486"]]},{"id":"aa8f86cd.a0486","type":"switch","z":"67af0149.01c99","name":"select State1","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"State1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":620,"y":320,"wires":[["87565481.398b3"]]},{"id":"87565481.398b3","type":"change","z":"67af0149.01c99","name":"extract value","rules":[{"t":"change","p":"payload","pt":"msg","from":"State1 = ","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":380,"wires":[["bc82f146.29b38"]]}]

Yes trying to learn Javascript. One thing with Node-RED is their are so many different ways of doing the same thing!! That's 3 in this thread now. :grinning:

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