Why is my function not working? If I input 10 lines it works.
[{"id":"bc74df63.d1433","type":"function","z":"698281a9.1e1108","name":"Tabel","func":"m = msg.payload;\nif(m === 0 ){\n msg.payload=0;\n} \nif(m == 1 ){\n msg.payload=4;\n}\nif(m == 2 ){\n msg.payload=12;\n}\nif(m == 3 ){\n msg.payload=22;\n}\nif(m == 4 ){\n msg.payload=33;\n}\nif(m == 5 ){\n msg.payload=46;\n}\nif(m == 6 ){\n msg.payload=61;\n}\nif(m == 7 ){\n msg.payload=77;\n}\nif(m == 8 ){\n msg.payload=93;\n}\nif(m == 9 ){\n msg.payload=111;\n}\nif(m == 10 ){\n msg.payload=130;\n}\nif(m == 11 ){\n msg.payload=149;\n}\nif(m == 12 ){\n msg.payload=170;\n}\nif(m == 13 ){\n msg.payload=191;\n}\nif(m == 14 ){\n msg.payload=213;\n}\nif(m == 15 ){\n msg.payload=236;\n}\nif(m == 16 ){\n msg.payload=259;\n}\nif(m == 17 ){\n msg.payload=283;\n}\nif(m == 18 ){\n msg.payload=307;\n}\nif(m == 19 ){\n msg.payload=332;\n}\nif(m == 20 ){\n msg.payload=358;\n}\nif(m == 21 ){\n msg.payload=384;\n}\nif(m == 22 ){\n msg.payload=411;\n}\nif(m == 23 ){\n msg.payload=438;\n}\nif(m == 24 ){\n msg.payload=466;\n}\nif(m == 25 ){\n msg.payload=494;\n}\nif(m == 26 ){\n msg.payload=522;\n}\nif(m == 27 ){\n msg.payload=551;\n}\nif(m == 28 ){\n msg.payload=580;\n}\nif(m == 29 ){\n msg.payload=610;\n}\nif(m == 30 ){\n msg.payload=640;\n}\nif(m == 31 ){\n msg.payload=670;\n}\nif(m == 32 ){\n msg.payload=701;\n}\nif(m == 33 ){\n msg.payload=732;\n}\nif(m == 34 ){\n msg.payload=764;\n}\nif(m == 35 ){\n msg.payload=795;\n}\nif(m == 36 ){\n msg.payload=827;\n}\nif(m == 37 ){\n msg.payload=859;\n}\nif(m == 38 ){\n msg.payload=892;\n}\nif(m == 39 ){\n msg.payload=924;\n}\nif(m == 40 ){\n msg.payload=957;\n}\nif(m == 41 ){\n msg.payload=990;\n}\nif(m == 42 ){\n msg.payload=1024;\n}\nif(m == 43 ){\n msg.payload=1057;\n}\nif(m == 44 ){\n msg.payload=1091;\n}\nif(m == 45 ){\n msg.payload=1125;\n}\nif(m == 46 ){\n msg.payload=1159;\n}\nif(m == 47 ){\n msg.payload=1193;\n}\nif(m == 48 ){\n msg.payload=1228;\n}\nif(m == 49 ){\n msg.payload=1262;\n}\nif(m == 50 ){\n msg.payload=1297;\n} \nif(m == 51 ){\n msg.payload=1332;\n}\nif(m == 52 ){\n msg.payload=1367;\n}\nif(m == 53 ){\n msg.payload=1402;\n}\nif(m == 54 ){\n msg.payload=1437;\n}\nif(m == 55 ){\n msg.payload=1472;\n}\nif(m == 56 ){\n msg.payload=1507;\n}\nif(m == 57 ){\n msg.payload=1543;\n}\nif(m == 58 ){\n msg.payload=1578;\n}\nif(m == 59 ){\n msg.payload=1614;\n}\nif(m == 60 ){\n msg.payload=1649;\n} \nif(m == 61 ){\n msg.payload=1685;\n}\nif(m == 62 ){\n msg.payload=1720;\n}\nif(m == 63 ){\n msg.payload=1756;\n}\nif(m == 64 ){\n msg.payload=1791;\n}\nif(m == 65 ){\n msg.payload=1827;\n}\nif(m == 66 ){\n msg.payload=1862;\n}\nif(m == 67 ){\n msg.payload=1898;\n}\nif(m == 68 ){\n msg.payload=1933;\n}\nif(m == 69 ){\n msg.payload=1969;\n}\nif(m == 70 ){\n msg.payload=2004;\n}\nif(m == 71 ){\n msg.payload=2040;\n}\nif(m == 72 ){\n msg.payload=2075;\n}\nif(m == 73 ){\n msg.payload=2110;\n}\nif(m == 74 ){\n msg.payload=2145;\n}\nif(m == 75 ){\n msg.payload=2180;\n}\nif(m == 76 ){\n msg.payload=2215;\n}\nif(m == 77 ){\n msg.payload=2250;\n}\nif(m == 78 ){\n msg.payload=2285;\n}\nif(m == 79 ){\n msg.payload=2319;\n}\nif(m == 80 ){\n msg.payload=2354;\n}\nelse msg.payload=\"fout\"\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":100,"wires":[["3f485885.cf3978"]]}]
Whilst sharing the full export is helpful it does mean we have to import it to see your Function node code. Plus consider sharing the code (if it isn't too long...) as well.
You also need to explain what you expect the function to do and what about it isn't working. Other wise we don't know where to start.
m = msg.payload;
msg.payload = "fout";
the rest of your if statements
or something like this
m = msg.payload;
if (m > 80) {
msg.payload = "fout";
return msg;
}
var data = [0,4,12,22..... and the rest of them];
msg.payload = data[m];
return msg;
Look at your code. What happens if msg.payload = 4 and what do you expect to happen?
Put a debug on the output of the function node and look at what comes out and ask yourself:
"Why did I get this result?"
then go look at your code again.
I always love the way you try to help. Not given the solution but let us search the solution! Love it.
I removed the last line
else msg.payload="fout"
and now it's working.
1 Like
I have a gasoline tank and I measure with a MQTT device the centimeters of gasoline left in the tank.
From the MQTT device (esp8266) I get the height in centimeters in my node red.
I know that 1 cm of gasoline is 4 liter, 2 cm is 12 liter, 3cm is 22 liter and so on...
I would like to display in my dashboard the liters and not the centimeters height.
With the code below I always get "fout"
My function below:
m = msg.payload;
if(m === 0 ){
msg.payload=0;
}
if(m == 1 ){
msg.payload=4;
}
if(m == 2 ){
msg.payload=12;
}
if(m == 3 ){
msg.payload=22;
}
if(m == 4 ){
msg.payload=33;
}
if(m == 5 ){
msg.payload=46;
}
if(m == 6 ){
msg.payload=61;
}
if(m == 7 ){
msg.payload=77;
}
if(m == 8 ){
msg.payload=93;
}
if(m == 9 ){
msg.payload=111;
}
if(m == 10 ){
msg.payload=130;
}
if(m == 11 ){
msg.payload=149;
}
if(m == 12 ){
msg.payload=170;
}
if(m == 13 ){
msg.payload=191;
}
if(m == 14 ){
msg.payload=213;
}
if(m == 15 ){
msg.payload=236;
}
if(m == 16 ){
msg.payload=259;
}
if(m == 17 ){
msg.payload=283;
}
if(m == 18 ){
msg.payload=307;
}
if(m == 19 ){
msg.payload=332;
}
if(m == 20 ){
msg.payload=358;
}
if(m == 21 ){
msg.payload=384;
}
if(m == 22 ){
msg.payload=411;
}
if(m == 23 ){
msg.payload=438;
}
if(m == 24 ){
msg.payload=466;
}
if(m == 25 ){
msg.payload=494;
}
if(m == 26 ){
msg.payload=522;
}
if(m == 27 ){
msg.payload=551;
}
if(m == 28 ){
msg.payload=580;
}
if(m == 29 ){
msg.payload=610;
}
if(m == 30 ){
msg.payload=640;
}
if(m == 31 ){
msg.payload=670;
}
if(m == 32 ){
msg.payload=701;
}
if(m == 33 ){
msg.payload=732;
}
if(m == 34 ){
msg.payload=764;
}
if(m == 35 ){
msg.payload=795;
}
if(m == 36 ){
msg.payload=827;
}
if(m == 37 ){
msg.payload=859;
}
if(m == 38 ){
msg.payload=892;
}
if(m == 39 ){
msg.payload=924;
}
if(m == 40 ){
msg.payload=957;
}
if(m == 41 ){
msg.payload=990;
}
if(m == 42 ){
msg.payload=1024;
}
if(m == 43 ){
msg.payload=1057;
}
if(m == 44 ){
msg.payload=1091;
}
if(m == 45 ){
msg.payload=1125;
}
if(m == 46 ){
msg.payload=1159;
}
if(m == 47 ){
msg.payload=1193;
}
if(m == 48 ){
msg.payload=1228;
}
if(m == 49 ){
msg.payload=1262;
}
if(m == 50 ){
msg.payload=1297;
}
if(m == 51 ){
msg.payload=1332;
}
if(m == 52 ){
msg.payload=1367;
}
if(m == 53 ){
msg.payload=1402;
}
if(m == 54 ){
msg.payload=1437;
}
if(m == 55 ){
msg.payload=1472;
}
if(m == 56 ){
msg.payload=1507;
}
if(m == 57 ){
msg.payload=1543;
}
if(m == 58 ){
msg.payload=1578;
}
if(m == 59 ){
msg.payload=1614;
}
if(m == 60 ){
msg.payload=1649;
}
if(m == 61 ){
msg.payload=1685;
}
if(m == 62 ){
msg.payload=1720;
}
if(m == 63 ){
msg.payload=1756;
}
if(m == 64 ){
msg.payload=1791;
}
if(m == 65 ){
msg.payload=1827;
}
if(m == 66 ){
msg.payload=1862;
}
if(m == 67 ){
msg.payload=1898;
}
if(m == 68 ){
msg.payload=1933;
}
if(m == 69 ){
msg.payload=1969;
}
if(m == 70 ){
msg.payload=2004;
}
if(m == 71 ){
msg.payload=2040;
}
if(m == 72 ){
msg.payload=2075;
}
if(m == 73 ){
msg.payload=2110;
}
if(m == 74 ){
msg.payload=2145;
}
if(m == 75 ){
msg.payload=2180;
}
if(m == 76 ){
msg.payload=2215;
}
if(m == 77 ){
msg.payload=2250;
}
if(m == 78 ){
msg.payload=2285;
}
if(m == 79 ){
msg.payload=2319;
}
if(m == 80 ){
msg.payload=2354;
}
else msg.payload="fout"
return msg;
For this situation, take a look at the Javascript switch
:
The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.
Also, think carefully about what if (condition) {...} else {...}
means, and see if you can work out why you always get fout
returned.
or a table lookup with a function node...
const table = {
"0" : 0,
"1" : 4,
"2" : 12,
"3" : 22,
"4" : 33,
"5" : 46,
"6" : 61,
"7" : 77,
"8" : 93,
"9" : 111,
};
msg.payload= table[msg.payload];
return msg;
1 Like
So what happend if all the if statements are false??
Start by - after setting m = msg.payload, set msg.payload = "fout" then run your if statements, if any are true it will replace "fout" with a value...
However I like @Andrei idea better - more concise - I would add a slight modificaton and change
msg.payload= table[msg.payload]
to
msg.payload= table[msg.payload]||"fout";
so if the lookup fails it will set msg.payloud to "fout"
1 Like
... but table[msg.payload] || "fout";
will also return "fout' when the payload is 0 - so you need to handle that separately.
2 Likes
That's no problem. When tank is lower then x liter there is a telegram send to me.
Thanks all
system
Closed
17 March 2020 17:40
13
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.