# 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:** 16\
**Page:** 3

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

</div>

There is an error in your function. Do you see any errors or warnings beeing displayed on the left hand side of the code editor when you open it?  
What is the 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:** [12 April 2020 04:59 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/44 "2020-04-12T04:59:01Z")

</div>

@JGKK Johannes I tried with this one:

```auto
const nerdamer = global.get(“nerdamer”);
var x = nerdamer.solve('(x+1)*3=x-6', 'x');
msg.payload = x;
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:** [12 April 2020 07:23 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/45 "2020-04-12T07:23:27Z")

</div>

You need to read the documentation of nerdamer to use it, in this case  
[https://nerdamer.com/functions/solveFor.html](https://nerdamer.com/functions/solveFor.html)  
If you want to solve this equation for x you have to do something like this

```auto
const nerdamer = global.get("nerdamer");
const equation = "(x+1)*3=x-6";
const myvars = {x:"x"};
const evalequat = nerdamer(equation);
const solution = evalequat.solveFor(myvars["x"]).toString();
msg.payload = solution;
return msg;

```

Johannes

---

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

</div>

Are this the kind of equations you will have in you have in your final project?  
Because up until now we were always talking about equations like  
x=5\*(y/3)  
x=(y+6)\*7  
where you have one variable that will be the result of an equation that is on the other side of equal and includes another variable that you gave a value to.  
What will it be as this will decide about your future strategies in your project.

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:** [12 April 2020 18:11 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/47 "2020-04-12T18:11:29Z")

</div>

@JGKK Johannes The equations that I am going to use more are like these:

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

```

the equations will come out of linear regressions

---

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

</div>

So you need to know what y is for a certain value of x that you have? Can there be more variables on the equation side than x?

---

<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:** [12 April 2020 18:47 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/49 "2020-04-12T18:47:59Z")

</div>

@JGKK i will always solve for y. I am going to get the values ​​of x from a msg.payload from a ui\_number node.

---

<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:** [12 April 2020 19:12 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/50 "2020-04-12T19:12:56Z")

</div>

I have something to play for you;

```auto
[{"id":"4a79662.d4d4498","type":"function","z":"f954564f.03e718","name":"solve input","func":"const nerdamer = global.get(\"nerdamer\");\nconst solvethis = msg.payload.equation;\nconst variables = msg.payload.variables;\nconst test = nerdamer(solvethis,variables);\nmsg.payload = test.text();\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":1220,"wires":[["a93b1f86.c23498"]]},{"id":"19602587.0095ba","type":"ui_form","z":"f954564f.03e718","name":"","label":"","group":"3f992e0f.516542","order":1,"width":0,"height":0,"options":[{"label":"y =","value":"equation","type":"text","required":true,"rows":null},{"label":"Value of x etc =","value":"variables","type":"text","required":true,"rows":null}],"formValue":{"equation":"","variables":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":730,"y":1220,"wires":[["e5ce3345.e2b27","2afee1ad.cc1ce6"]]},{"id":"e5ce3345.e2b27","type":"function","z":"f954564f.03e718","name":"format input","func":"const lettersr = msg.payload.equation.match(/[a-z]/g);\nlet letters = [];\nlettersr.forEach(letter => {\n if(!letters.includes(letter)) letters.push(letter);\n})\nlet variablesr = [];\nif(msg.payload.variables.match(/\\;+/g)){\n variablesr = msg.payload.variables.split(\";\");\n} else {\n variablesr.push(msg.payload.variables);\n}\nlet variables = {};\nletters.forEach((letter,index) => {\n variables[letter] = parseFloat(variablesr[index]);\n});\nmsg.payload.variables = variables;\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":1220,"wires":[["4a79662.d4d4498","64ae15ac.03f71c","eb331539.5399c"]]},{"id":"a93b1f86.c23498","type":"ui_text","z":"f954564f.03e718","group":"3f992e0f.516542","order":4,"width":0,"height":0,"name":"","label":"y =","format":"{{msg.payload}}","layout":"row-spread","x":1290,"y":1220,"wires":[]},{"id":"2afee1ad.cc1ce6","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.equation","pt":"msg","to":"your equation","tot":"str"},{"t":"set","p":"payload.variables","pt":"msg","to":"var values left to right separated by \";\"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1220,"wires":[["19602587.0095ba"]]},{"id":"64ae15ac.03f71c","type":"ui_text","z":"f954564f.03e718","group":"3f992e0f.516542","order":2,"width":0,"height":0,"name":"","label":"equation:","format":"{{msg.payload.equation}}","layout":"row-spread","x":1120,"y":1280,"wires":[]},{"id":"eb331539.5399c","type":"function","z":"f954564f.03e718","name":"format for ui","func":"msg.payload = JSON.stringify(msg.payload.variables).replace(/[\\{\\}\\\"]+/g,\"\").replace(/\\:+/g,\" = \");\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":1340,"wires":[["d8859fc4.40ed18"]]},{"id":"d8859fc4.40ed18","type":"ui_text","z":"f954564f.03e718","group":"3f992e0f.516542","order":3,"width":0,"height":0,"name":"","label":"variables:","format":"{{msg.payload}}","layout":"row-spread","x":1290,"y":1340,"wires":[]},{"id":"d02b27b2.805ef","type":"ui_ui_control","z":"f954564f.03e718","name":"","events":"all","x":360,"y":1220,"wires":[["2afee1ad.cc1ce6"]]},{"id":"3f992e0f.516542","type":"ui_group","name":"Group 1","tab":"52521beb.5fd24c","order":1,"disp":true,"width":6},{"id":"52521beb.5fd24c","type":"ui_tab","z":"","name":"equation solver","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a44e8c8d6ba812e571c8a57ce058fa00634acc8.jpeg) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67e691e76d3edab04a88dbd566da92fbb42adcd7.jpeg) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/345dffcf420cbbc7361ccad5b5a50b853670b2d1.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f9590a5b664c5d803fa9b6d643352f303e1a5bb.jpeg)  
This is probably similar to what you need but you should be able to salvage/adapt everything you need from this.  
The screenshots shows you how to work it. you put in everything on the right side of the equation into the first field, you put any values of variables that are in that equation in order of appearance from left to right and separated by ";’s into the second field.  
Press submit and it gets solved.  
The first function mostly does stuff to actually format that stuff to work with nerdamer. The second one called solve input does what it says.  
Put a debug node on the format input function to see what you need as an input for the nerdamer functions.  
Have fun Johannes  
PS I build this a few days ago when you first brought up nerdamer to understand it myself so by now i can’t remember half of it myself 🙈

---

<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:** [13 April 2020 03:48 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/51 "2020-04-13T03:48:16Z")

</div>

@JGKK Johannes thank you so much this really helps me I'll try to do some changes to the things that I want to build but for sure this help me a lot.

---

<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 April 2020 18:50 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/52 "2020-04-30T18:50:23Z")

</div>

Hi, @JGKK @zenofmud I have a new problem, I was pretty sure that i was working good, I don't know what happend. This is the flow

```auto
[{"id":"4a0dc1db.6373e","type":"inject","z":"217dd16.38f122e","name":"Daily backup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":"","x":160,"y":340,"wires":[["fa85aa84.0a4638"]]},{"id":"fa85aa84.0a4638","type":"function","z":"217dd16.38f122e","name":"Prep data","func":"// global variables separated by semicolon to be saved\nvar globallist = \"equa\"\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":340,"wires":[["2c1fdad6.69b846"]]},{"id":"9b4b6a57.eb6778","type":"comment","z":"217dd16.38f122e","name":"Save global variables","info":"","x":160,"y":300,"wires":[]},{"id":"ca84e556.c9a598","type":"comment","z":"217dd16.38f122e","name":"Restore global variables","info":"","x":160,"y":380,"wires":[]},{"id":"790a4252.12c0bc","type":"inject","z":"217dd16.38f122e","name":"Startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":140,"y":420,"wires":[["9a04477d.284958"]]},{"id":"5d6f31de.662ed","type":"file","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","appendNewline":true,"createDir":false,"overwriteFile":"false","x":800,"y":320,"wires":[["5fe6e7f0.c44da8"]]},{"id":"9a04477d.284958","type":"file in","z":"217dd16.38f122e","name":"","filename":"/home/pi/global.json","format":"utf8","x":340,"y":420,"wires":[["8beb74b6.258248"]]},{"id":"fd2c7017.65bee","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":420,"wires":[["d4dfbe8f.e242f"]]},{"id":"2c1fdad6.69b846","type":"json","z":"217dd16.38f122e","name":"","property":"payload","action":"","pretty":false,"x":490,"y":340,"wires":[["5d6f31de.662ed"]]},{"id":"8beb74b6.258248","type":"json","z":"217dd16.38f122e","name":"","property":"payload","action":"str","pretty":false,"x":510,"y":420,"wires":[["fd2c7017.65bee"]]},{"id":"5fe6e7f0.c44da8","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1030,"y":320,"wires":[]},{"id":"4178edb7.43bda4","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":420,"wires":[["43476590.82e54c","150ef5c1.6a00ea"]]},{"id":"43476590.82e54c","type":"ui_toast","z":"217dd16.38f122e","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1390,"y":480,"wires":[]},{"id":"83c72504.6d9138","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;\n","outputs":1,"noerr":0,"x":990,"y":420,"wires":[["4178edb7.43bda4"]]},{"id":"4a9dc514.f12e2c","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":160,"wires":[["12c6d6f5.6d8499"]]},{"id":"2f5d1fcc.dfd9a","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":200,"wires":[["12c6d6f5.6d8499"]]},{"id":"bc5979b3.76c988","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":240,"wires":[["12c6d6f5.6d8499"]]},{"id":"d4dfbe8f.e242f","type":"function","z":"217dd16.38f122e","name":"","func":"var arr = global.get(\"equa\")||[];\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":420,"wires":[["83c72504.6d9138"]]},{"id":"12c6d6f5.6d8499","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":200,"wires":[["e971c6f1.ded758"]]},{"id":"e971c6f1.ded758","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":200,"wires":[["29bb44a0.dd201c"]]},{"id":"29bb44a0.dd201c","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":200,"wires":[]},{"id":"dc8a4c38.a4d1b","type":"comment","z":"217dd16.38f122e","name":"equation data in","info":"","x":140,"y":120,"wires":[]},{"id":"150ef5c1.6a00ea","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":240,"wires":[["89d2583.2b544a8"]]},{"id":"89d2583.2b544a8","type":"debug","z":"217dd16.38f122e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1570,"y":240,"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}]

```

So basically at the beggining of this topic I was asking for a dropdown. I have some equations as an input, this equations will become as my options in the dropdown.

 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3464a16679ca978279ab6f9928dc4f80779e749c.png) .  
The debbug is showing my equations that will become in to my options on the dropdown  
so I have two issues.  
 ![Sin título](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d37b0ee90317db3ac8df4ebbf8b1ecf1826168b0.png)  
The first one is this my equations appears as numbers but I don't want that, what I want them to appear like this y = 2.1425x + 3.8416, and the second issue is when I recover the equations from the json file they add this

---

<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 April 2020 20:36 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/53 "2020-04-30T20:36:02Z")

</div>

so have you tried adding a debug node to the output of the node feeding the ui node? what does it show you are sending to the dropdown?

---

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

</div>

yeah I already did that and it sent me my y = 2.1425x + 3.8416 equations

---

<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 April 2020 20:41 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/55 "2020-04-30T20:41:57Z")

</div>

can you please show exactly what is going into the dropdown  
also you should put a `debug` on the output of the dropdown 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:** [1 May 2020 19:00 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/56 "2020-05-01T19:00:59Z")

</div>

![Sin título](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46092f5410d71d247c2d22f04f7755a1dcd13bba.png)  
I put a debbug node before and after the dropdown.

---

<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:** [1 May 2020 20:21 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/57 "2020-05-01T20:21:16Z")

</div>

so now go look at the info tab on the ui-dropdown node. WWhat are you suppose to put in the msg being sent to the dropdown node and what are you sending?

---

<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:** [30 June 2020 20:29 UTC](https://discourse.nodered.org/t/solving-mathematical-functions-made-from-dashboard-inputs/23812/58 "2020-06-30T20:29:24Z")

</div>

Closed on 30 June 2020 20:29 UTC

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

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