# Logic gate doesn't seem to behave logically

**URL:** https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531
**Category:** General
**Created:** [1 July 2023 22:03 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531 "2023-07-01T22:03:25Z")
**Posts on this page:** 20
**Page:** 3

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [5 July 2023 11:26 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/46 "2023-07-05T11:26:45Z")

</div>

you missed the conversation i believe.

there was an inadvertant setting mistake in `switch` node, which was noticed, and since 'string' value _happen_ to evaluate as expected, the error was not noted.

> [@smanjunath211](#):
>
> I noticed in the given flow that in switch node `Soc 50%` , the evaluationis done on _string_ and not number, how does that work?

---

<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: [5 July 2023 13:18 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/47 "2023-07-05T13:18:18Z")

</div>

So are the actual values coming in Strings or Numbers?

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [5 July 2023 14:56 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/48 "2023-07-05T14:56:22Z")

</div>

the OP says it is from a certain `Victron` Nodes and they are numeric, the switch node used however were wrongly set to 'string'. but the issue was not that actually

> **Summary**
>
> (it was due to not using different topics for inject nodes to maually simulate the conditon (was working with Victron nodes but not with inject nodes simulation since inject did not have different topics),

this wrong setting would also come into effect in right circumstances. (or wrong circumstances)

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [5 July 2023 15:40 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/49 "2023-07-05T15:40:15Z")

</div>

The data from the Victron nodes that I am using is Battery State of Charge between 0 and 100 (%) and Charge Current which could theoretically be between 100 Amps and -100 Amps.  
I had no idea what I was doing when I used that switch node so I didn't even notice it was set to "string".

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [7 July 2023 02:20 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/50 "2023-07-07T02:20:32Z")

</div>

Node-RED is MESSAGE based. Logic gates are STATE based. Messages are classified using the TOPIC of the message as a classifier. The gate is defined as 2 input.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c794ca0bedbd91cd1817b81121307ad4c60a9bb4.png)

The topics are different, therefore input ONE and TWO. The states are expressed in the payload defined as boolean type. The ultimate logic outputs , AND, OR, XOR are shown. The message state is 'remembered' as you insert them ONE and TWO. The Output state changes with the input messages.

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [7 July 2023 08:51 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/51 "2023-07-07T08:51:12Z")

</div>

Hi, thanks for the explanation but I am still having trouble understanding why I get unexpected outputs. I am currently inputting a "1" and a "0" into a logic gate and getting AND "true".

---

<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: [7 July 2023 09:15 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/52 "2023-07-07T09:15:14Z")

</div>

Add a debug node showing what is going into the gate node and show us what is there. Also show us how you now have the gate node configured.  
Set the debug node to output the complete message.

---

<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: [7 July 2023 10:37 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/53 "2023-07-07T10:37:56Z")

</div>

> [@usernamepasswordBS](#):
>
> I am currently inputting a "1" and a "0" into a logic gate and getting AND "true".

I thought I had read somewhere above that you have it working?

So that we can understand and perhaps explain what's going on, please post an export of a flow in which you can inject a 1 and a 0 but the AND output is "true".  
No Victron nodes though, use inject nodes to inject identical data to the Vicron output.

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [7 July 2023 10:44 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/54 "2023-07-07T10:44:52Z")

</div>

Does the `topic` of a `payload`, also flow into next node, if a `switch` node is used in between?

Edit: I just did a check in a test flow and it does. So please ignore the query,  
I was just thinking since the OP is taking data from Victron nodes, checking the condition through switch node to generate the 1 or 0 and then feeding the gate, there may be an issue with topic.

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [7 July 2023 10:56 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/55 "2023-07-07T10:56:19Z")

</div>

The below flow, gives me correctly. if i inject true/true I get true in AND operation as expected.

```auto
[{"id":"02c360c9c0900403","type":"inject","z":"370cbf905893daf4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"true","payloadType":"bool","x":240,"y":1800,"wires":[["bc23e3c2bc383775"]]},{"id":"bc23e3c2bc383775","type":"switch","z":"370cbf905893daf4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"30","vt":"num"},{"t":"gte","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1830,"wires":[["6aaef2607b6df86a","f2ae016f782a3c2b"],["6aaef2607b6df86a","f2ae016f782a3c2b"]]},{"id":"a32d1b90fd97e444","type":"inject","z":"370cbf905893daf4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"false","payloadType":"bool","x":240,"y":2040,"wires":[["e27e9ff8e86e9007"]]},{"id":"6aaef2607b6df86a","type":"BooleanLogicUltimate","z":"370cbf905893daf4","name":"","payloadPropName":"payload","filtertrue":"both","persist":true,"sInitializeWith":"WaitForPayload","triggertopic":"trigger","outputtriggeredby":"all","inputCount":2,"topic":"result","restrictinputevaluation":false,"delayEvaluation":0,"x":620,"y":1920,"wires":[["c73f201bac554932"],[],[]]},{"id":"f2eb784ce7a91e88","type":"inject","z":"370cbf905893daf4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"false","payloadType":"bool","x":240,"y":1860,"wires":[["bc23e3c2bc383775"]]},{"id":"0c308dd276ff04f2","type":"inject","z":"370cbf905893daf4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"true","payloadType":"bool","x":230,"y":1980,"wires":[["e27e9ff8e86e9007"]]},{"id":"e27e9ff8e86e9007","type":"switch","z":"370cbf905893daf4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"30","vt":"num"},{"t":"gte","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":2010,"wires":[["6aaef2607b6df86a","e6c09bf2c7622f1a"],["6aaef2607b6df86a","e6c09bf2c7622f1a"]]},{"id":"c73f201bac554932","type":"debug","z":"370cbf905893daf4","name":"debug 412","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":800,"y":1890,"wires":[]},{"id":"f2ae016f782a3c2b","type":"debug","z":"370cbf905893daf4","name":"debug 413","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":640,"y":1800,"wires":[]},{"id":"e6c09bf2c7622f1a","type":"debug","z":"370cbf905893daf4","name":"debug 414","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":650,"y":2040,"wires":[]}]

```

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a3e439bfd90b57f2c0bcbbb34479465e43656fe.png)

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [7 July 2023 19:41 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/56 "2023-07-07T19:41:01Z")

</div>

I think my issues may come from the node waiting for 2 inputs and some of the rules around that. Sometimes the gate says true when one of the inputs is false but it is perhaps waiting for a second new message before it changes state. Your example there is fine but you always input 2 messages so you always get a correct result but it seems these nodes need two inputs each time for a change of state, i.e. you cannot expect a change if you only send one new input.  
At least I think that is what's happening but I am happy to be corrected.

---

<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: [7 July 2023 20:26 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/57 "2023-07-07T20:26:27Z")

</div>

I believe that the node has a setting to change that.

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [7 July 2023 21:35 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/58 "2023-07-07T21:35:02Z")

</div>

I would love to know how. I've messed with the settings a fair amount and still not sure if I've really got it working the way it "should".

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [8 July 2023 00:48 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/59 "2023-07-08T00:48:57Z")

</div>

Boolean states are expressed like ON/OFF. For most programming like Javascript the values are expressed as True/False. True/False is used in Logic because Logic statements are expressed as a False Statement or a True Statement. False is evaluated to 0 and True is evaluated to 1. For bit logic gates, 1 is 5 volts and then 0 is 0 Volts or common. The gate in Node-RED will accept a 0 number and a 1 number bit values are not an actual 1 or 0, but states On/Off or also True/False. It is convenient to express states as 1 & 0. It then outputs the logical result as True/False. You can convert False = 0 and True = 1 through a Switch node.

A boolean logic example.

Lets say, This = True, That = False.  
This AND That is False, 0  
This AND NOT That is True, 1  
This OR That is True, 1

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [8 July 2023 00:52 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/60 "2023-07-08T00:52:55Z")

</div>

> [@usernamepasswordBS](#):
>
> if you only send one new input.

Only for the first time, subsequently every single input it evaluates, by comparing last two inputs.

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [8 July 2023 00:58 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/61 "2023-07-08T00:58:54Z")

</div>

in this logic gate the inputs are sent to the same input, The topic will separate them to input ONE or input TWO. This is so that you don't have to worry about the input, but change the name of the input message to say if it's input ONE or TWO.

Message Topic = ONE or TWO. Payload = State True/False.  
The example flow I posted shows you the payload 'state' and the Topic name. You can also send more topic names. Then also you can configure the logic gate node to how many inputs you'd like. If you have physical inputs you have to wire them. Here, you don't need to worry about it, just wire them all to the single input.

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [8 July 2023 08:18 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/62 "2023-07-08T08:18:14Z")

</div>

I understand all that you say about logic gates, that's not my issue at all. What I am puzzled by is that I have found (or at least I think I have) that sometimes the debug nodes are showing a "1" and a "0" on the node input yet the AND output is "true".  
I think I have got over this but I can't remember exactly how (!) so it seems to be working fine but when my flow restarts in a while I will watch it for that and take a screenshot.

---

<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: [8 July 2023 08:30 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/63 "2023-07-08T08:30:59Z")

</div>

> [@usernamepasswordBS](#):
>
> I would love to know how.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9ee3f5319d47754d0a40f11313ea60eff06d901b.png)

---

<div class="post-metadata">

### Author: ![usernamepasswordBS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usernamepasswordbs/32/81035_2.png) [@usernamepasswordBS](https://discourse.nodered.org/u/usernamepasswordBS)
#### Post date: [8 July 2023 08:40 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/64 "2023-07-08T08:40:10Z")

</div>

I have changed away from Ultimate boolean to another node but when I was trying with Ultimate I used the node in that mode.  
Here is a screenshot of what I am talking about:  
Look at debug 272 and 280

 ![Screenshot (38)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da8944d338184aa6a69aa2fb49961e859aa19859.png)

---

<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: [8 July 2023 08:47 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/65 "2023-07-08T08:47:27Z")

</div>

> [@usernamepasswordBS](#):
>
> Look at debug 272 and 280

That tells us nothing unless you do what I asked, which is to set the debug nodes to output the complete message and show us the debug sequence output in the debug pane.

[Previous page](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531.md?page=2)

[Next page](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531.md?page=4)
