# Logical And Function

**URL:** https://discourse.nodered.org/t/logical-and-function/70093
**Category:** General
**Created:** [4 November 2022 17:26 UTC](https://discourse.nodered.org/t/logical-and-function/70093 "2022-11-04T17:26:20Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [16 November 2022 22:41 UTC](https://discourse.nodered.org/t/logical-and-function/70093/21 "2022-11-16T22:41:27Z")

</div>

> [@HansZnarf](#):
>
> 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.

There is not a simple AND gate node perhaps because in flow based programming values from an earlier message do not hang around until you want to use them. They are not variables.

There is no Java involved. While nodes may be written in Javascript (a very different language) you do not need to know Javascript to use Node-red, any more than you need to understand C to use the Bash shell.

I will try and explain my suggested flow step by step.

We start with a msg which contains `msg.payload = { "A": true, "B": false}`  
**One** message has **both** of the elements that we want to AND.

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd51b7be697e3e9713525eed4046fbbe7e4b8a1e.jpeg)

The message arrives at the first switch node. Is msg.payload.A equal to true? Yes it is so the message leaves the node via the first wire.

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/8889130df8f6415de3abf814384ffcd03abc424f.jpeg)

It arrives at the second switch node. Is msg.payload.B equal to true? No, it is not, so the message leaves that node via the second wire

 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7280b57c257b8e21ddbf6bafb1dde6e00107eb9.jpeg)  
It next arrives at a change node which sets msg.payload to false.

Finally at the exit of this snippet of a flow msg.payload = false.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [17 November 2022 05:48 UTC](https://discourse.nodered.org/t/logical-and-function/70093/22 "2022-11-17T05:48:13Z")

</div>

And you have been told many many times that there is a node that does what you want - the Ultimate boolean node - however you seem to be continually ignoring this and saying how amazed you are that there is not a simple node

You have two choices if you want to manage multiple incoming messages and do something with them - either use the Join node to turn them into a single message with an identifying property for each message or use something like the Ultimate boolean node and send your messages to it with different topics as part of the message

Craig

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [17 November 2022 18:00 UTC](https://discourse.nodered.org/t/logical-and-function/70093/23 "2022-11-17T18:00:21Z")

</div>

Colin:

Thank you for the reply. I do not know/understand how to separate the two incoming message so a Logical And can be created. Have looked at Node-red videos. None that I have found so far explain to me how this is done. Happy to learn but am a newbe. Node-red does a great job establishing communication. That was easy. Having more trouble figuring out how data is handled. It does not work like other system that I have used.

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [17 November 2022 18:03 UTC](https://discourse.nodered.org/t/logical-and-function/70093/24 "2022-11-17T18:03:36Z")

</div>

When it comes to Node-red / Java question I misunderstood the nature of the scripting in the programming blocks. Can you aim me at training on how these blocks function?

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [17 November 2022 18:09 UTC](https://discourse.nodered.org/t/logical-and-function/70093/25 "2022-11-17T18:09:14Z")

</div>

Craigcurtin: Thanks for your reply. Regret if I am being annoying. That is not the intention. There is a big gap in my understanding of Node-red. My background is in building / lighting controls and electrical power monitoring. Devices / Software in these environments have logical block for the functions I need. I apologize for my ignorance on how these are handled in Node-red. No offence was intended.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [17 November 2022 18:19 UTC](https://discourse.nodered.org/t/logical-and-function/70093/26 "2022-11-17T18:19:41Z")

</div>

- Node-RED uses Java **script** not Java.
- The mentioned boolean-logic-ultimate does what you need.

The concept that you need to keep in mind is that an incoming message (ie. payload) has a lifecycle, it lives on its own, it is not aware of other messages, nor are the nodes it passes through - **unless** they have been created/programmed to handle those requirements.

The join node is one of them, the boolean logic ultimate node is another. Another way is to use "context" to store messages, so that you can compare/combine etc.

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [17 November 2022 20:08 UTC](https://discourse.nodered.org/t/logical-and-function/70093/27 "2022-11-17T20:08:57Z")

</div>

bakman2: Thanks for the information. My background is decades as a field technician with building / light control as well as BASIC going back to TI 99/4A, Coleco Adam and Timex Sinclair. Newer programming environments are causing me struggle. I am a child in these modernish ways, so baby steps. There are many newer concepts that are foreign.

Node-red does such a good job with reading data from devices. That was easy to figure out. The rest will take time. Would hope we could keep a friendly dialog for newbes like me.

Sincerely,

Hans

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [17 November 2022 22:47 UTC](https://discourse.nodered.org/t/logical-and-function/70093/28 "2022-11-17T22:47:28Z")

</div>

> [@HansZnarf](#):
>
> Newer programming environments are causing me struggle.

No need to apologize. Node-RED is not newer, just different. Those of us familiar with programmable logic tend to think in terms of states. The inputs to a logic block represent a state, and the output(s) define a resultant state. This is not how [flow-based programming](https://nodered.org/about/) works, and there is bound to be a period of adjustment. By the way, trying to reproduce the patterns of one approach in the other is generally not a good idea. It's better to develop a style that is natural to the environment.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [17 November 2022 23:56 UTC](https://discourse.nodered.org/t/logical-and-function/70093/29 "2022-11-17T23:56:41Z")

</div>

> [@HansZnarf](#):
>
> [I] Want to learn Node-Red since it is a "advertised" as a Low/No code alternative. Java[script] is counter-intuitive to me.
> 
> I misunderstood the nature of the scripting in the programming blocks. Can you aim me at training on how these blocks function?

Which do you really want - a script (code based) solution or a low/no-code solution?

People have offered you multiple no-code solutions but you give no indication of having tested them.  
Instead you keep hinting that you want a script based solution.

Ok then here is a function node which you can pass two messages sequentially into and if both are true it will return true.

```auto
[
    {
        "id": "1209d5c4281ce57c",
        "type": "function",
        "z": "f3e02add17e4b873",
        "name": "function 18",
        "func": "let firstvalue = context.get('firstvalue') || \"nuthin\";\nlet secondvalue = msg.payload\nif (firstvalue == \"nuthin\") {\n context.set('firstvalue',(secondvalue === true)? \"TRUE\":\"FALSE\")\n}\nelse {\n if (secondvalue === true && firstvalue === \"TRUE\") {\n msg.payload = true\n }\n else {\n msg.payload = false\n }\n context.set('firstvalue', 'nuthin')\n return msg\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 160,
        "wires": [
            [
                "c5eebefb6dabaff1"
            ]
        ]
    }
]

```

As @drmibell has said, when you adopt a new language you are much wiser to work with the language's strengths than trying to force it to work the way you expect.  
Therefore I strongly urge you not to use this function. It is horrible, horrible code for Node-red.  
Instead try each of the low/no-code suggestions given by the people who are trying to help you. Let us know how you get on with them?

---

<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: [18 November 2022 16:14 UTC](https://discourse.nodered.org/t/logical-and-function/70093/30 "2022-11-18T16:14:48Z")

</div>

> [@HansZnarf](#):
>
> I do not know/understand how to separate the two incoming message so a Logical And can be created.

You said at the start:

> [@HansZnarf](#):
>
> 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.

I presume you mean two nodes rather than blocks. If so then then they are already separate so there is no need to separate them. Just add a change node to the output of each node and in there set the topics to different values "A" and "B" for example. Then feed them into the boolean node.

Edit: unless they already have different topics, in which case you don't need the change nodes.

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 15:21 UTC](https://discourse.nodered.org/t/logical-and-function/70093/31 "2022-11-28T15:21:50Z")

</div>

[Hans\_Bool\_Example.json](https://discourse.nodered.org/uploads/short-url/mFLgpoY4E7QTva9TerlX9ubB0hy.json) (12.7 KB)

Regret I called Nodes, Blocks. In many PLC programs Node like objects have been called Blocks. Did find Bool Nodes. It works as expected when true / false used in simulated Inject flow but not in live data flow. Program is attached.

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 15:26 UTC](https://discourse.nodered.org/t/logical-and-function/70093/32 "2022-11-28T15:26:35Z")

</div>

Would rather use Nodes. Showing structured text as examples. Did find Bool Nodes. It works as expected when true / false used in simulated Inject flow but not in live data flow. Program is attached.  
Hope someone can find what I am missing.

[Hans\_Bool\_Example.json](https://discourse.nodered.org/uploads/short-url/mFLgpoY4E7QTva9TerlX9ubB0hy.json) (12.7 KB)

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 15:28 UTC](https://discourse.nodered.org/t/logical-and-function/70093/33 "2022-11-28T15:28:12Z")

</div>

Regret any frustration my posts cause. Did find Bool Nodes. It works as expected when true / false used in simulated Inject flow but not in live data flow. Program is attached.  
Hope someone can find what I am missing.

[Hans\_Bool\_Example.json](https://discourse.nodered.org/uploads/short-url/mFLgpoY4E7QTva9TerlX9ubB0hy.json) (12.7 KB)

---

<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: [28 November 2022 15:52 UTC](https://discourse.nodered.org/t/logical-and-function/70093/34 "2022-11-28T15:52:38Z")

</div>

> [@HansZnarf](#):
>
> Regret I called Nodes, Blocks

The terminology used in node-red of "nodes" and "wires" is quite common in flow-based programming. They also relate directly to "vertices" and "edges" in [Graph theory](https://en.wikipedia.org/wiki/Graph_theory#:~:text=In%20mathematics%2C%20graph%20theory%20is,also%20called%20links%20or%20lines).

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [28 November 2022 16:02 UTC](https://discourse.nodered.org/t/logical-and-function/70093/35 "2022-11-28T16:02:41Z")

</div>

In the switch node you are evaluating/comparing "text" to numeric values. Change it to numeric.  
 ![SCR-20221128-nmn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5dbd0bd66153554443a0ff2c2b183fb39ae82dce.png)

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 16:50 UTC](https://discourse.nodered.org/t/logical-and-function/70093/36 "2022-11-28T16:50:33Z")

</div>

Made simple flow for clarity. Thanks.

[Hans\_Bool\_Example.json](https://discourse.nodered.org/uploads/short-url/mFLgpoY4E7QTva9TerlX9ubB0hy.json) (12.7 KB)

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 16:54 UTC](https://discourse.nodered.org/t/logical-and-function/70093/37 "2022-11-28T16:54:22Z")

</div>

Many thanks for the reply. Regret that I am not seeing this. Switch output is working. This goes to filter and both examples are putting out true or false results before going to And block.

[Hans\_Bool\_Example.json](https://discourse.nodered.org/uploads/short-url/mFLgpoY4E7QTva9TerlX9ubB0hy.json) (12.7 KB)

Hans

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [28 November 2022 17:04 UTC](https://discourse.nodered.org/t/logical-and-function/70093/38 "2022-11-28T17:04:31Z")

</div>

Just to avoid confusion and unnecessary downloads, can you confirm that the 5 copies you have posted of Hans\_Bool\_Example.json are all the same file?

> Switch output is working. This goes to filter and both examples are putting out true or false results before going to And block.

Can you post actual examples of the data coming out of your two filter nodes in the flow which doesn't work, together with the output of the boolean logic node?

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 17:16 UTC](https://discourse.nodered.org/t/logical-and-function/70093/39 "2022-11-28T17:16:14Z")

</div>

jbudd:

Thanks. Did not know that. Believed Reply was like an email. Never would do anything on purpose to make others uncomfortable.

---

<div class="post-metadata">

### Author: ![HansZnarf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hansznarf/32/70215_2.png) [@HansZnarf](https://discourse.nodered.org/u/HansZnarf)
#### Post date: [28 November 2022 19:18 UTC](https://discourse.nodered.org/t/logical-and-function/70093/40 "2022-11-28T19:18:39Z")

</div>

jbudd:

Many thanks. The true / false look the same from both filters. See attached.

 ![NodeMetering](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/490ca7f821494c1f04e2749a20f1c912a834602e.jpeg)

I cannot see a difference?

Hans

[Previous page](https://discourse.nodered.org/t/logical-and-function/70093.md?page=1)

[Next page](https://discourse.nodered.org/t/logical-and-function/70093.md?page=3)
