# 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:** 2

<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: [2 April 2020 05:31 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/21 "2020-04-02T05:31:45Z")

</div>

Hey @zenofmud Paul Thanks it does what i want but is still having a problem when the dropdown options appears on dashboar looks like this:  
 ![equation options dropdown](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3ac8842332bd62b499d79ac24e3ad226f61a47aa.png)  
As I understand that is a key object there is a way to change the 0 for example for the name equation sally and the for the 1 equation carl or something like that?  
here is the image of the debug  
 ![arrayequations](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a124ea68842f8573a70796965b5ed3a05e4addeb.png)

also I have another problem when i reboot my Raspberry my json file with the equations saved are like erased when i try to recover it in the debug appears empty i will put an screenshot and also the flow.  
 ![empty](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f32b95f4a46be03c9c94fe1e849c81c419da9b5.png)

```auto
[{"id":"2a76abae.3cb354","type":"inject","z":"217dd16.38f122e","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":"","x":160,"y":320,"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":340,"y":320,"wires":[["796ae060.2571d"]]},{"id":"722bde30.b4061","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":160,"y":280,"wires":[]},{"id":"82ed5561.8ea6c8","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":160,"y":360,"wires":[]},{"id":"623cfee.c4562","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":140,"y":400,"wires":[["ec4e4dad.53165"]]},{"id":"3cf22d71.3cf902","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"true","x":660,"y":320,"wires":[["41a8df7d.8a1b2"]]},{"id":"ec4e4dad.53165","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":340,"y":400,"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":640,"y":400,"wires":[["5bc9e96f.723ff8"]]},{"id":"796ae060.2571d","type":"json","z":"217dd16.38f122e","name":"","x":490,"y":320,"wires":[["3cf22d71.3cf902"]]},{"id":"cc306e1f.95de2","type":"json","z":"217dd16.38f122e","name":"","x":510,"y":400,"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":800,"y":400,"wires":[["e13d9371.87db7"]]},{"id":"41a8df7d.8a1b2","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":850,"y":320,"wires":[]},{"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":1360,"y":400,"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":1570,"y":460,"wires":[]},{"id":"93af67ea.420c68","type":"function","z":"217dd16.38f122e","name":"transform data to correct format","func":"msg.options = [];//create empty array\n let row = msg.payload; //get the row\n msg.options.push(row);//add the row to array \nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":400,"wires":[["73bebdc6.3a46b4"]]},{"id":"fda2e215.67a4","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1550,"y":340,"wires":[]},{"id":"6114a54b.88c9fc","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 2.1425x + 3.8416 ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":140,"wires":[["8300838.480678"]]},{"id":"33030a2a.bd0b66","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 4.4607x + 8.3455 ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":180,"wires":[["8300838.480678"]]},{"id":"4037b410.2ddfac","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 7.5648x + 13.75","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":220,"wires":[["8300838.480678"]]},{"id":"e13d9371.87db7","type":"function","z":"217dd16.38f122e","name":"","func":"var arr = global.get(\"equa\")||[];\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":400,"wires":[["93af67ea.420c68"]]},{"id":"8300838.480678","type":"function","z":"217dd16.38f122e","name":"","func":"if (!context.outputAlarms) {\n context.outputAlarms = [];\n}\ncontext.outputAlarms.push(msg.payload);\nmsg.payload = context.outputAlarms;\nreturn msg; ","outputs":1,"noerr":0,"x":430,"y":180,"wires":[["71ed98ef.8705c8","6d54e799.eb1b68"]]},{"id":"71ed98ef.8705c8","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":180,"wires":[]},{"id":"6d54e799.eb1b68","type":"function","z":"217dd16.38f122e","name":"","func":"var equa = msg.payload;\n\nglobal.set('equa',equa);\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":140,"wires":[["31a04691.d2251a"]]},{"id":"31a04691.d2251a","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":140,"wires":[]},{"id":"a89a8a34.044418","type":"comment","z":"217dd16.38f122e","name":"equation data in","info":"","x":140,"y":100,"wires":[]},{"id":"679f2509.be684c","type":"ui_group","z":"","name":"Default","tab":"658319a6.10c4d8","disp":true,"width":"6","collapse":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: [2 April 2020 09:39 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/22 "2020-04-02T09:39:59Z")

</div>

Lalo - why have you added another function numt in the first part? All you need is one function node with the following

```auto
var arr = global.get("equa")||[]
arr.push(msg.payload)
global.set('equa',arr)
return msg

```

Line 1 `var arr = global.get("equa")||[]` creates a variable 'arr' and the set it to the value of the global variable 'equa' or it that does not exist, it sets it to an empty array  
line 2 `arr.push(msg.payload)` adds what is in msg.payload to the end of the array  
line 3 `global.set('equa',arr)` writes the contents of 'arr' to the global variable 'equa'  
line 4 `return msg` returns the msg

If you want to, you could add `msg.payload = arr` before the `return msg` so the debug would show the results in the debug window.

> I have another problem when i reboot my Raspberry my json file with the equations saved are like erased

Are you saying that the file on your pi `/home/pi/global.json` is erased when you reboot? Or you talking about `global.equa`?

If it is `global.equa`, have you read the documentation on [Working with context?](https://nodered.org/docs/user-guide/context) You should read the section on `Saving context data to the file-system` so if you reboot your pi, the data will be saved.

UPDATE: put a debug node after the 'Restore' function and a second after the 'equation restore' function (both set to display the `Complete msg object`) and see if you are getting the results you 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: [2 April 2020 20:48 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/23 "2020-04-02T20:48:40Z")

</div>

> Are you saying that the file on your pi `/home/pi/global.json` is erased when you reboot? Or you talking about `global.equa` ?

No i mean the `/home/pi/global.json` is not erased but the data inside is erased i'm following your advices thanks.

---

<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: [2 April 2020 21:31 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/24 "2020-04-02T21:31:58Z")

</div>

> [@lalo](#):
>
> No i mean the `/home/pi/global.json` is not erased but the data inside is erased i'm following your advices thanks.

If the data is being erased then your backup flow is writting empty data to it. Since you tell it to `overwrite` the file each time. So now you have to determine why is it writing empty data?

If you have not specified (in settings.js) that the context variables should be persistant and you stop/start node red, then they go away. This is probably what is causing the issue.

---

<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: [4 April 2020 04:24 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/25 "2020-04-04T04:24:21Z")

</div>

@zenofmud @JGKK. Paul and Johannes thank you i read all that you told me and it works how i want, now i'm in the part after selecting my dropdown options and i'm using a case function to make a simple sum but that's just to see how it works so basically i want to do 2 things. The first one i don't know if it's possible to create automatic cases when is added a new equation. For example if i add this ecuation `y = 4.4607x + 8.3455 `will create a new case and if it is selected i can resolve the ecuation for example if i choose `y = 4.4607x + 8.3455 ` and it appears on the dropdown in a function it substitute the values ​​in x to solve it .  
Best regards from Lalo this is my current 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":160,"y":320,"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","outputs":1,"noerr":0,"x":340,"y":320,"wires":[["796ae060.2571d"]]},{"id":"722bde30.b4061","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":160,"y":280,"wires":[]},{"id":"82ed5561.8ea6c8","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":160,"y":360,"wires":[]},{"id":"623cfee.c4562","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":140,"y":400,"wires":[["ec4e4dad.53165"]]},{"id":"3cf22d71.3cf902","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"false","x":800,"y":300,"wires":[["41a8df7d.8a1b2"]]},{"id":"ec4e4dad.53165","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":340,"y":400,"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":640,"y":400,"wires":[["e13d9371.87db7"]]},{"id":"796ae060.2571d","type":"json","z":"217dd16.38f122e","name":"","property":"payload","action":"","pretty":false,"x":490,"y":320,"wires":[["3cf22d71.3cf902"]]},{"id":"cc306e1f.95de2","type":"json","z":"217dd16.38f122e","name":"","property":"payload","action":"str","pretty":false,"x":510,"y":400,"wires":[["baebe8d4.f4d4e8"]]},{"id":"41a8df7d.8a1b2","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1030,"y":300,"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":160,"y":40,"wires":[["a9476aaf.676e58"]]},{"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":1200,"y":400,"wires":[["810a2cc2.b603c","28fcfe2f.75d1b2"]]},{"id":"810a2cc2.b603c","type":"ui_toast","z":"217dd16.38f122e","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1390,"y":460,"wires":[]},{"id":"93af67ea.420c68","type":"function","z":"217dd16.38f122e","name":"transform data to correct format","func":"msg.options = [];//create empty array\n let row = msg.payload; //get the row\n msg.options.push(row);//add the row to array \nreturn msg;","outputs":1,"noerr":0,"x":990,"y":400,"wires":[["73bebdc6.3a46b4"]]},{"id":"6114a54b.88c9fc","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 2.1425x + 3.8416","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":140,"wires":[["8300838.480678"]]},{"id":"33030a2a.bd0b66","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 4.4607x + 8.3455","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":180,"wires":[["8300838.480678"]]},{"id":"4037b410.2ddfac","type":"inject","z":"217dd16.38f122e","name":"","topic":"","payload":"y = 7.5648x + 13.75","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":220,"wires":[["8300838.480678"]]},{"id":"e13d9371.87db7","type":"function","z":"217dd16.38f122e","name":"","func":"var arr = global.get(\"equa\")||[];\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":400,"wires":[["93af67ea.420c68"]]},{"id":"8300838.480678","type":"function","z":"217dd16.38f122e","name":"","func":"if (!context.outputAlarms) {\n context.outputAlarms = [];\n}\ncontext.outputAlarms.push(msg.payload);\nmsg.payload = context.outputAlarms;\nreturn msg; ","outputs":1,"noerr":0,"x":430,"y":180,"wires":[["6d54e799.eb1b68"]]},{"id":"a9476aaf.676e58","type":"function","z":"217dd16.38f122e","name":"","func":"if (!context.outputAlarms) {\n context.outputAlarms = [];\n}\ncontext.outputAlarms.push(msg.payload);\nmsg.payload = context.outputAlarms;\nreturn msg; ","outputs":1,"noerr":0,"x":390,"y":40,"wires":[["9fee7d48.ba5d8"]]},{"id":"9fee7d48.ba5d8","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":40,"wires":[]},{"id":"6d54e799.eb1b68","type":"function","z":"217dd16.38f122e","name":"","func":"var equa = msg.payload;\n\nglobal.set('equa',equa);\n\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":180,"wires":[["31a04691.d2251a"]]},{"id":"31a04691.d2251a","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":180,"wires":[]},{"id":"a89a8a34.044418","type":"comment","z":"217dd16.38f122e","name":"equation data in","info":"","x":140,"y":100,"wires":[]},{"id":"28fcfe2f.75d1b2","type":"function","z":"217dd16.38f122e","name":"dropdown case","func":"let a = msg.payload;\nvar n = 2;\nvar m = 3;\nswitch (a){\n case \"y = 4.4607x + 8.3455 \" :\n msg.payload = n+m;\n\t\treturn msg;\n\t\n\tcase \"y = 2.1425x + 3.8416 \":\n\t msg.payload = m-n;\n\t\treturn msg;\n}","outputs":1,"noerr":0,"x":1380,"y":220,"wires":[["6e94da70.475954"]]},{"id":"6e94da70.475954","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1570,"y":220,"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: ![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: [4 April 2020 07:11 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/26 "2020-04-04T07:11:26Z")

</div>

Please explain how you would solve the equation on paper. Give a detailed step by step explanation

---

<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: [6 April 2020 04:15 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/27 "2020-04-06T04:15:29Z")

</div>

@zenofmud Paul something really simple like this.

![solve](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da19986582bcd0fa5777ac1e085f9062a5297a2d.png)

---

<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: [6 April 2020 09:35 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/28 "2020-04-06T09:35:07Z")

</div>

Where did `x` come from? How does the value of `x` get entered? This is something new that is not shown in the flow at all.

So you are storing a formula as a string. To solve the formula you will first need to parse the string so you know what computation needs to be done.

But before that, you need to validate you have a valid formula? Say someone entered a formula  
`4.4607x + 8.3455 = y` or `y = 4.4607x + 8.3455` or there is a typo like `y + 4.4607x + 8.3455` or they enter `animals equals six dogs added to three cats`?

So you figure out  
1 - how to validate the text string to see if it is a valid formula when it is entered  
2 - how to parse the string formula and create the logic to handle all the cases it could be (is it doing + or - or / etc, could there a number raised to a power?)  
3 - where `x` is coming from  
4 - could there be more than one variable for the formula for example `y = 4.4607x + 8.3455z`

You have your work cut out for you. Feel free to ask questions and provide the code you are having issues with.

---

<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: [7 April 2020 05:10 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/29 "2020-04-07T05:10:27Z")

</div>

Hi @zenofmud Paul.

1. I have seen some documentation on excel to validate with some rules. But i just have seen some buttons and commands to see if text or dates are validate.
2. I think i will only have linear regression because i will have some data from excel and get the equation.
3. X is coming from a number input on the dashboard
4. i think i could't be more than one variable.

---

<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: [7 April 2020 05:48 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/30 "2020-04-07T05:48:43Z")

</div>

you are building a kind of calculator. I think you will have to build something from ground up that tokenizes your expression string and than validates to things like numbers decimals and operands.  
This is not something that is build into nodered or javascript but it can be done.  
Search goggle for javascript calculator and you will find many tutorials you can learn from.

---

<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: [10 April 2020 04:49 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/31 "2020-04-10T04:49:44Z")

</div>

@JGKK i was looking for some kind of calculator and i found this that i think is really close to what i want.

[https://www.youtube.com/watch?v=4GCC5DBh\_Pk](https://www.youtube.com/watch?v=4GCC5DBh_Pk)

also i found this code:

```auto
//solve linear equations
var x = nerdamer.solve('(x+1)*3=x-6', 'x');
console.log(x.toString());
//quadratic
var x2 = nerdamer.solve('x^2-8x+15', 'x');
console.log(x2.toString());
//quadratic algebraically
var x3 = nerdamer.solve('x^2-ax+3*b', 'x');
console.log(x3.toString());
//multiple roots
var x4 = nerdamer.solve('x^6+41*x^5+652*x^4+5102*x^3+20581*x^2+40361*x+30030', 'x');
console.log(x4.toString());
//functions - numerically around to zero up to a predefined range
var x5 = nerdamer.solve('cos(x)^2+sin(x-1)', 'x');
console.log(x5.toString());
//solve a system of linear equations
var x6 = nerdamer.solveEquations(['2x+y=7', 'x-y+3z=11', 'y-z=-1']);
console.log(x6.toString());
//solve a system of nonlinear equations
var x7 = nerdamer.solveEquations(['3*x^2/y=2', 'z*x*y-1=35', '5*z^2+7=52']);
console.log(x7.toString());

```

---

<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: [10 April 2020 05:04 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/32 "2020-04-10T05:04:06Z")

</div>

and also this.

> <https://gist.github.com/torjusti/cd66074e4273a6d318af>

---

<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: [10 April 2020 07:26 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/33 "2020-04-10T07:26:02Z")

</div>

The first code you found is nearly it. It uses an npm module [https://www.npmjs.com/package/nerdamer](https://www.npmjs.com/package/nerdamer) which does exactly what you want. Good find.  
Now you have some more Homework.  
1st you need to learn how to install external modules so that you can use them in Nodered.  
2nd install the Nerdamer Module as per these instructions.  
3rd read the documentation of Nerdamer [https://nerdamer.com/documentation.html](https://nerdamer.com/documentation.html) which fortunately seam to be very good and figure out how to use it.  
To get you started here is the Nodered documentation on how to use external modules in functions:  
[https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

Have fun and stay healthy, Johannes

---

<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: [11 April 2020 04:58 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/34 "2020-04-11T04:58:10Z")

</div>

Hey @JGKK Johannes Thanks but I'm having troubles to install npm.

![npm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5ab481a6b3ab31dd8294c2052786a4dd82471c6c.png)

---

<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: [11 April 2020 05:14 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/35 "2020-04-11T05:14:16Z")

</div>

you are using the wrong command. Use it like it says on the npm website link i posted before [https://www.npmjs.com/package/nerdamer](https://www.npmjs.com/package/nerdamer) on the top right `npm i nerdamer`

---

<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: [11 April 2020 05:35 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/36 "2020-04-11T05:35:22Z")

</div>

@JGKK Johannes yeah I realized that the code was in the upper corner. Sorry and Thanks I'm going to try with the nerdamer.

---

<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: [11 April 2020 05:37 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/37 "2020-04-11T05:37:10Z")

</div>

you will find this very helpful for what you need to enter in your settings.js file as its otherwise not very clear in the documentation of nerdamer:

> <https://github.com/jiggzson/nerdamer/issues/113>
>
> ref #112:
> I'm sorry that it wasn't clear that Solve depends on Algebra and Calculus (I noticed that in your comments).
> I read...

  
edit:  
feel free to ask if you have more question as i already have it up and running

---

<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: [11 April 2020 20:28 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/38 "2020-04-11T20:28:56Z")

</div>

I have a doubt after installing in a page i found that i have to do something like this...

 ![install](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d534ecccb1f810f41c9b93f70c0fb1b1c16e7b07.png)

I need some help with that I don' want to mess it up

---

<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: [11 April 2020 20:32 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/39 "2020-04-11T20:32:10Z")

</div>

```auto
    functionGlobalContext: {
        nerdamer:require('nerdamer/all')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({r$
    },

```

you can than access it with

```auto
const nerdamer = global.get(“nerdamer”);

```

in a function node

---

<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: [11 April 2020 21:24 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/40 "2020-04-11T21:24:24Z")

</div>

![Sin título](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af86b1d2e546881c17c21dc61fa75dcd71c4d135.png)  
I don't know if I did something wrong.

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

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