Logical And Function

Have two block that create True and False Output. Want a True output when Both are True and a False output when one or the other is false. This is typically a simple AND command in most programming. Have be tripping over this for hours. In a PLC it would be two contact in a roll that both need to be closed before the output coil would go true. If is a common function. We have two alarm conditions that need to both be true before there is an alarm. Please help.
flowsMeter_11_04_2022.json (80.5 KB)

Can you post the Node-RED flow you have created so far (so people can offer targeted help for you) ??

You can do this logic with a JSONata expression in a switch node, you will need to join the messages as a key value object.
e.g.

[{"id":"053e730f8ed7b7b5","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":1200,"wires":[["46973854391ebdf0"]]},{"id":"46973854391ebdf0","type":"change","z":"452103ea51141731","name":"Set topic for device 1","rules":[{"t":"set","p":"topic","pt":"msg","to":"device1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1220,"wires":[["75b2152ac2a306b4"]]},{"id":"6b145b190947b96c","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":1240,"wires":[["46973854391ebdf0"]]},{"id":"75b2152ac2a306b4","type":"join","z":"452103ea51141731","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":1260,"wires":[["a7e76851d61b6f41"]]},{"id":"c9cc99dd60e48a81","type":"change","z":"452103ea51141731","name":"Set topic for device 2","rules":[{"t":"set","p":"topic","pt":"msg","to":"device2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1320,"wires":[["75b2152ac2a306b4"]]},{"id":"a7e76851d61b6f41","type":"switch","z":"452103ea51141731","name":"Any false","property":"false in  $$.payload.*","propertyType":"jsonata","rules":[{"t":"false"},{"t":"true"}],"checkall":"false","repair":false,"outputs":2,"x":560,"y":1260,"wires":[["48d78d985236f5cb"],["3986f0b8b2045558"]]},{"id":"b34238dac0a07c93","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":1300,"wires":[["c9cc99dd60e48a81"]]},{"id":"523b705de16f819d","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":1340,"wires":[["c9cc99dd60e48a81"]]},{"id":"48d78d985236f5cb","type":"debug","z":"452103ea51141731","name":"All true","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":1220,"wires":[]},{"id":"3986f0b8b2045558","type":"debug","z":"452103ea51141731","name":"contains false","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":1280,"wires":[]}]

Expression

false in  $$.payload.*

Or there is the boolean ultimate node

You can also do it in a change node
e.g.

[{"id":"053e730f8ed7b7b5","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":1200,"wires":[["46973854391ebdf0"]]},{"id":"46973854391ebdf0","type":"change","z":"452103ea51141731","name":"Set topic for device 1","rules":[{"t":"set","p":"topic","pt":"msg","to":"device1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1220,"wires":[["75b2152ac2a306b4"]]},{"id":"6b145b190947b96c","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":1240,"wires":[["46973854391ebdf0"]]},{"id":"75b2152ac2a306b4","type":"join","z":"452103ea51141731","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":1260,"wires":[["e0cf41a26d66df1f"]]},{"id":"c9cc99dd60e48a81","type":"change","z":"452103ea51141731","name":"Set topic for device 2","rules":[{"t":"set","p":"topic","pt":"msg","to":"device2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1320,"wires":[["75b2152ac2a306b4"]]},{"id":"e0cf41a26d66df1f","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$not(false in $$.payload.*)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":1260,"wires":[["61e6181955e13b38"]]},{"id":"b34238dac0a07c93","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":1300,"wires":[["c9cc99dd60e48a81"]]},{"id":"523b705de16f819d","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":1340,"wires":[["c9cc99dd60e48a81"]]},{"id":"61e6181955e13b38","type":"debug","z":"452103ea51141731","name":"debug 104","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":1260,"wires":[]}]

Expression

$not(false in $$.payload.*)

Are the values coming in together in one message or are they coming in in separate messages? If in separate messages have a look at node-red-contrib-boolean-ultimate. Or something similar to that name at least.

flows.json (25.9 KB)

Hope that this helps. In PLC logic it would look something like this

-----------{Value 1}-----------{Value 2}-----------(Output)------------

Flow attached in earlier reply.

Many thanks. Tried different ways to use those blocks. Reason I want to use Node-Red is that it is like many PLC programs I have used. However json is a bit odd to me when it comes to what I have experienced with while doing simple machine logic.

Don't know if this is of use but here is a flow that performs logical AND, OR, NAND, NOR, XOR and XNOR on (boolean) msg.payload.1 and msg.payload.2.


[{"id":"9372feccca70ce91","type":"tab","label":"Logic Gates","disabled":false,"info":"","env":[]},{"id":"fb1fb2f6e94391af","type":"group","z":"9372feccca70ce91","name":"XOR / XNOR","style":{"label":true},"nodes":["ba919fc6ffb202b2","76fa27260cf66be7","8200d44a23687907","7614143f8e6d6284","07fadef42294b894","c53457f7e9831735"],"x":94,"y":719,"w":512,"h":182},{"id":"5ba4b3220ea1be55","type":"junction","z":"9372feccca70ce91","x":260,"y":80,"wires":[["075923e2b0610a53","1d19ebdbeae23232"]]},{"id":"650686c876d5bded","type":"junction","z":"9372feccca70ce91","x":700,"y":300,"wires":[["9f39315ff7fb8da4"]]},{"id":"dff5231da6615362","type":"inject","z":"9372feccca70ce91","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":true, \"2\":true}","payloadType":"json","x":120,"y":20,"wires":[["5ba4b3220ea1be55"]]},{"id":"5e9e00a6f40dd412","type":"inject","z":"9372feccca70ce91","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":true, \"2\":false}","payloadType":"json","x":130,"y":60,"wires":[["5ba4b3220ea1be55"]]},{"id":"a97345035317f0b4","type":"inject","z":"9372feccca70ce91","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":false, \"2\":true}","payloadType":"json","x":130,"y":100,"wires":[["5ba4b3220ea1be55"]]},{"id":"6c58fd6a8ca15105","type":"inject","z":"9372feccca70ce91","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":false, \"2\":false}","payloadType":"json","x":130,"y":140,"wires":[["5ba4b3220ea1be55"]]},{"id":"1d19ebdbeae23232","type":"switch","z":"9372feccca70ce91","name":"1 true?","property":"payload.1","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":300,"wires":[["c42c81415b813df4"],["ae8e66805180fa38"]]},{"id":"c42c81415b813df4","type":"switch","z":"9372feccca70ce91","name":"2 true?","property":"payload.2","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":280,"y":280,"wires":[["8bb8730f87c6f28d","bda32bbdc5df094d","cfa1755f18e2fb84"],["e8e9095ad0f5f89e","bda32bbdc5df094d","63166de2560da93b"]]},{"id":"ae8e66805180fa38","type":"switch","z":"9372feccca70ce91","name":"2 true?","property":"payload.2","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":280,"y":320,"wires":[["e8e9095ad0f5f89e","bda32bbdc5df094d","63166de2560da93b"],["e8e9095ad0f5f89e","c714e1fec94dccd5","cfa1755f18e2fb84"]]},{"id":"8bb8730f87c6f28d","type":"change","z":"9372feccca70ce91","name":"AND true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"AND","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":140,"wires":[["7a9624c0fd0d1df1","650686c876d5bded"]]},{"id":"e8e9095ad0f5f89e","type":"change","z":"9372feccca70ce91","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"AND","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":180,"wires":[["7a9624c0fd0d1df1","650686c876d5bded"]]},{"id":"7a9624c0fd0d1df1","type":"change","z":"9372feccca70ce91","name":"NAND","rules":[{"t":"set","p":"topic","pt":"msg","to":"NAND","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"temporary","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"temporary","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":140,"wires":[["650686c876d5bded"]]},{"id":"9f39315ff7fb8da4","type":"join","z":"9372feccca70ce91","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"6","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":770,"y":300,"wires":[["05a06145966345b4"]]},{"id":"6a478126074489ad","type":"debug","z":"9372feccca70ce91","name":"OUTPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":140,"wires":[]},{"id":"05a06145966345b4","type":"change","z":"9372feccca70ce91","name":"del topic","rules":[{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":300,"wires":[["6a478126074489ad"]]},{"id":"075923e2b0610a53","type":"debug","z":"9372feccca70ce91","name":"INPUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":80,"wires":[]},{"id":"bda32bbdc5df094d","type":"change","z":"9372feccca70ce91","name":"OR true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"OR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":260,"wires":[["b3aa6f3506fd1e7b","650686c876d5bded"]]},{"id":"c714e1fec94dccd5","type":"change","z":"9372feccca70ce91","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"OR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":340,"wires":[["b3aa6f3506fd1e7b","650686c876d5bded"]]},{"id":"b3aa6f3506fd1e7b","type":"change","z":"9372feccca70ce91","name":"NOR","rules":[{"t":"set","p":"topic","pt":"msg","to":"NOR","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"temporary","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"temporary","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":300,"wires":[["650686c876d5bded"]]},{"id":"63166de2560da93b","type":"change","z":"9372feccca70ce91","name":"XOR true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"XOR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":420,"wires":[["ad3202d218e93d29","650686c876d5bded"]]},{"id":"cfa1755f18e2fb84","type":"change","z":"9372feccca70ce91","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"XOR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":460,"wires":[["ad3202d218e93d29","650686c876d5bded"]]},{"id":"ad3202d218e93d29","type":"change","z":"9372feccca70ce91","name":"XNOR","rules":[{"t":"set","p":"topic","pt":"msg","to":"XNOR","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"temporary","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"temporary","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":460,"wires":[["650686c876d5bded"]]},{"id":"ba919fc6ffb202b2","type":"switch","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"1 true?","property":"payload.1","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":180,"y":780,"wires":[["76fa27260cf66be7"],["8200d44a23687907"]]},{"id":"76fa27260cf66be7","type":"switch","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"2 true?","property":"payload.2","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":760,"wires":[["07fadef42294b894"],["7614143f8e6d6284"]]},{"id":"8200d44a23687907","type":"switch","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"2 true?","property":"payload.2","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":800,"wires":[["7614143f8e6d6284"],["07fadef42294b894"]]},{"id":"7614143f8e6d6284","type":"change","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"XOR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":760,"wires":[["c53457f7e9831735"]]},{"id":"07fadef42294b894","type":"change","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"XOR","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":800,"wires":[["c53457f7e9831735"]]},{"id":"c53457f7e9831735","type":"change","z":"9372feccca70ce91","g":"fb1fb2f6e94391af","name":"swap true/false","rules":[{"t":"set","p":"topic","pt":"msg","to":"XNOR","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"temporary","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"temporary","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":860,"wires":[[]]}]

Raspi_Gpio_Example

As @colin and @E1cid says - the easiest way to do Boolean logic is to use

The author is very responsive, has output compatible with Home Assistant if you use that - and he even has a Youtube channel that takes you throught the nodes

Craig

ii0t2k: How do I link the contacts to existing "true" "false" outputs from other blocks

Thank you. Would expect there to be a simple "And" block. In some code it would like something like "If A = true And B = true Then C= true; Else C = false"

As well as an OR node, XOR, NOR .....
One node that is configurable to do simple and more complex logic is more useful.


I Made up the Bool And Block in the image. However this is a common logic gate used in programming


Want to learn Node-Red since it is a "advertised" as a Low/No code alternative. Java is counter intuitive to me.

See Image. I "made up" the Bool And block, however many automation programs use similar logic for a simple And command.

Script could look like If "A" is True and "B" is True then "C" is True; Else "C" is False.

You are not understanding i believe.

Node-red nodes only have one input. To differentiate between inputs in a join node or a boolean ultimate node, you have to give the individual message wires topic's as in my examples. both my example(bottom one) and the boolean ultimate node will do what you wish you just need to give each wire input a topic. Post you exported flow and I will add the logic for you, then maybe you will have a eureka moment.

E1cid:

Many thanks for the kind reply. Still learning Node-red and am stuck in the past of how "things have been done."

Notice that Node-red only have one input. Is there a tutorial on how to make that separation? Do not want someone to do that for me or I will not learn. Need a way to understand how to split the one input to except both bools separately.

To me Java is very non-intuitive.

Thanks

Hans

Untitled 1

I think you are trying to code the block you have drawn as "Bool and".

Your first difficulty is that most Node-red nodes can only process one message at a time so that you have to join your messages into a single one. The usual way to achieve that is with a Join node (I won't give the details, you have to read the documentation).

Then it's up to you how you implement the logic gate. Here is a no-code version using 2 switch nodes and two change nodes but you could do it in javascript or whatever.

jbudd: Thanks for your comment. Yes we are trying to "AND" two BOOLs so that when both are TRUE the output is TRUE otherwise the output will be FALSE. Appreciate you shows the diagram. Being newish to Node-red and with zero JAVA background I am having difficulty understanding your solution.

Logic ANDs are so common in the Industrial world that I am surprised there is not a simple AND gate block.

Sincerely,

Hans

1 Like

Fortunately that is no hindrance to you as node-red is not java based. It is javascript which is nothing to do with java.

If there were AND, OR, XOR, NOR etc then there would be proliferation of nodes. The ultimate boolean node referenced earlier will do it for you. Have you looked at that?

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.