AND gate inputs

#1

Hi
I have this flow below and i want to put AND gate so when the output of the function (Calculate Percentage) decreased to 10 the AND gate gives output so I can run on of GPIOs of the raspberry pi.
I put the AND gate and set the topics for the (Calculate Percentage) with number less than 10 and Inject node (less) with number ==10.
but I think there is something wrong.
please advise where is the mistake in this setting

[{"id":"b35f6956.6db678","type":"rpi-srf","z":"dae97729.e9eed8","name":"Sensor","topic":"","pulse":"0.5","pins":"37,38","x":108,"y":155,"wires":[["616ac931.29b938"]]},{"id":"616ac931.29b938","type":"function","z":"dae97729.e9eed8","name":"Calculate Percentage","func":"var distance = parseInt(msg.payload);\nvar EmptyDist = 100 -distance;\nmsg.payload = parseInt((EmptyDist/100)*100);\n//msg.payload =(100- parseInt((temp/125)*100));\nreturn msg;\n","outputs":1,"noerr":0,"x":309,"y":155,"wires":[["cd2a360c.cbed68","f1ae908b.e85ce"]]},{"id":"39f04960.f55496","type":"debug","z":"dae97729.e9eed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":628,"y":252,"wires":[]},{"id":"cd2a360c.cbed68","type":"ui_gauge","z":"dae97729.e9eed8","name":"","group":"2cad78f1.953508","order":0,"width":"6","height":"7","gtype":"gage","title":"Tank Filled","label":"Percentage","format":"{{value}}","min":0,"max":"100","colors":["#b52100","#e6e600","#42ca38"],"seg1":"","seg2":"","x":661,"y":120,"wires":[]},{"id":"f1ae908b.e85ce","type":"and-gate","z":"dae97729.e9eed8","name":"","rules":[{"t":"lt","v":"10","vt":"num","propertyType":"msg","property":"payload","topic":"Calculate Percentage"},{"t":"eq","v":"10","vt":"num","propertyType":"msg","property":"payload","topic":"less"}],"outputTopic":"","gateType":"and","emitOnlyIfTrue":false,"x":373,"y":267,"wires":[["39f04960.f55496"]]},{"id":"8c0273fa.30168","type":"inject","z":"dae97729.e9eed8","name":"","topic":"less","payload":"10","payloadType":"num","repeat":".2","crontab":"","once":false,"onceDelay":0.1,"x":121,"y":301,"wires":[["f1ae908b.e85ce"]]},{"id":"2cad78f1.953508","type":"ui_group","z":"","name":"Mustafa","tab":"9f2b0241.7a073","disp":true,"width":"6","collapse":false},{"id":"9f2b0241.7a073","type":"ui_tab","z":"","name":"Home","icon":"my dash","order":2}]

#2

Which And gate are you using, you presumably installed node-red-contrib-something?
Where are you setting the topic for the Calculate Percentage input?

#3

AND gate from node-red-contrib-bool-gate
please see the screenshot

#4

So you are using an AND gate and the output will be true if both conditions are true
However you are testing msg.payload to be equal to 10 AND less than 10

do you see an issue with that?

#5

@zenofmud That's a perfectly valid AND gate configuration. The topics are read separately. It's a workaround for nodered's lack of multiple-input nodes.

@mustafa The topic of the sensor node is blank. Based on the gate configuration, it should be "Calculate Percentage".

#6

You have not answered my question, where are you setting the topic for the percentage message?

#7

the AND gate connected between function node named (Calculate Percentage) and inject node named (less) , why shall I set the topic of the sensor node?? please explain to me

#8

Do you mean in the AND gate must be a setting for the topic of Calculate Percentage?

#9

@Riven5 oops, your right, I don't know how missed the inject :thinking:

@mustafa - the node works by looking at the topic of the msg and matching that to the first part of the each AND condition. It would make a lot more sense if the author had named the first part of each condition "Topic'`

#10

Just in case you're still not clear on why: the names of the nodes are ignored. Only the topic matters. I said to set the topic in the sensor node because that is the origin, but you can set it in the function node if you prefer. msg.topic = "Calculate Percentage"

#11

Hi All
Please see below the flow
[{"id":"b35f6956.6db678","type":"rpi-srf","z":"dae97729.e9eed8","name":"Sensor","topic":"Calculate Percentage","pulse":"0.5","pins":"37,38","x":108,"y":155,"wires":[["616ac931.29b938"]]},{"id":"39f04960.f55496","type":"debug","z":"dae97729.e9eed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":628,"y":252,"wires":[]},{"id":"cd2a360c.cbed68","type":"ui_gauge","z":"dae97729.e9eed8","name":"","group":"2cad78f1.953508","order":0,"width":"6","height":"7","gtype":"gage","title":"Tank Filled","label":"Percentage","format":"{{value}}","min":0,"max":"100","colors":["#b52100","#e6e600","#42ca38"],"seg1":"","seg2":"","x":661,"y":120,"wires":[]},{"id":"f1ae908b.e85ce","type":"and-gate","z":"dae97729.e9eed8","name":"","rules":[{"t":"lt","v":"10","vt":"num","propertyType":"msg","property":"payload","topic":"Calculate Percentage"},{"t":"eq","v":"10","vt":"num","propertyType":"msg","property":"payload","topic":"less"}],"outputTopic":"","gateType":"and","emitOnlyIfTrue":false,"x":378,"y":322,"wires":[["e1487b82.6dfe98","39f04960.f55496"]]},{"id":"8c0273fa.30168","type":"inject","z":"dae97729.e9eed8","name":"","topic":"less","payload":"10","payloadType":"num","repeat":"0.5","crontab":"","once":false,"onceDelay":0.1,"x":121,"y":301,"wires":[["f1ae908b.e85ce"]]},{"id":"e1487b82.6dfe98","type":"rpi-gpio out","z":"dae97729.e9eed8","name":"","pin":"40","set":"","level":"0","freq":"","out":"out","x":563,"y":414,"wires":[]},{"id":"616ac931.29b938","type":"function","z":"dae97729.e9eed8","name":"Calculate Percentage","func":"var distance = parseInt(msg.payload);\nvar EmptyDist = 100 -distance;\nmsg.payload = parseInt((EmptyDist/100)*100);\n//msg.payload =(100- parseInt((temp/125)*100));\nreturn msg;\n","outputs":1,"noerr":0,"x":309,"y":155,"wires":[["cd2a360c.cbed68","f1ae908b.e85ce"]]},{"id":"2cad78f1.953508","type":"ui_group","z":"","name":"Mustafa","tab":"9f2b0241.7a073","disp":true,"width":"6","collapse":false},{"id":"9f2b0241.7a073","type":"ui_tab","z":"","name":"Home","icon":"my dash","order":2}]

I set the msg.topic = "Calculate Percentage" in the sensor node and fit the distance arround 6 percent and connect the debug on the AND gate output then i got the debug as below :
0/23/2018, 2:54:19 PMnode: e1487b82.6dfe98
msg : string[17]
"Invalid input: 10"
10/23/2018, 2:54:19 PMnode: 39f04960.f55496
msg.payload : number
10
10/23/2018, 2:54:19 PMnode: e1487b82.6dfe98
msg : string[17]
"Invalid input: 10"
10/23/2018, 2:54:19 PMnode: 39f04960.f55496
msg.payload : number
10
10/23/2018, 2:54:19 PMnode: e1487b82.6dfe98
msg : string[16]
"Invalid input: 6"
10/23/2018, 2:54:19 PMnode: 39f04960.f55496
msg.payload : number
6
10/23/2018, 2:54:19 PMnode: 979e0d81.a137
msg.payload : number
4
10/23/2018, 2:54:19 PMnode: 979e0d81.a137
msg.payload : number
6
10/23/2018, 2:54:23 PMnode: e1487b82.6dfe98
msg : string[17]
"Invalid input: 10"
10/23/2018, 2:54:23 PMnode: 39f04960.f55496
msg.payload : number
10
10/23/2018, 2:54:23 PMnode: e1487b82.6dfe98
msg : string[17]
"Invalid input: 10"
10/23/2018, 2:54:23 PMnode: 39f04960.f55496
msg.payload : number
10
10/23/2018, 2:54:23 PMnode: e1487b82.6dfe98
msg : string[16]
"Invalid input: 6"
10/23/2018, 2:54:23 PMnode: 39f04960.f55496
msg.payload : number
6
10/23/2018, 2:54:23 PMnode: 979e0d81.a137
msg.payload : number
7

it seems that the debug output is reading periodically the output of the inject node which is 10 and the read after that the actual distance which is 7 percent and no output from the GPIO.
any idea please ??

#12

First have a look at the info tab for the and gate. Particularly notice what it says about what is in msg.payload of the output message and what is in msg.bool of the output message.
Also put a debug on the output of the function node to check exactly what is in that message that would explain the "Invalid Input: 10" messages. If it looks ok then post that here.

#13

The output of the function node is ( 10/23/2018, 3:44:22 PMnode: 979e0d81.a137
msg.payload : number
-136
10/23/2018, 3:44:23 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-135
10/23/2018, 3:44:23 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
49
10/23/2018, 3:44:24 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-135
10/23/2018, 3:44:25 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:25 PMnode: 979e0d81.a137
msg.payload : number
80
10/23/2018, 3:44:25 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
78
10/23/2018, 3:44:26 PMnode: 979e0d81.a137
msg.payload : number
85
10/23/2018, 3:44:26 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
87
10/23/2018, 3:44:26 PMnode: 979e0d81.a137
msg.payload : number
86
10/23/2018, 3:44:26 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
86
10/23/2018, 3:44:27 PMnode: 979e0d81.a137
msg.payload : number
87
10/23/2018, 3:44:27 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
87
10/23/2018, 3:44:28 PMnode: 979e0d81.a137
msg.payload : number
88
10/23/2018, 3:44:28 PMnode: 979e0d81.a137
msg.payload : number
90
10/23/2018, 3:44:28 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
90
10/23/2018, 3:44:29 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:29 PMnode: 979e0d81.a137
msg.payload : number
89
10/23/2018, 3:44:29 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
89
10/23/2018, 3:44:30 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:30 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
91
10/23/2018, 3:44:30 PMnode: 979e0d81.a137
msg.payload : number
92
10/23/2018, 3:44:31 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:32 PMnode: 979e0d81.a137
msg.payload : number
92
10/23/2018, 3:44:32 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
90
10/23/2018, 3:44:32 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:33 PMnode: 979e0d81.a137
msg.payload : number
92
10/23/2018, 3:44:33 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
91
10/23/2018, 3:44:33 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:34 PMnode: 979e0d81.a137
msg.payload : number
92
10/23/2018, 3:44:34 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
90
10/23/2018, 3:44:34 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
92
10/23/2018, 3:44:36 PMnode: 979e0d81.a137
msg.payload : number
91
10/23/2018, 3:44:36 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
88
10/23/2018, 3:44:36 PMnode: 979e0d81.a137
msg.payload : number
-136
10/23/2018, 3:44:36 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-137
10/23/2018, 3:44:37 PMnode: 979e0d81.a137
msg.payload : number
45
10/23/2018, 3:44:37 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-27
10/23/2018, 3:44:37 PMnode: 979e0d81.a137
msg.payload : number
47
10/23/2018, 3:44:37 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-137
10/23/2018, 3:44:38 PMnode: 979e0d81.a137
msg.payload : number
-136
10/23/2018, 3:44:38 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-38
10/23/2018, 3:44:38 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:39 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-137
10/23/2018, 3:44:39 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:40 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-138
10/23/2018, 3:44:40 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:42 PMnode: 979e0d81.a137
msg.payload : number
-137
10/23/2018, 3:44:42 PMnode: 979e0d81.a137
msg.payload : number
-135
10/23/2018, 3:44:42 PMnode: 979e0d81.a137
msg.payload : number
-136
10/23/2018, 3:44:42 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-110
10/23/2018, 3:44:43 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-135
10/23/2018, 3:44:43 PMnode: 979e0d81.a137
msg.payload : number
-135
10/23/2018, 3:44:43 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-131
10/23/2018, 3:44:44 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:44 PMnode: 979e0d81.a137
msg.payload : number
-136
10/23/2018, 3:44:44 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-109
10/23/2018, 3:44:45 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:46 PMnode: 979e0d81.a137
msg.payload : number
39
10/23/2018, 3:44:46 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
18
10/23/2018, 3:44:46 PMnode: 979e0d81.a137
msg.payload : number
-137
10/23/2018, 3:44:47 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-21
10/23/2018, 3:44:47 PMnode: 979e0d81.a137
msg.payload : number
-135
10/23/2018, 3:44:47 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-136
10/23/2018, 3:44:47 PMnode: 39f04960.f55496
Calculate Percentage : msg.payload : number
-135)

and regarding to the AND gate info tab is says: ( payload string | object

the payload of the message that trigged the node

bool /bool

the result of the node)

what suppose the output of the AND gate must be ?

#14

We don't need to see dozens of results, just the output of the function node when you get the "illegal value" message from the AND gate. Disable the AND gate debug while you are looking for that so the output is easier to read.

If you want to send the result of the AND operation to the GPIO pin then you must use the value of msg.bool as it says in the info. I would not have designed the gate like that, I would have put the boolean output in the payload which is what you have assumed will happen. However, as it says in the Info panel, it returns the result of the AND in msg.bool. The easiest thing is to put a Change node after the AND node. Configure it to Move msg.bool To msg.payload.

#15


is that correct for change node?
still the GPIO not work while the on the flow the AND gate show the true status at level below 10 and false status at level above 10 percent

#16

Configure it to Move msg.bool To msg.payload.

To achieve the above in the Change node you:
Set msg.payload
to msg.bool

or

Move msg.bool
to msg.payload

#17

It is works now but the LED get ON when the distance less 10 and keep ON even after distance more than 10 when AND gate in false status
any idea ?

#18

Do you mean the AND gate changes state as it should, but the LED is not following the AND gate status display?
If so first put a debug showing what is going to the GPIO pin and see if that is correct.

#19

Colin
Exactlly as you says
here is the debug
10/31/2018, 12:19:55 AMnode: 979e0d81.a137msg : Object

object

topic: null

payload: true

bool: true

_msgid: "7d766744.27a018"

I was selecting the option of True restriction in the AND gate so the it will gives output once the condition is true if not selected the LED always ON

#20

With that option selected it will send true when it becomes true, but will never send false. So how do you expect the led to change? Clear that option so the AND node should send true or false as the inputs change so the led will change state.