Write a "function" if/then/else with 2 variables

#1

hi

I'm still a "greenhorn" and have to learn, I known, but I have a problem and will ask for help :face_with_raised_eyebrow:

may following model/example:

I want make a inhibitor, this means, as long the "counter" is 0, the "Result" stay low (0) when "Start's" chance from low to high and to low (0-1-0-1-0...)
only the "counter is 1 (or higher) the "Result" should chance the value (0 to 1)

[{"id":"f3bf11b5.f40c38","type":"function","z":"9c52b872.61fc1","name":"","func":"if ( msg.Flag === 0 ) {\n return [mgs, null ];\n}\n else {\n msg.Start = true\n return [ null, msg ];\n}\n","outputs":1,"noerr":0,"x":522.5,"y":1175,"wires":[["ec9ffa4.fd95b08"]]},{"id":"327bb24b.f44886","type":"inject","z":"9c52b872.61fc1","name":"","topic":"Start","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":104.5,"y":1166,"wires":[["f3bf11b5.f40c38"]]},{"id":"1924cd39.0a6bb3","type":"inject","z":"9c52b872.61fc1","name":"","topic":"Flag","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":105.5,"y":1263,"wires":[["bd378d47.6afc8"]]},{"id":"bd378d47.6afc8","type":"counter","z":"9c52b872.61fc1","inc":1,"name":"counter","x":345.5,"y":1265,"wires":[["f3bf11b5.f40c38","39d1796e.004f76"]]},{"id":"ec9ffa4.fd95b08","type":"debug","z":"9c52b872.61fc1","name":"Resultat","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":712.5,"y":1176,"wires":},{"id":"844e8fd1.6b91","type":"inject","z":"9c52b872.61fc1","name":"clear","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":102.5,"y":1321,"wires":[["e364d872.ef3958"]]},{"id":"e364d872.ef3958","type":"function","z":"9c52b872.61fc1","name":"","func":"msg.reset = true;\nreturn msg;","outputs":1,"noerr":0,"x":244.5,"y":1321,"wires":[["bd378d47.6afc8"]]},{"id":"39d1796e.004f76","type":"debug","z":"9c52b872.61fc1","name":"Couter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":537.5,"y":1263,"wires":}]

I'm looking forward to a assistanc, many thank's
Max

0 Likes

#2

So there's some basics that might make this easier...

Generally the important bit within the message that gets sent between nodes is expected to be in msg.payload if you want to move it to something else you need to use a change node to move it.

So your "Start" inject node is injecting a message which includes the following:
msg.payload = 1 ( as you have set Payload to be 1)
msg.topic = "Start"

Your function node will need to check for those or alternatively use a change node to move the values to where you want to be.
The second thing is that nodes generally have no memory of previous messages. So if you have two wires entering a node, and you need need to do something to other messages coming into that node you will need to store that value. See https://nodered.org/docs/writing-functions#storing-data (but actually easier to use a change node to store the value)

Finally in that function node you have return [mgs, null ]; (i'll assume that mgs is a typo) which return the msg to output 1 and won't return anything to output 2 ( see Multiple Outputs part of page linked above) but your function node only has one output so I'm not quite sure what you are trying to do here.

This page might be useful https://nodered.org/docs/user-guide/messages also there are a number of Node-RED tutorials online

0 Likes

#3

hi

thanks for answer

I like to have on this output a 0, when counter is 0 and Start is 1

the output should be 1, when counter is 1 (or higher) and Start brings 1

Müller Max

max.r.mueller@gmail.com

0 Likes

#4

mgs is a typo, it will be msg

but --- I can't follow also I have several times consult the page "storing-data", but not find the way

my meaning was, whit the counter to can set a Flag

Flag = 0 and Start = 1, output is 0
Flag is 1 and Start become 1, the output goes to 1

0 Likes

#5

I'd start by working through this tutorial
http://noderedguide.com/node-red-lecture-5-the-node-red-programming-model/
which should help you to understand some of the basics of how Node-RED works, of the Node-RED msg object and how to store context

0 Likes