# Python equation to node red function

**URL:** <https://discourse.nodered.org/t/python-equation-to-node-red-function/23668>\
**Category:** General\
**Created:** [26 March 2020 10:50 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668 "2020-03-26T10:50:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SmudgeMarxUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smudgemarxuk/32/29335_2.png) [@SmudgeMarxUK](https://discourse.nodered.org/u/SmudgeMarxUK)\
**Post date:** [26 March 2020 10:50 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/1 "2020-03-26T10:50:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 March 2020 11:20 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/2 "2020-03-26T11:20:53Z")

</div>

> [@SmudgeMarxUK](#):
>
> A leading decimal point can be confused with a dot: '.9'

Use 0.9 instead.

---

<div class="post-metadata">

**Author:** ![SmudgeMarxUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smudgemarxuk/32/29335_2.png) [@SmudgeMarxUK](https://discourse.nodered.org/u/SmudgeMarxUK)\
**Post date:** [26 March 2020 11:38 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/3 "2020-03-26T11:38:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [26 March 2020 13:59 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/4 "2020-03-26T13:59:22Z")

</div>

Show the javascript code you have written so far

---

<div class="post-metadata">

**Author:** ![SmudgeMarxUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smudgemarxuk/32/29335_2.png) [@SmudgeMarxUK](https://discourse.nodered.org/u/SmudgeMarxUK)\
**Post date:** [26 March 2020 14:24 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/5 "2020-03-26T14:24:47Z")

</div>

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](https://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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [26 March 2020 15:25 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/6 "2020-03-26T15:25:20Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdb6d1b11115c06cc8eab17779ed6a4e3cd0b10b.png)

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

```

---

<div class="post-metadata">

**Author:** ![SmudgeMarxUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smudgemarxuk/32/29335_2.png) [@SmudgeMarxUK](https://discourse.nodered.org/u/SmudgeMarxUK)\
**Post date:** [26 March 2020 15:33 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/7 "2020-03-26T15:33:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 March 2020 16:04 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/8 "2020-03-26T16:04:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SmudgeMarxUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smudgemarxuk/32/29335_2.png) [@SmudgeMarxUK](https://discourse.nodered.org/u/SmudgeMarxUK)\
**Post date:** [26 March 2020 16:06 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/9 "2020-03-26T16:06:03Z")

</div>

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

---

<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:** [25 May 2020 16:06 UTC](https://discourse.nodered.org/t/python-equation-to-node-red-function/23668/10 "2020-05-25T16:06:05Z")

</div>

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