Python equation to node red function

Hi all, I'm trying to convert part of a python script, i wrote, to a node red function...but i'm struggling to find to best method to do this. I'm a super noob to coding & I'm attempting to get the basics down with Javascript, but a push in the right direction would be greatly appreciated.

the relevant part of the code is(after 'global Std'):
compvalue = avg / 60
global Std
if (compvalue <= Std/.9) and (compvalue > Std/.97):
PackBtn.configure(background = "#ffa500")
elif (compvalue <= Std/.97) and (compvalue >= Std/1.1):
PackBtn.configure(background = "#00ff00")
elif Std/.9 < compvalue:
PackBtn.configure(background = "red")
else:
PackBtn.configure(background = "#00e5e5")

The 'PackBtn' is a GPIO button, with it's delta being compared to a standard time. With the awful conversions i've attempted, I get the 'i' (in the function node) warning me:
"A leading decimal point can be confused with a dot: '.9'"

& also configuring the colours to a group background, have given me problems

Thanks to anyone who can help me crack this.

Use 0.9 instead.

Thanks, that did help to remove the 'i' warning, just have to figure out the rest conversion.

Show the javascript code you have written so far

This is one iteration I've been tinkering with:

var std = context.node || 0;
var act = context.node1 || 0;
//var topic = msg.topic;
if (msg.topic == 'Get Std'){
std = msg.payload;
} else if (msg.topic == 'Avg'){
act = msg.payload;
}
if (act <= std/0.9 && act <= std/0.97)
return{ topic: 'Result/A', payload: "Orange"}
} else if (act <= std/0.97 && act <= std/1.1){
return{ topic: 'Result/B', payload: "Green"}
} else if (std/0.9 < act){
return{ topic: 'Result/B', payload: "Red"}
}
else{
return{ topic: 'RESULT/d', payload: "#00e5e5"}
}
//msg.topic = "SendVar"
//return msg;

I was trying to expand on a function i watched on "Node Red for Dummies", where he compared values as Equal/unequal...but I'm not up to scratch yet & haven't found any videos/articles that get me to the specifics i need.

I'm not one hundred sure what you wanna calculate, so I'm assuming a bit here that you want to check if a value lies between certain limits and then, based on that, wanna set some colours

Here is a sample based on yours that is checking that. Please note, check out the details in the documentation how to use data from context (get and set)

Also check out how to share code and flows correctly. You can try the flow below and see if this is in line with what you are looking for

[{"id":"918a7aca.3af0a8","type":"function","z":"a4d7c0e0.63723","name":"","func":"var std = context.get('node') || 0;\nvar act = context.get('node1') || 0;\n//var topic = msg.topic;\nif (msg.topic === 'Get Std'){\n    std = msg.payload;\n    context.set('node', std);\n} else if (msg.topic === 'Avg'){\n    act = msg.payload;\n    context.set('node1', act);\n}\nif (act <= std/0.9 && act >= std/0.97){\n    return ({ topic: 'Result/A', payload: \"Orange\"});\n} else if (act < std/0.97 && act >= std/1.1){\n    return ({ topic: 'Result/B', payload: \"Green\"});\n} else if (act > std/0.9){\n    return ({ topic: 'Result/B', payload: \"Red\"});\n} else {\nreturn ({ topic: 'RESULT/d', payload: \"#00e5e5\"});\n}\n//msg.topic = \"SendVar\"\n//return msg;","outputs":1,"noerr":0,"x":870,"y":280,"wires":[["c795a02f.0ea36"]]},{"id":"701abf6b.c3cb6","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"Get Std","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":650,"y":110,"wires":[["918a7aca.3af0a8"]]},{"id":"c795a02f.0ea36","type":"debug","z":"a4d7c0e0.63723","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1100,"y":300,"wires":[]},{"id":"3fa714b2.6e31ec","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"Avg","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":640,"y":230,"wires":[["918a7aca.3af0a8"]]},{"id":"87d51a33.905718","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"Avg","payload":"5.0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":640,"y":330,"wires":[["918a7aca.3af0a8"]]},{"id":"be1cb977.613378","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"Avg","payload":"5.25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":650,"y":280,"wires":[["918a7aca.3af0a8"]]},{"id":"edbd542e.a4fbd8","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"Avg","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":640,"y":380,"wires":[["918a7aca.3af0a8"]]}]

Thanks alot, this is awesome & what i was aiming for. There's a hell of a lot I've got to learn...& i will follow you're advice. Top man

For future reference, please format code as pre-formatted so that it is more readable. Thanks.

Will in future, I'm reading up on it now...

1 Like