Msg.payload splitting

Hi!

Many posts on this, still having problems with what should be "simple"

Debug Output from node which collects an MQTT topic:

{"topic":"BlueIris/LND/Status","payload":"{ \"type\": \"MOTION_A\", \"trigger\": \"ON\" }","qos":0,"retain":false,"_msgid":"36e3e0a9.8d986"}

The two variable I want to change with function into msg are:

msg.topic=LND
msg.payload=ON

Thanks

Ok, this isn't quite that simple.

The message (as you said) is coming from a MQTT node.

So the payload isn't quite that simple.

The payload you really want to change is actually msg.payload.trigger.

You will need to use a JSON node just after the MQTT IN node to make it ..... correct.

Then you use the change node to do that.

Hope that helps.

1 Like

Thanks. That works for the payload, also I would like to extract only "LND" (actually the letters between the first and second fwd slash in that position) from the topic "BlueIris/LND/Status"

Yeah, ok.

That would be done with a SWITCH node and set it for the topic rather than the payload.

Then (this isn't exactly what you want, but the best I can do) in the field set it to contains and put LND

Is that what you are wanting?

Something like this:

[{"id":"dcd802b1.9eae08","type":"json","z":"8bb4de19.f72c88","name":"","property":"payload","action":"","pretty":false,"x":420,"y":4250,"wires":[["daf3986b.deefc8","1462e6b4.eb9ff1"]]},{"id":"1462e6b4.eb9ff1","type":"switch","z":"8bb4de19.f72c88","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"LND","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":4250,"wires":[["412ba252.458c9c"]]},{"id":"412ba252.458c9c","type":"debug","z":"8bb4de19.f72c88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":4250,"wires":[]}]

Thanks again. Not quite, the topic will change, may be LND or DVW etc., is there a way to extract the specific letters component from the topic using the "/***/" meaning whatever the letters between the / and / are?

Hang on.

You said you wanted LND.... But now you want LND or DVW.

That is easy enough as you just add a second line in the switch node and make it DVW.

Yes, you could even go as far as making the texts /LND/ and /DVW/ to get better matching.

You then just expand on the switch node to filter (not really) the messages you want to get through.

You then just send both outputs of the switch node to the same place - if the payloads are going to be handled the same way.

Like this:

[{"id":"1462e6b4.eb9ff1","type":"switch","z":"8bb4de19.f72c88","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/LND/","vt":"str"},{"t":"cont","v":"/DVW/","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":4250,"wires":[["412ba252.458c9c"],["412ba252.458c9c"]]}]

I think I figured it out, what I really need is a change node with JSONata:

[{"id":"49926122.1a257","type":"change","z":"ed4fc3d3.44bd8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$substring(msg.topic, 9, 3)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":820,"wires":[["e40e9885.143af8"]]}]

You've lost me with that, but if it works: no worries.

jsonata $substring(msg.topic, 9, 3) works .. but will the word after the first slash always start at position 9 and will be 3 characters ?

you could use javascript in a Function node to split() the string in an array at the / character and pick the second element (independent of word length and char position)

msg.topic = msg.topic.split("/")[1]
return msg;

Thanks to everyone for the help, definitely pointed me in the right direction.

What I wanted to accomplish was to identify the MQTT topic that sat between the /../ knowing that it was variable depending on the alert that was set. (sometimes its LND, DVW, BACK... and pass this to a discrete object.

The solution was to use JSONata and regex to match the characters in between the / symbols. This was accomplished using a change node with the following expression:

$match(msg.topic,/(?<=\/).*(?=\/)/)

Was a bit tricky as the forward slash needs to be escaped first with a backslash and this employed both a lookback and lookforward.

[{"id":"49926122.1a257","type":"change","z":"ed4fc3d3.44bd8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$match(msg.topic,/(?<=\\/).*(?=\\/)/)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":820,"wires":[["e40e9885.143af8","2f685416.745b9c"]]}]

Thanks again for the direction and trials!

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