Solving mathematical functions made from dashboard inputs

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?

@JGKK Johannes I tried with this one:

const nerdamer = global.get(“nerdamer”);
var x = nerdamer.solve('(x+1)*3=x-6', 'x');
msg.payload = x;
return msg;

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

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

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

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

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

the equations will come out of linear regressions

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?

@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.

I have something to play for you;

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



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 :see_no_evil:

1 Like

@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.

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

[{"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.

.
The debbug is showing my equations that will become in to my options on the dropdown
so I have two issues.

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

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?

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

can you please show exactly what is going into the dropdown
also you should put a debug on the output of the dropdown node.


I put a debbug node before and after the dropdown.

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?

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