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"]]}]