Function Node Code issues

Hi All,

Wondering if someone could help me.

Trying to get this code working for a friend. It has 2 limits -150 and 150. Its adds the last number to the new number and outputs it but will only allow to stay with in limits. And has a reset back to 0.

For some reason this works on my Raspi Pi 4 but not on my friends computer.

[{"id":"d2492f6838bf6632","type":"function","z":"3a22222acce369c8","name":"","func":"var max = 500;\nvar min = global.get ('socvar');\nvar total = context.get('total');\nif (msg.reset) {\ntotal = 0;\n}\nelse {\ntotal = total + msg.payload\n}\nif (total > max) total = max;\nif (total < min) total = min;\ncontext.set('total', total);\nmsg.payload = total;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1400,"wires":[["252eefed571911b3"]]},{"id":"252eefed571911b3","type":"debug","z":"3a22222acce369c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1660,"y":1400,"wires":[]},{"id":"35eb22f321d14cdc","type":"inject","z":"3a22222acce369c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":1140,"y":1320,"wires":[["d2492f6838bf6632"]]},{"id":"736abd7f5ee98b8f","type":"inject","z":"3a22222acce369c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-10","payloadType":"num","x":1130,"y":1460,"wires":[["d2492f6838bf6632"]]}]

I did input this code into an online JSON checker and it flags some errors. https://jsonlint.com/

Says

Error: Parse error on line 1:
var max = 500;,var
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Would someone please help me debug this code please? Im not a coder :frowning:

Any help would be much appreciated :slight_smile:
Thanks

Gareth

What error does your friend get? as i see no errors. All i would say is, are their global context values set? as that would return a NAN, and that would be save to context total, from then it would always return NaN as number + Nan equals NaN
Ask you friend to delete the context value in the side bar, the function must be selected to see the functions context in side bar.
you could also add some defaults for first run if context and global are not set.

var max = 500;
var min = global.get ('socvar') || -150;
var total = context.get('total') || 0;
if (msg.reset) {
total = 0;
}
else {
total = total + msg.payload
}
if (total > max) total = max;
if (total < min) total = min;
context.set('total', total);
msg.payload = total;
return msg;

Just a note that if global value socvar was ever 0 then that would set min to -150.

Hi All thank you for your replies. Much appreciated. I amended the code. It appears the || 0 on line 3 fixed the issue.

var max = 500;
var min = -150;
var total = context.get('total') || 0;
if (msg.reset) {
total = 0;
}
else {
total = total + msg.payload
}
if (total > max) total = max;
if (total < min) total = min;
context.set('total', total);
msg.payload = total;
return msg;

Happy Days!