Put multiple variables into global object

Hi,
I want to put multiple messages I'm getting from a PLC into a global Object. How can I do that?

I tried to change a code I ripped of another node-red instance, but can't get it to work.

var msgTopic = msg.topic;
var msgPayload = msg.payload;
var itemsArray=  flow.get("items")|| [];
var auto, warn, err;
var arrData = [];


//Daten an globale Variable übergeben
var data=global.get("obStatus");


if (msgTopic.Automatik > 0){
    // inp.auto_ein = msgPayload;
    itemsArray[0] = msgPayload;
}

else if (msgTopic.Warnung > 0){
     //inp.auto_halt = msgPayload;
     itemsArray[1] = msgPayload;
     } 

else if (msgTopic.Stoerung > 0){
     //inp.ba_auto = msgPayload;
     itemsArray[2] = msgPayload;
     } 


var newObject = {"Automatik": itemsArray[0],
               "Warnung": itemsArray[1],
                "Stoerung": itemsArray[2],
}


//msg.payload = inp;
msg.payload = newObject;

global.set("obStatus",newObject);
flow.set("items",itemsArray);

return msg;

this is what the sensor data looks like
Screenshot 2022-08-23 071909

Does anyone have an idea?

best regards

I suspect flow.get("items") already has an object stored inside of it. Try deleting it from the context manager in the sidebar and run your function again.

It's empty. When I delete it it comes back after a few seconds.

Can you show us what it contains??

Screenshot 2022-08-23 075725

I haven't used this anywhere else

To understand what is going on, I need to know what is in the global context, the flow context, the incoming message and the outgoing result. Without each part it is difficult to assess.

This is the incoming stream of messages. They are read every 5 seconds.

This is what the context looks like with my current code.

Screenshot 2022-08-23 080558

I want to combine Automatik, Warnung and Störung into one global object I can use in other flows.

This is what the flow looks like

[{"id":"f490a8c0.7aa2e8","type":"function","z":"9cb96d17.d9b33","name":"","func":"var msgTopic = msg.topic;\nvar msgPayload = msg.payload;\nvar itemsArray=  flow.get(\"items\")|| [];\nvar auto, warn, err;\nvar arrData = [];\n\n\n//Daten an globale Variable übergeben\nvar data=global.get(\"obStatus\");\n\n\nif (msgTopic.Automatik > 0){\n    // inp.auto_ein = msgPayload;\n    itemsArray[0] = msgPayload;\n}\n\nelse if (msgTopic.Warnung > 0){\n     //inp.auto_halt = msgPayload;\n     itemsArray[1] = msgPayload;\n     } \n\nelse if (msgTopic.Stoerung > 0){\n     //inp.ba_auto = msgPayload;\n     itemsArray[2] = msgPayload;\n     } \n\n\nvar newObject = {\"Automatik\": itemsArray[0],\n               \"Warnung\": itemsArray[1],\n                \"Stoerung\": itemsArray[2],\n}\n\n\n//msg.payload = inp;\nmsg.payload = newObject;\n\nglobal.set(\"obStatus\",newObject);\nflow.set(\"items\",itemsArray);\n\nreturn msg;","outputs":1,"noerr":0,"x":830,"y":480,"wires":[[]]},{"id":"81ce9481.5f2ff8","type":"s7 in","z":"9cb96d17.d9b33","endpoint":"","mode":"single","variable":"","diff":true,"name":"","x":680,"y":480,"wires":[["f490a8c0.7aa2e8"]]}]

So when you export a flow, you should consider the person helping you does not have access to things like your PLC - so the flow you posted was of no use. It is always helpful to use INJECT nodes to simulate your data.

Any how, I now understand why it doesnt work

Try this instead...

const msgTopic = msg.topic;
const msgPayload = msg.payload;
const obStatus = global.get("obStatus") || {};
let { Automatik, Warnung, Stoerung } = obStatus
//node.warn({ Automatik, Warnung, Stoerung })

if (msgTopic === "Automatik") {
    obStatus.Automatik = msgPayload;
    //node.warn({ "setting Automatik": Automatik })
} else if (msgTopic === "Warnung") {
    obStatus.Warnung = msgPayload;
    //node.warn({ "setting Warnung": Warnung })
} else if (msgTopic === "Stoerung") {
    obStatus.Stoerung = msgPayload;
    //node.warn({ "setting Stoerung": Stoerung })
}

global.set("obStatus", obStatus);
const items = [obStatus.Automatik, obStatus.Warnung, obStatus.Stoerung]
flow.set("items", items);

msg.payload = obStatus;
return msg;

Proof...
chrome_VuSLRjdDkl

Demo flow...

[{"id":"08a790b59d189683","type":"function","z":"6f3a24bd7ed7326b","name":"join and store","func":"const msgTopic = msg.topic;\nconst msgPayload = msg.payload;\nconst obStatus = global.get(\"obStatus\") || {};\nlet { Automatik, Warnung, Stoerung } = obStatus\n//node.warn({ Automatik, Warnung, Stoerung })\n\nif (msgTopic === \"Automatik\") {\n    obStatus.Automatik = msgPayload;\n    //node.warn({ \"setting Automatik\": Automatik })\n} else if (msgTopic === \"Warnung\") {\n    obStatus.Warnung = msgPayload;\n    //node.warn({ \"setting Warnung\": Warnung })\n} else if (msgTopic === \"Stoerung\") {\n    obStatus.Stoerung = msgPayload;\n    //node.warn({ \"setting Stoerung\": Stoerung })\n}\n\nglobal.set(\"obStatus\", obStatus);\nconst items = [obStatus.Automatik, obStatus.Warnung, obStatus.Stoerung]\nflow.set(\"items\", items);\n\nmsg.payload = obStatus;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":1220,"wires":[["0f442bb1006c4b18"]]},{"id":"4b0b1c007ea0da93","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Automatik","payload":"true","payloadType":"bool","x":880,"y":1140,"wires":[["08a790b59d189683"]]},{"id":"5cec8a2575223a4c","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Warnung","payload":"true","payloadType":"bool","x":880,"y":1180,"wires":[["08a790b59d189683"]]},{"id":"9325262c80cd3b04","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Stoerung","payload":"true","payloadType":"bool","x":880,"y":1220,"wires":[["08a790b59d189683"]]},{"id":"52b6d5559be8bb36","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Automatik","payload":"false","payloadType":"bool","x":890,"y":1280,"wires":[["08a790b59d189683"]]},{"id":"4bd9df71f509943f","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Warnung","payload":"false","payloadType":"bool","x":880,"y":1320,"wires":[["08a790b59d189683"]]},{"id":"78a443d88f3e1783","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Stoerung","payload":"false","payloadType":"bool","x":880,"y":1360,"wires":[["08a790b59d189683"]]},{"id":"0f442bb1006c4b18","type":"debug","z":"6f3a24bd7ed7326b","name":"debug 65","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":1280,"wires":[]}]
1 Like

And now the low-code no-code version...

chrome_zVUj5LbnXS

Demo flow...

[{"id":"4b0b1c007ea0da93","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Automatik","payload":"true","payloadType":"bool","x":880,"y":1140,"wires":[["57b091e23b3baf31"]]},{"id":"5cec8a2575223a4c","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Warnung","payload":"true","payloadType":"bool","x":880,"y":1180,"wires":[["57b091e23b3baf31"]]},{"id":"9325262c80cd3b04","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Stoerung","payload":"true","payloadType":"bool","x":880,"y":1220,"wires":[["57b091e23b3baf31"]]},{"id":"52b6d5559be8bb36","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Automatik","payload":"false","payloadType":"bool","x":890,"y":1280,"wires":[["57b091e23b3baf31"]]},{"id":"4bd9df71f509943f","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Warnung","payload":"false","payloadType":"bool","x":880,"y":1320,"wires":[["57b091e23b3baf31"]]},{"id":"78a443d88f3e1783","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Stoerung","payload":"false","payloadType":"bool","x":880,"y":1360,"wires":[["57b091e23b3baf31"]]},{"id":"0f442bb1006c4b18","type":"debug","z":"6f3a24bd7ed7326b","name":"debug 65","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1140,"y":1300,"wires":[]},{"id":"57b091e23b3baf31","type":"join","z":"6f3a24bd7ed7326b","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1070,"y":1220,"wires":[["67c7828af6dd69f2"]]},{"id":"67c7828af6dd69f2","type":"change","z":"6f3a24bd7ed7326b","name":"store","rules":[{"t":"set","p":"items2","pt":"flow","to":"[payload.Automatik, payload.Stoerung, payload.Warnung]","tot":"jsonata"},{"t":"set","p":"obStatus2","pt":"global","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":1260,"wires":[["0f442bb1006c4b18"]]}]
3 Likes

Sorry, I didn't think of that.
Thanks a lot for your help.

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