# Solving mathematical functions made from dashboard inputs

**URL:** <https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812>\
**Category:** General\
**Created:** [29 March 2020 06:03 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812 "2020-03-29T06:03:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [29 March 2020 06:03 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/1 "2020-03-29T06:03:04Z")

</div>

hi i'm working on a dropdown with options so basically i want to write some equations and save it in a database to use and then recover it on a dropdown like new options and when i choose for example equation 1 send this equation as a variable like varequation 1 = (5/1023) \*  
and use it in a second variable calling var equation 1 = 5/1023 var1 = msg.payload  
var result equation 1 = 5/1023\* var1;  
so i'm having problems with the step between recover it and send it to the dropdown.  
i will put my flow.

```auto
[{"id":"2a76abae.3cb354","type":"inject","z":"217dd16.38f122e","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":"","x":260,"y":540,"wires":[["404e5869.289af8"]]},{"id":"404e5869.289af8","type":"function","z":"217dd16.38f122e","name":"Prep data","func":"// global variables separated by semicolon to be saved\nvar globallist = \"equa;timeminute\"\nvar mylist = globallist.split(\";\");\n\nvar outputs = [];\n\nfor (i=0; i<mylist.length; i++) {\n outputs.push({ key : mylist[i], type: typeof global.get(mylist[i]), value: global.get(mylist[i])});\n}\n \n \nmsg.payload = outputs;\nreturn msg;\n\n//msg.payload=typeof global.get(\"torrent_keywords\");\n","outputs":1,"noerr":0,"x":440,"y":540,"wires":[["796ae060.2571d"]]},{"id":"722bde30.b4061","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":252,"y":495,"wires":[]},{"id":"82ed5561.8ea6c8","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":280,"y":626,"wires":[]},{"id":"623cfee.c4562","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":240,"y":680,"wires":[["ec4e4dad.53165"]]},{"id":"3cf22d71.3cf902","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"true","x":760,"y":540,"wires":[["41a8df7d.8a1b2"]]},{"id":"ec4e4dad.53165","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":440,"y":680,"wires":[["cc306e1f.95de2"]]},{"id":"baebe8d4.f4d4e8","type":"function","z":"217dd16.38f122e","name":"Restore","func":"var output = [];\n\nfor (var i=0; i<msg.payload.length; i++) {\n switch (msg.payload[i].type) {\n case 'undefined': \n // the global variable probably had no value, nothing needs to be restored\n output.push(msg.payload[i].key + \" is undefined.\");\n break;\n case 'number':\n if (msg.payload[i].value===\"NaN\") {\n // there is no valid value to be restored, skip this variable\n output.push(msg.payload[i].key + \" is NaN.\");\n } else {\n if (msg.payload[i].value.toString().indexOf(\".\")>-1) {\n // the value appears to be a float\n global.set(msg.payload[i].key,parseFloat(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n } else {\n global.set(msg.payload[i].key,parseInt(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n }\n }\n break;\n case 'string':\n global.set(msg.payload[i].key,msg.payload[i].value);\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n case 'boolean':\n global.set(msg.payload[i].key,msg.payload[i].value===\"true\");\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n }\n}\n\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":780,"y":680,"wires":[["5bc9e96f.723ff8","f7213c41.3124"]]},{"id":"796ae060.2571d","type":"json","z":"217dd16.38f122e","name":"","x":590,"y":540,"wires":[["3cf22d71.3cf902"]]},{"id":"cc306e1f.95de2","type":"json","z":"217dd16.38f122e","name":"","x":630,"y":680,"wires":[["baebe8d4.f4d4e8"]]},{"id":"5bc9e96f.723ff8","type":"function","z":"217dd16.38f122e","name":"Irrigation hour restore","func":"msg.topic = \"equa\";\nmsg.payload = global.get(\"equa\");\nreturn msg;","outputs":1,"noerr":0,"x":980,"y":640,"wires":[["376f6bb1.5aa224","93af67ea.420c68"]]},{"id":"f7213c41.3124","type":"function","z":"217dd16.38f122e","name":"Irrigation minute restore","func":"msg.topic = \"timeminute\";\nmsg.payload = global.get(\"timeminute\");\nreturn msg;","outputs":1,"noerr":0,"x":990,"y":720,"wires":[["89b5d8a5.bdc828"]]},{"id":"41a8df7d.8a1b2","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":540,"wires":[]},{"id":"376f6bb1.5aa224","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1230,"y":540,"wires":[]},{"id":"89b5d8a5.bdc828","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1230,"y":840,"wires":[]},{"id":"43e0e95c.6cc1b8","type":"ui_text_input","z":"217dd16.38f122e","name":"","label":"","tooltip":"","group":"d656deff.eac3b","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"","x":220,"y":380,"wires":[["b751f9f3.add038"]]},{"id":"cef3b486.eb6f88","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":380,"wires":[]},{"id":"b751f9f3.add038","type":"function","z":"217dd16.38f122e","name":"","func":"var equa = msg.payload;\n\nglobal.set('equa',equa);\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":380,"wires":[["cef3b486.eb6f88"]]},{"id":"73bebdc6.3a46b4","type":"ui_dropdown","z":"217dd16.38f122e","name":"","label":"","tooltip":"","place":"Select option","group":"679f2509.be684c","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":1500,"y":640,"wires":[["810a2cc2.b603c"]]},{"id":"810a2cc2.b603c","type":"ui_toast","z":"217dd16.38f122e","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1530,"y":480,"wires":[]},{"id":"93af67ea.420c68","type":"function","z":"217dd16.38f122e","name":"transform data to correct format","func":"//as per info panel, the data needs to be formatted\n//as [\"Choice 1\", \"Choice 2\", {\"Choice 3\":\"3\"}]\n//and sent in msg.options.\nmsg.options = [];//create empty array\nfor(let i = 0; i < msg.payload.length; i++){\n let row = msg.payload[i]; //get the row\n let opt = {};//make new opt object\n opt[row[0]] = row[1]; //add a propery to object called row[0] & set its value to row[1]\n msg.options.push(opt);//add the opt to array \n}\nreturn msg;","outputs":1,"noerr":0,"x":1270,"y":640,"wires":[["73bebdc6.3a46b4"]]},{"id":"d656deff.eac3b","type":"ui_group","z":"","name":"Default","tab":"8f4c42b3.c0e6","disp":true,"width":"6","collapse":false},{"id":"679f2509.be684c","type":"ui_group","z":"","name":"Default","tab":"658319a6.10c4d8","disp":true,"width":"6","collapse":false},{"id":"8f4c42b3.c0e6","type":"ui_tab","z":"","name":"dropdown","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"658319a6.10c4d8","type":"ui_tab","z":"","name":"Menu","icon":"fa-tachometer","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 March 2020 10:24 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/2 "2020-03-29T10:24:34Z")

</div>

Please provide some test data - i.e. what you would enter in to see this work.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [29 March 2020 19:52 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/3 "2020-03-29T19:52:09Z")

</div>

y = 2.1425x + 3.8416  
y = 4.4607x + 8.3455  
y = 7.5648x + 13.75

this ecuations @zenofmud

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 March 2020 21:01 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/4 "2020-03-29T21:01:12Z")

</div>

You should put a `debug` node (set to display the `Complete msg object` on the output of the `transform data to correct format` function node and see what you get.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [29 March 2020 22:31 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/5 "2020-03-29T22:31:00Z")

</div>

hi, yeah i get this but i don't know why in the drop down it appers splitted.  
i appreciate that you try to help me but i think i need more help than a debbug, actually i don't know why was important the ecuations.

 ![debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/69d5d1f5928638cd65e5f99efc1295ea6aea4f6d.png) ![splitted](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c5cdfe5b291657224225a978c9384628584dbb56.png)  
@zenofmud

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 March 2020 23:36 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/6 "2020-03-29T23:36:26Z")

</div>

Did you expand msg.options in the debug? Is is giving what you think you are creating?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [30 March 2020 05:10 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/7 "2020-03-30T05:10:12Z")

</div>

yeah in the debug appears what i want @zenofmud

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [30 March 2020 05:46 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/8 "2020-03-30T05:46:15Z")

</div>

In your screenshot is the first debug message we see the output/msg.payload of the irrigation hour restore function?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [30 March 2020 05:48 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/9 "2020-03-30T05:48:28Z")

</div>

@JGKK sorry my mistake i did't change the names of the functions

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [30 March 2020 05:52 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/10 "2020-03-30T05:52:51Z")

</div>

Can you post an exact example payload that the irrigation hour restore node would output? Just so I can use it in an inject node?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 March 2020 08:04 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/11 "2020-03-30T08:04:25Z")

</div>

When I run your flow with your data, I see that this is the payload going into the 'transform data to correct format' function:  
`payload: "y = 2.1425x + 3.8416"`  
note that this is a string. In the function you process msg.payload as if it is an array:  
` let row = msg.payload[i]; //get the row`  
This will turn the string into an array so  
msg.payload[0] will be `y`  
msg.payload[1] will be ` `  
msg.payload[2] will be `=`  
etc etc.

Is that what you really want?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [30 March 2020 17:47 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/12 "2020-03-30T17:47:23Z")

</div>

@JGKK here is the flow with the correct names.

```auto
[{"id":"2a76abae.3cb354","type":"inject","z":"217dd16.38f122e","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":"","x":260,"y":540,"wires":[["404e5869.289af8"]]},{"id":"404e5869.289af8","type":"function","z":"217dd16.38f122e","name":"Prep data","func":"// global variables separated by semicolon to be saved\nvar globallist = \"equa;timeminute\"\nvar mylist = globallist.split(\";\");\n\nvar outputs = [];\n\nfor (i=0; i<mylist.length; i++) {\n outputs.push({ key : mylist[i], type: typeof global.get(mylist[i]), value: global.get(mylist[i])});\n}\n \n \nmsg.payload = outputs;\nreturn msg;\n\n//msg.payload=typeof global.get(\"torrent_keywords\");\n","outputs":1,"noerr":0,"x":440,"y":540,"wires":[["796ae060.2571d"]]},{"id":"722bde30.b4061","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":252,"y":495,"wires":[]},{"id":"82ed5561.8ea6c8","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":280,"y":626,"wires":[]},{"id":"623cfee.c4562","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":240,"y":680,"wires":[["ec4e4dad.53165"]]},{"id":"3cf22d71.3cf902","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"true","x":760,"y":540,"wires":[["41a8df7d.8a1b2"]]},{"id":"ec4e4dad.53165","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":440,"y":680,"wires":[["cc306e1f.95de2"]]},{"id":"baebe8d4.f4d4e8","type":"function","z":"217dd16.38f122e","name":"Restore","func":"var output = [];\n\nfor (var i=0; i<msg.payload.length; i++) {\n switch (msg.payload[i].type) {\n case 'undefined': \n // the global variable probably had no value, nothing needs to be restored\n output.push(msg.payload[i].key + \" is undefined.\");\n break;\n case 'number':\n if (msg.payload[i].value===\"NaN\") {\n // there is no valid value to be restored, skip this variable\n output.push(msg.payload[i].key + \" is NaN.\");\n } else {\n if (msg.payload[i].value.toString().indexOf(\".\")>-1) {\n // the value appears to be a float\n global.set(msg.payload[i].key,parseFloat(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n } else {\n global.set(msg.payload[i].key,parseInt(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n }\n }\n break;\n case 'string':\n global.set(msg.payload[i].key,msg.payload[i].value);\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n case 'boolean':\n global.set(msg.payload[i].key,msg.payload[i].value===\"true\");\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n }\n}\n\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":780,"y":680,"wires":[["5bc9e96f.723ff8"]]},{"id":"796ae060.2571d","type":"json","z":"217dd16.38f122e","name":"","x":590,"y":540,"wires":[["3cf22d71.3cf902"]]},{"id":"cc306e1f.95de2","type":"json","z":"217dd16.38f122e","name":"","x":630,"y":680,"wires":[["baebe8d4.f4d4e8"]]},{"id":"5bc9e96f.723ff8","type":"function","z":"217dd16.38f122e","name":"equation restore","func":"msg.topic = \"equa\";\nmsg.payload = global.get(\"equa\");\nreturn msg;","outputs":1,"noerr":0,"x":960,"y":640,"wires":[["376f6bb1.5aa224","93af67ea.420c68"]]},{"id":"41a8df7d.8a1b2","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":540,"wires":[]},{"id":"376f6bb1.5aa224","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1230,"y":540,"wires":[]},{"id":"43e0e95c.6cc1b8","type":"ui_text_input","z":"217dd16.38f122e","name":"","label":"","tooltip":"","group":"d656deff.eac3b","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"","x":220,"y":380,"wires":[["b751f9f3.add038"]]},{"id":"cef3b486.eb6f88","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":380,"wires":[]},{"id":"b751f9f3.add038","type":"function","z":"217dd16.38f122e","name":"","func":"var equa = msg.payload;\n\nglobal.set('equa',equa);\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":380,"wires":[["cef3b486.eb6f88"]]},{"id":"73bebdc6.3a46b4","type":"ui_dropdown","z":"217dd16.38f122e","name":"","label":"","tooltip":"","place":"Select option","group":"679f2509.be684c","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":1500,"y":640,"wires":[["810a2cc2.b603c","fda2e215.67a4"]]},{"id":"810a2cc2.b603c","type":"ui_toast","z":"217dd16.38f122e","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1530,"y":480,"wires":[]},{"id":"93af67ea.420c68","type":"function","z":"217dd16.38f122e","name":"transform data to correct format","func":"//as per info panel, the data needs to be formatted\n//as [\"Choice 1\", \"Choice 2\", {\"Choice 3\":\"3\"}]\n//and sent in msg.options.\nmsg.options = [];//create empty array\nfor(let i = 0; i < msg.payload.length; i++){\n let row = msg.payload; //get the row\n msg.options.push(row);//add the opt to array \n}\nreturn msg;","outputs":1,"noerr":0,"x":1270,"y":640,"wires":[["73bebdc6.3a46b4","cf142e35.d45ea"]]},{"id":"cf142e35.d45ea","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1700,"y":560,"wires":[]},{"id":"fda2e215.67a4","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1720,"y":640,"wires":[]},{"id":"d656deff.eac3b","type":"ui_group","z":"","name":"Default","tab":"8f4c42b3.c0e6","disp":true,"width":"6","collapse":false},{"id":"679f2509.be684c","type":"ui_group","z":"","name":"Default","tab":"658319a6.10c4d8","disp":true,"width":"6","collapse":false},{"id":"8f4c42b3.c0e6","type":"ui_tab","z":"","name":"dropdown","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"658319a6.10c4d8","type":"ui_tab","z":"","name":"Menu","icon":"fa-tachometer","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [30 March 2020 17:50 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/13 "2020-03-30T17:50:06Z")

</div>

@zenofmud you're right now i fixed that part of the code but now i have the problem with the dropdown my equation appears like 17 times i'm not a person really in to with coding i will put the flow and also the function code.

```auto
[{"id":"2a76abae.3cb354","type":"inject","z":"217dd16.38f122e","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":"","x":260,"y":540,"wires":[["404e5869.289af8"]]},{"id":"404e5869.289af8","type":"function","z":"217dd16.38f122e","name":"Prep data","func":"// global variables separated by semicolon to be saved\nvar globallist = \"equa;timeminute\"\nvar mylist = globallist.split(\";\");\n\nvar outputs = [];\n\nfor (i=0; i<mylist.length; i++) {\n outputs.push({ key : mylist[i], type: typeof global.get(mylist[i]), value: global.get(mylist[i])});\n}\n \n \nmsg.payload = outputs;\nreturn msg;\n\n//msg.payload=typeof global.get(\"torrent_keywords\");\n","outputs":1,"noerr":0,"x":440,"y":540,"wires":[["796ae060.2571d"]]},{"id":"722bde30.b4061","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":252,"y":495,"wires":[]},{"id":"82ed5561.8ea6c8","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":280,"y":626,"wires":[]},{"id":"623cfee.c4562","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":240,"y":680,"wires":[["ec4e4dad.53165"]]},{"id":"3cf22d71.3cf902","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"true","x":760,"y":540,"wires":[["41a8df7d.8a1b2"]]},{"id":"ec4e4dad.53165","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":440,"y":680,"wires":[["cc306e1f.95de2"]]},{"id":"baebe8d4.f4d4e8","type":"function","z":"217dd16.38f122e","name":"Restore","func":"var output = [];\n\nfor (var i=0; i<msg.payload.length; i++) {\n switch (msg.payload[i].type) {\n case 'undefined': \n // the global variable probably had no value, nothing needs to be restored\n output.push(msg.payload[i].key + \" is undefined.\");\n break;\n case 'number':\n if (msg.payload[i].value===\"NaN\") {\n // there is no valid value to be restored, skip this variable\n output.push(msg.payload[i].key + \" is NaN.\");\n } else {\n if (msg.payload[i].value.toString().indexOf(\".\")>-1) {\n // the value appears to be a float\n global.set(msg.payload[i].key,parseFloat(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n } else {\n global.set(msg.payload[i].key,parseInt(msg.payload[i].value));\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n }\n }\n break;\n case 'string':\n global.set(msg.payload[i].key,msg.payload[i].value);\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n case 'boolean':\n global.set(msg.payload[i].key,msg.payload[i].value===\"true\");\n output.push(msg.payload[i].key + \" is restored to \" + msg.payload[i].value);\n break;\n }\n}\n\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":780,"y":680,"wires":[["5bc9e96f.723ff8"]]},{"id":"796ae060.2571d","type":"json","z":"217dd16.38f122e","name":"","x":590,"y":540,"wires":[["3cf22d71.3cf902"]]},{"id":"cc306e1f.95de2","type":"json","z":"217dd16.38f122e","name":"","x":630,"y":680,"wires":[["baebe8d4.f4d4e8"]]},{"id":"5bc9e96f.723ff8","type":"function","z":"217dd16.38f122e","name":"equation restore","func":"msg.topic = \"equa\";\nmsg.payload = global.get(\"equa\");\nreturn msg;","outputs":1,"noerr":0,"x":960,"y":640,"wires":[["376f6bb1.5aa224","93af67ea.420c68"]]},{"id":"41a8df7d.8a1b2","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":540,"wires":[]},{"id":"376f6bb1.5aa224","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1230,"y":540,"wires":[]},{"id":"43e0e95c.6cc1b8","type":"ui_text_input","z":"217dd16.38f122e","name":"","label":"","tooltip":"","group":"d656deff.eac3b","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"","x":220,"y":380,"wires":[["b751f9f3.add038"]]},{"id":"cef3b486.eb6f88","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":380,"wires":[]},{"id":"b751f9f3.add038","type":"function","z":"217dd16.38f122e","name":"","func":"var equa = msg.payload;\n\nglobal.set('equa',equa);\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":380,"wires":[["cef3b486.eb6f88"]]},{"id":"73bebdc6.3a46b4","type":"ui_dropdown","z":"217dd16.38f122e","name":"","label":"","tooltip":"","place":"Select option","group":"679f2509.be684c","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":1500,"y":640,"wires":[["810a2cc2.b603c","fda2e215.67a4"]]},{"id":"810a2cc2.b603c","type":"ui_toast","z":"217dd16.38f122e","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1530,"y":480,"wires":[]},{"id":"93af67ea.420c68","type":"function","z":"217dd16.38f122e","name":"transform data to correct format","func":"//as per info panel, the data needs to be formatted\n//as [\"Choice 1\", \"Choice 2\", {\"Choice 3\":\"3\"}]\n//and sent in msg.options.\nmsg.options = [];//create empty array\nfor(let i = 0; i < msg.payload.length; i++){\n let row = msg.payload; //get the row\n msg.options.push(row);//add the opt to array \n}\nreturn msg;","outputs":1,"noerr":0,"x":1270,"y":640,"wires":[["73bebdc6.3a46b4","cf142e35.d45ea"]]},{"id":"cf142e35.d45ea","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1700,"y":560,"wires":[]},{"id":"fda2e215.67a4","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1720,"y":640,"wires":[]},{"id":"d656deff.eac3b","type":"ui_group","z":"","name":"Default","tab":"8f4c42b3.c0e6","disp":true,"width":"6","collapse":false},{"id":"679f2509.be684c","type":"ui_group","z":"","name":"Default","tab":"658319a6.10c4d8","disp":true,"width":"6","collapse":false},{"id":"8f4c42b3.c0e6","type":"ui_tab","z":"","name":"dropdown","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"658319a6.10c4d8","type":"ui_tab","z":"","name":"Menu","icon":"fa-tachometer","order":1,"disabled":false,"hidden":false}].

```

and the code is:

```auto
//as per info panel, the data needs to be formatted
//as ["Choice 1", "Choice 2", {"Choice 3":"3"}]
//and sent in msg.options.
msg.options = [];//create empty array
for(let i = 0; i < msg.payload.length; i++){
    let row = msg.payload; //get the row
    msg.options.push(row);//add the opt to array    
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [30 March 2020 18:07 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/14 "2020-03-30T18:07:47Z")

</div>

Yes this is happening because your payload is a string but your treating it like an array as @zenofmud already said. So when you use a for loop that runs for the string length it will push the payload once for every single character in this string. In this case 17 times.  
The question is are you actually expecting the global var that you get in the previous function to be an array of several equations? So than the error would be where ever you are creating/setting that global var and not here.  
Best regards Johannes

PS: Is the text input the only place you are setting this global var?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [30 March 2020 18:36 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/15 "2020-03-30T18:36:12Z")

</div>

@JGKK thanks now i get it. I will fix it. But yeah also you are right on this '"The question is are you actually expecting the global var that you get in the previous function to be an array of several equations? So than the error would be where ever you are creating/setting that global var and not here." I need some help with this, I know that is a text there is a way to convert this in to equation function and resolve them?  
Also another thing that i realize is i will insert maybe per month 3 or 4 new equations and i need to save each like new variables there is a way to do that? to create like a database of new equations and have my dropdown with the old ones and the new equations that i need?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 March 2020 20:37 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/16 "2020-03-30T20:37:10Z")

</div>

What do you expect 'global.equa' to be after entering two equations in the text input?

Next try it and then go look at what the global holds. ![Screen Shot 2020-03-30 at 4.36.40 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e1e6f9d79ff9527a3655c4ee131c686d5d8b843d.png)

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [31 March 2020 05:45 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/17 "2020-03-31T05:45:40Z")

</div>

@zenofmud well now the ecuation appears as i want but after entering the equation i was expecting that was saved on the database and when i entering a new equation both appears as options but it wasn't like that

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 March 2020 09:58 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/18 "2020-03-31T09:58:04Z")

</div>

> [@lalo](#):
>
> i was expecting that was saved on the database

So you want to  
1 - save each equation to the global variable as an entry in array  
2 - at the end of the day, you want to write that array to a file  
3 - whenever you start NR you want to read the file and write it back out to the global variable.

So the first step is to get (1) working. to do this I would ignore the `ui_text` node for the moment and add three injects with three equations in them and feed them to the function node. The other thing is to set a variable to the global but if it is empty initialize the typw as an array. Look up the javascript logical OR (`||`). You can use it in an assignment. Take a look at this code, try it and then change x to equal 5 and try it again

```auto
var x = 3
msg.payload = (x==3) ||[1,2,3]
return msg

```

So you understand what is happening and how you could use this withthe global variable?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [31 March 2020 19:02 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/19 "2020-03-31T19:02:27Z")

</div>

1 - save each equation to the global variable as an entry in array  
2 - at the end of the day, you want to write that array to a file  
3 - whenever you start NR you want to read the file and write it back out to the global variable.  
actually this is what i want.  
I tried it and i think I get what you say with the array so OR ( | | ). will give me an assigment. But how can I use it?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 March 2020 20:18 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/20 "2020-03-31T20:18:45Z")

</div>

When you get the global use this `var arr = global.get("equa")||[]`  
If the global does not exist, it will set `arr` to an empty array then you will want to push the equation in msg.payload on to the array.

If you don't know how to do that do a google search using `javascript push w3schools`

[Next page](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812.md?page=2)
