Splitting apart and recombining a payload

I have an incoming payload that looks something like this:
"person:80%,Car:40%,Dog:38%,Camera1" or in different terms:
"object1:80% sure,object2:40% sure,Source-Camera1"

I would like to take ONLY the FIRST object and put it into a "variable" and the source and put it in a variable and combine those variables with a bit of other text to have a NEW payload come out the other side.

Example:
input payload: "person:80%,Car:40%,Dog:38%,Camera1"
output payload "There is a person detected at Camera1"

Having multiple "objects" in the output is OK too. ie: "There is a person and car detected at Camera1"

Once I learn how to split this and rejoin it I'm sure the possibilities are endless I guess.

Hope someone can help me out with how to do this.

Before giving you a convoluted string splitting/joining solution, a quick question that may lead to a better answer.

Q. What sends this payload "person:80%,Car:40%,Dog:38%,Camera1"?
Is the format of this determined by something you have written (i.e. could you change this string to JSON? (dont worry if you dont know what that means - just need to know if you have the ability to provide this input payload in another format))

Good question. This is coming from Blue Iris camera / security software.
The first part ("person:80%,Car:40%,Dog:38%") is one object coming from BlueIris. It is their Macro called &MEMO and the "Camera1" is coming from their Macro called &NAME.

I can send almost anything in the MQTT string but it's the &MEMO one that I really need to split apart. Like to be able to take all of the possible "objects" (person, car, dog) remove the percentages, and then uses as many of the "objects" as I want. Using only non-null ones of course.

Hope that helps you help me!

Well, as i am not aware of the macro capabilities of blue iris, I will show you how to split up that string...

Flow...

[{"id":"48c18140b4da15fd","type":"function","z":"62a0e5a5.fc59bc","name":"","func":"const data = msg.payload;\nconst parts = data.split(\",\");\nconst objects = [];\nconst results = [];\nconst objectResults = {};\n\n//assuming the last entry is the device...\nconst device = parts.pop();\n\n\nfor (let index = 0; index < parts.length; index++) {\n    const str = parts[index];\n    const pair = str.split(\":\")\n    if (pair.length == 2 && pair[1].trim().endsWith(\"%\") ) {\n        objects.push(pair[0]);\n        results.push(pair[1]);\n        objectResults[pair[0]] = pair[1];\n    }\n}\n\nconst object = {\n    data,\n    device,\n    objects,\n    results,\n    objectResults\n}\n\nmsg.payload = object;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1720,"y":280,"wires":[["aedb432b6f8d2cf8"]]},{"id":"0b2e38bd38cc85c1","type":"inject","z":"62a0e5a5.fc59bc","name":"fake blue iris string","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"person:80%,Car:40%,Dog:38%,Camera1","payloadType":"str","x":1760,"y":220,"wires":[["48c18140b4da15fd"]]},{"id":"aedb432b6f8d2cf8","type":"debug","z":"62a0e5a5.fc59bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1920,"y":280,"wires":[]}]

To access parts of the message you are interested in, there’s a great page in the docs 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

https://nodered.org/docs/user-guide/messages

  1. Thanks.
  2. forgive my ignorance, but do I just cut and paste your above code into a function block? Sorry, just have never received this from anyone like this. Normally just see what each flow block shows and then type it out myself.

Copy the flow, then, in node-red, press CTRL+I. paste the copied flow, click the button.

Fantastic! Thank you!

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