# How to save data in variables to process them

**URL:** <https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531>\
**Category:** General\
**Created:** [2 April 2021 01:27 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531 "2021-04-02T01:27:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrokenBeat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brokenbeat/32/40023_2.png) [@BrokenBeat](https://discourse.nodered.org/u/BrokenBeat)\
**Post date:** [2 April 2021 01:27 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531/1 "2021-04-02T01:27:17Z")

</div>

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:

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [2 April 2021 02:13 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531/2 "2021-04-02T02:13:39Z")

</div>

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

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [2 April 2021 02:58 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531/3 "2021-04-02T02:58:22Z")

</div>

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;

```auto
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.

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![BrokenBeat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brokenbeat/32/40023_2.png) [@BrokenBeat](https://discourse.nodered.org/u/BrokenBeat)\
**Post date:** [2 April 2021 03:15 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531/4 "2021-04-02T03:15:22Z")

</div>

Done, i am sorry

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 April 2021 03:16 UTC](https://discourse.nodered.org/t/how-to-save-data-in-variables-to-process-them/43531/5 "2021-04-16T03:16:21Z")

</div>

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