Add an object to a payload

How do I add an object to a payload?

Here is what I have

[{"id":"d221c2c7.dcf32","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d92f539e.01a78","type":"inject","z":"d221c2c7.dcf32","name":"create fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":180,"y":80,"wires":[["385c74a6.71e1fc"]]},{"id":"7b7b8ba6.0deb14","type":"ui_table","z":"d221c2c7.dcf32","group":"37b5f1bd.773eae","name":"","order":1,"width":"6","height":"4","columns":[],"outputs":1,"cts":true,"x":780,"y":220,"wires":[[]]},{"id":"12b4a833.ab7ef8","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":140,"wires":[["385c74a6.71e1fc"]]},{"id":"9139f5f2.5c8b08","type":"inject","z":"d221c2c7.dcf32","name":"create fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":180,"y":300,"wires":[["e22fb67e.179248"]]},{"id":"5475d1b0.9d254","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":360,"wires":[["e22fb67e.179248"]]},{"id":"88a6e89c.4cb5f8","type":"function","z":"d221c2c7.dcf32","name":"add to schedule","func":"var x;\nvar msg1;\n\nmsg1 = {payload: flow.get(\"fault1\")};\nmsg1.payload[1] = flow.get(\"fault2\");\n\nreturn msg1; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":220,"wires":[["7b7b8ba6.0deb14","c21a6a95.b5d448"]]},{"id":"e22fb67e.179248","type":"change","z":"d221c2c7.dcf32","name":"ui_control","rules":[{"t":"set","p":"fault2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":320,"wires":[["88a6e89c.4cb5f8"]]},{"id":"385c74a6.71e1fc","type":"change","z":"d221c2c7.dcf32","name":"ui_control","rules":[{"t":"set","p":"fault1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":120,"wires":[["88a6e89c.4cb5f8"]]},{"id":"c21a6a95.b5d448","type":"debug","z":"d221c2c7.dcf32","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":140,"wires":[]},{"id":"37b5f1bd.773eae","type":"ui_group","name":"1750+ 1 alarms","tab":"78efbcf6.3781f4","order":2,"disp":true,"width":"6","collapse":false},{"id":"78efbcf6.3781f4","type":"ui_tab","name":"Scheduler","icon":"autorenew","order":2,"disabled":false,"hidden":false}]

Here is the result

image

As you can see, it added the object, but as an array. I need them both to be objects (like [0]). How do I do this?

Ok you add the first fault to msg1.payload, the first is an array containing an object.

So when you add the next fault which is an array, you then add it to msg1.payload[1]. So you put an array in an array.

what you need to do is
msg1 = {payload: flow.get("fault1")}; msg1.payload[1] = flow.get("fault2")[0];

But without knowing what you are attempting to do there is not much more help we can give.

Thanks! Almost there.
So what I'm trying to do is create a table of alarms. Node red will monitor a number of booleans. If a certain boolean goes true, then an alarm happened and a fault payload will be sent. What I have created simulates 2 booleans.

[{"id":"d221c2c7.dcf32","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d92f539e.01a78","type":"inject","z":"d221c2c7.dcf32","name":"create fault1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":190,"y":80,"wires":[["385c74a6.71e1fc"]]},{"id":"7b7b8ba6.0deb14","type":"ui_table","z":"d221c2c7.dcf32","group":"37b5f1bd.773eae","name":"","order":1,"width":"6","height":"4","columns":[],"outputs":1,"cts":true,"x":790,"y":220,"wires":[[]]},{"id":"12b4a833.ab7ef8","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":140,"wires":[["385c74a6.71e1fc"]]},{"id":"9139f5f2.5c8b08","type":"inject","z":"d221c2c7.dcf32","name":"create fault2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":190,"y":300,"wires":[["e22fb67e.179248"]]},{"id":"5475d1b0.9d254","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":360,"wires":[["e22fb67e.179248"]]},{"id":"88a6e89c.4cb5f8","type":"function","z":"d221c2c7.dcf32","name":"add to schedule","func":"var x;\nvar msg1;\n\n\n\nmsg1 = {payload: flow.get(\"fault1\")}; msg.payload[1] = flow.get(\"fault2\")[0];\n//msg1.payload[1] = flow.get(\"fault2\");\n\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":200,"wires":[["7b7b8ba6.0deb14","c21a6a95.b5d448"]]},{"id":"e22fb67e.179248","type":"change","z":"d221c2c7.dcf32","name":"construct alarm 2 payload","rules":[{"t":"set","p":"fault2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":320,"wires":[["88a6e89c.4cb5f8"]]},{"id":"385c74a6.71e1fc","type":"change","z":"d221c2c7.dcf32","name":"construct alarm 1 payload","rules":[{"t":"set","p":"fault1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":120,"wires":[["88a6e89c.4cb5f8"]]},{"id":"c21a6a95.b5d448","type":"debug","z":"d221c2c7.dcf32","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":140,"wires":[]},{"id":"a7bde91a.c0cc28","type":"comment","z":"d221c2c7.dcf32","name":"alarm 1","info":"","x":80,"y":120,"wires":[]},{"id":"bfb15a2.e8373a8","type":"comment","z":"d221c2c7.dcf32","name":"alarm 2","info":"","x":70,"y":340,"wires":[]},{"id":"7bdaf511.1ed95c","type":"comment","z":"d221c2c7.dcf32","name":"combines all messages","info":"prepare payload of alarms to deliver to table","x":620,"y":240,"wires":[]},{"id":"37b5f1bd.773eae","type":"ui_group","name":"1750+ 1 alarms","tab":"78efbcf6.3781f4","order":2,"disp":true,"width":"6","collapse":false},{"id":"78efbcf6.3781f4","type":"ui_tab","name":"Scheduler","icon":"autorenew","order":2,"disabled":false,"hidden":false}]

I added comments and names to the nodes. I hope it's easier to read now.

For some reason when I press create fault 2 that message gets written twice (once where fault 1 should be, and once where fault 2 should be.)

image

Can you explain what you have here?

msg1 = {payload: flow.get("fault1")}; msg.payload[1] = flow.get("fault2")[0];

Thanks!

You wrote that not me, mine references msg1.payload, while what you typed is msg.payload.

But wht mine did with flow.get("fault2")[0], was to target the object in fault2 not the array.

Oh I get it!
So my payload is looking something like this:
image

The problem is it's creating situations like this:
image

It's because of the blanks in object 0. So I think the best way to do this is create the payload continuously and only add payloads when there is a fault. Otherwise don't add that alarm payload. How do I do that?

You would need to add if statements to check values of objects and only add if the do not equal "". Or a switch node prior to the function to check if the property is not empty.

Got it. Going back to your code:

msg1 = {payload: flow.get("fault1")}; msg1.payload[1] = flow.get("fault2")[0];

So let me paraphrase here and please correct where I'm wrong...

payload: flow.get("fault1")};

This gets creates the initial payload

msg1.payload[1] = flow.get("fault2")

This adds creates/add to object 1

what does the [0] at the end do?

flow.get("fault2") is an array [{"fault":"kitchen"}]
flow.get("fault2")[0] is the object {"fault":"kitchen"}. i.e position 0 of the array

But what you want to do is this, to add more increase number of messages in join node. At start up it has to receive all meassages, then after that any single message change will update table.

[{"id":"7015004b.e05608","type":"inject","z":"bc569d1f.364ac8","name":"no fault 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"null\",\"fault\":null}]","payloadType":"json","x":150,"y":140,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"4a7a4ae2.c9e9f4","type":"join","z":"bc569d1f.364ac8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":380,"y":200,"wires":[["a3ad66c0.31c058"]]},{"id":"e38a06c9.d6f2a","type":"inject","z":"bc569d1f.364ac8","name":"no fault 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"null\",\"fault\":null}]","payloadType":"json","x":150,"y":220,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"3cc29ec9.5d594a","type":"inject","z":"bc569d1f.364ac8","name":"create fault2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":160,"y":260,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"501a4f71.9aea18","type":"inject","z":"bc569d1f.364ac8","name":"create fault1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":160,"y":180,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"a3ad66c0.31c058","type":"change","z":"bc569d1f.364ac8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload.*[*][fault != null]]\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":260,"wires":[["f7c7fdd4.5da26"]]},{"id":"f7c7fdd4.5da26","type":"debug","z":"bc569d1f.364ac8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":200,"wires":[]}]

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