How to save data in variables to process them

I am using nodes "inject" as test data that would arrive every second, later using node "function" I am saving the data in different variables identifying them with the "msg.topic" to be able to identify the data, separate them and save them in their respective variables.

However I need to save these local variables in "flow" variables to be able to access them in another function node (node P Fase A), and to be able to perform the equation there.

The problem is that it does not return the result of the equation, I do not know if it is because I am not saving the variables properly, or I am not calling them correctly, or failing that, I am not returning the result of the equation correctly.

I am new to node-red and the javascript language, so sorry for syntax errors.

Flow:

[{"id":"7ca8c0c6.600d4","type":"tab","label":"Comparacion","disabled":false,"info":""},{"id":"72e602d3.f6bffc","type":"inject","z":"7ca8c0c6.600d4","name":"FA V","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAV","payload":"8689","payloadType":"num","x":130,"y":120,"wires":[["415a0535.f8fa2c"]]},{"id":"4ec4afbb.85bf8","type":"inject","z":"7ca8c0c6.600d4","name":"FA C","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAC","payload":"7000","payloadType":"num","x":130,"y":260,"wires":[["415a0535.f8fa2c"]]},{"id":"415a0535.f8fa2c","type":"function","z":"7ca8c0c6.600d4","name":"Guardado de variables","func":"var fav, fac, fav_o, fac_o;\n\nswitch (msg.topic) {\n    case 'FAV':\n        fav = msg.payload\n        break;\n    case 'FAC':\n        fac = msg.payload\n        break;\n    case 'FAV°':\n        fav_o = msg.payload\n        break;\n    case 'FAC°':\n        fac_o = msg.payload\n        break;\n    \n}\n\nflow.set(\"FAV\", fav);\nflow.set(\"FAC\", fac);\nflow.set(\"FAV°\", fav_o);\nflow.set(\"FAC°\", fac_o);\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":200,"wires":[["3170e2d2.a5291e","859f57d0.5f1dd8"]]},{"id":"e536a019.7afdc","type":"inject","z":"7ca8c0c6.600d4","name":"FA V°","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAV°","payload":"0","payloadType":"num","x":130,"y":160,"wires":[["415a0535.f8fa2c"]]},{"id":"8820d9df.92c958","type":"inject","z":"7ca8c0c6.600d4","name":"FA C°","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAC°","payload":"30","payloadType":"num","x":140,"y":300,"wires":[["415a0535.f8fa2c"]]},{"id":"6ed91e5d.7724e","type":"comment","z":"7ca8c0c6.600d4","name":"((FAV*FAC)*Cos(FAC°- FAV°))","info":"var FAV = \n((FAV*FAC)*Cos(FAC°- FAV°))","x":640,"y":160,"wires":[]},{"id":"311afb3a.a449b4","type":"debug","z":"7ca8c0c6.600d4","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":450,"y":260,"wires":[]},{"id":"3170e2d2.a5291e","type":"function","z":"7ca8c0c6.600d4","name":"P Fase A","func":"var fav = flow.get(\"FAV\");\nvar fac = flow.get(\"FAC\");\nvar fav_o = flow.get(\"FAV°\");\nvar fac_o = flow.get(\"FAC°\");\n\nif (fav != null && fac != null && fav_o != null && fac_o != null) \n{\n    var result = (fav * fac) * Math.cos(fac_o - fav_o);\n    \n}\n\nflow.set(\"P_Fase_A\", result);\n\nmsg.payload = flow.get(\"P_Fase_A\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":200,"wires":[["b4ef3b70.890ae8"]]},{"id":"b4ef3b70.890ae8","type":"debug","z":"7ca8c0c6.600d4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"","statusType":"msg","x":790,"y":200,"wires":[]},{"id":"334857a7.c35448","type":"comment","z":"7ca8c0c6.600d4","name":"Voltajes","info":"Voltajes\nMagnitud y angulo\nFase A, B y C","x":130,"y":80,"wires":[]},{"id":"33b6c783.639c48","type":"comment","z":"7ca8c0c6.600d4","name":"Corrientes","info":"Corrientes\nMagnitud y angulo\nFase A, B y C","x":120,"y":220,"wires":[]},{"id":"859f57d0.5f1dd8","type":"rbe","z":"7ca8c0c6.600d4","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":330,"y":260,"wires":[["311afb3a.a449b4"]]}]

Would love to help you out - can you format your code following these guidelines ?

You had a typo for fac_o in your case statement set as fac_0.

Also you need to not overwrite the flow with a null value every time a msg is received. You can use return msg; as a break;

var fav, fac, fav_o, fac_o;

switch (msg.topic) {
 case 'FAV':
 fav = msg.payload;
 flow.set("FAV", fav);
 return msg;
 
 case 'FAC':
 fac = msg.payload;
 flow.set("FAC", fac);
 return msg;
 
 case 'FAV°':
 fav_o = msg.payload;
 flow.set("FAV°", fav_o);
 return msg;
 
 case 'FAC°':
 fac_o = msg.payload;
 flow.set("FAC°", fac_o);
 return msg;
 

}


Here is a working solution you can import.

[{"id":"7ca8c0c6.600d4","type":"tab","label":"Comparacion - working","disabled":false,"info":""},{"id":"72e602d3.f6bffc","type":"inject","z":"7ca8c0c6.600d4","name":"FA V","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAV","payload":"8689","payloadType":"num","x":130,"y":120,"wires":[["415a0535.f8fa2c"]]},{"id":"4ec4afbb.85bf8","type":"inject","z":"7ca8c0c6.600d4","name":"FA C","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAC","payload":"7000","payloadType":"num","x":130,"y":260,"wires":[["415a0535.f8fa2c"]]},{"id":"415a0535.f8fa2c","type":"function","z":"7ca8c0c6.600d4","name":"Guardado de variables","func":"var fav, fac, fav_o, fac_o;\n\nswitch (msg.topic) {\n case 'FAV':\n fav = msg.payload;\n flow.set(\"FAV\", fav);\n return msg;\n \n case 'FAC':\n fac = msg.payload;\n flow.set(\"FAC\", fac);\n return msg;\n \n case 'FAV°':\n fav_o = msg.payload;\n flow.set(\"FAV°\", fav_o);\n return msg;\n \n case 'FAC°':\n fac_o = msg.payload;\n flow.set(\"FAC°\", fac_o);\n return msg;\n \n break;\n \n}\n\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":200,"wires":[["859f57d0.5f1dd8","a6e429e3.1ff47"]]},{"id":"e536a019.7afdc","type":"inject","z":"7ca8c0c6.600d4","name":"FA V°","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAV°","payload":"0","payloadType":"num","x":130,"y":160,"wires":[["415a0535.f8fa2c"]]},{"id":"8820d9df.92c958","type":"inject","z":"7ca8c0c6.600d4","name":"FA C°","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"FAC°","payload":"30","payloadType":"num","x":140,"y":300,"wires":[["415a0535.f8fa2c"]]},{"id":"6ed91e5d.7724e","type":"comment","z":"7ca8c0c6.600d4","name":"((FAV*FAC)*Cos(FAC°- FAV°))","info":"var FAV = \n((FAV*FAC)*Cos(FAC°- FAV°))","x":960,"y":160,"wires":[]},{"id":"311afb3a.a449b4","type":"debug","z":"7ca8c0c6.600d4","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":770,"y":260,"wires":[]},{"id":"b4ef3b70.890ae8","type":"debug","z":"7ca8c0c6.600d4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1150,"y":200,"wires":[]},{"id":"334857a7.c35448","type":"comment","z":"7ca8c0c6.600d4","name":"Voltajes","info":"Voltajes\nMagnitud y angulo\nFase A, B y C","x":130,"y":80,"wires":[]},{"id":"33b6c783.639c48","type":"comment","z":"7ca8c0c6.600d4","name":"Corrientes","info":"Corrientes\nMagnitud y angulo\nFase A, B y C","x":120,"y":220,"wires":[]},{"id":"859f57d0.5f1dd8","type":"rbe","z":"7ca8c0c6.600d4","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":650,"y":260,"wires":[["311afb3a.a449b4"]]},{"id":"a6e429e3.1ff47","type":"function","z":"7ca8c0c6.600d4","name":"P Fase A","func":"var fav = flow.get(\"FAV\");\nvar fac = flow.get(\"FAC\");\nvar fav_o = flow.get(\"FAV°\");\nvar fac_o = flow.get(\"FAC°\");\n\nif (fav != null && fac != null && fav_o != null && fac_o != null) \n{\n var result = (fav * fac) * Math.cos(fac_o - fav_o);\n flow.set(\"P_Fase_A\", result);\n msg.payload = flow.get(\"P_Fase_A\");\n msg.topic = \"result\";\n return msg;\n \n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":200,"wires":[["b4ef3b70.890ae8"]]}]

hope this helps

1 Like

Done, i am sorry

1 Like

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