# 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:** 2

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

</div>

Sorry about that. I have replaced the victron nodes with some values that should emulate the equipment I'm using.

---

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

</div>

I will have a look at that flow as my solution to make the AND gate is quite complicated.

---

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

</div>

You have not posted the modified flow

---

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

</div>

Yes I noticed it was wrong.

Here it is:

```auto
[{"id":"7e3990660f331109","type":"switch","z":"427b38e8dd718872","name":"SoC 50%","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"str"},{"t":"gte","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":321.99999237060547,"y":234.0000057220459,"wires":[["628a04013a442d2b"],["4e3e1791d494f834"]]},{"id":"628a04013a442d2b","type":"change","z":"427b38e8dd718872","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":586.9999923706055,"y":189.0000057220459,"wires":[["4995e8047d8fdd51"]]},{"id":"0b4746d6483380ac","type":"switch","z":"427b38e8dd718872","name":"Charge <10 or >20 Amps","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"10","vt":"str"},{"t":"gte","v":"20","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":338,"y":291.99999046325684,"wires":[["6ac5d9e8daaaeb3c"],["0067a70dd6c12ae3"]]},{"id":"4e3e1791d494f834","type":"change","z":"427b38e8dd718872","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":586.9999923706055,"y":249.0000057220459,"wires":[["4995e8047d8fdd51","19276f61c8cb70b3"]]},{"id":"6ac5d9e8daaaeb3c","type":"change","z":"427b38e8dd718872","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":586.9999923706055,"y":294.0000057220459,"wires":[["4995e8047d8fdd51"]]},{"id":"0067a70dd6c12ae3","type":"change","z":"427b38e8dd718872","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":586.9999923706055,"y":354.0000057220459,"wires":[["4995e8047d8fdd51","e28726885a314705"]]},{"id":"4995e8047d8fdd51","type":"BooleanLogicUltimate","z":"427b38e8dd718872","name":"","payloadPropName":"payload","filtertrue":"both","persist":true,"sInitializeWith":"false","triggertopic":"trigger","outputtriggeredby":"all","inputCount":2,"topic":"result","restrictinputevaluation":false,"delayEvaluation":0,"x":811.9999923706055,"y":264.0000057220459,"wires":[[],[],[]]},{"id":"f26cafa0351159bd","type":"inject","z":"427b38e8dd718872","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"49","payloadType":"num","x":103.39614486694336,"y":174.9846248626709,"wires":[["7e3990660f331109"]]},{"id":"9695df4cc79af59d","type":"inject","z":"427b38e8dd718872","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"51","payloadType":"num","x":100.39998626708984,"y":229.9807643890381,"wires":[["7e3990660f331109"]]},{"id":"e9433ddce9df6e42","type":"inject","z":"427b38e8dd718872","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9","payloadType":"num","x":102.40385437011719,"y":295.98077677001953,"wires":[["0b4746d6483380ac"]]},{"id":"c967e1b3125d8471","type":"inject","z":"427b38e8dd718872","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"num","x":97.39614868164062,"y":351.9769315551758,"wires":[["0b4746d6483380ac"]]},{"id":"e28726885a314705","type":"debug","z":"427b38e8dd718872","name":"debug 68","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":846.3999557495117,"y":416.4384651184082,"wires":[]},{"id":"19276f61c8cb70b3","type":"debug","z":"427b38e8dd718872","name":"debug 69","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850.4038696289062,"y":109.43846414794922,"wires":[]}]

```

---

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

</div>

Actually I can't get that to work. It works OK in my main flow with the Victron nodes but when I inject two values that should give a "true" or even link two inputs so they inject at the same time and I see both outputs from the Set nodes come out "true" yet the AND gate says "false".

---

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

</div>

> [@Colin](#):
>
> Have you made sure the inputs have different topics?

> [@Colin](#):
>
> However, you do not appear to be setting individual topics on the inputs so the node can distinguish which input is which.

> [@jbudd](#):
>
> you must be failing to set msg.topic to "input1" or "Input2" (or whatever) so that the node can tell which is which.

Three strong hints that your flow fails because there is no way for the boolean logic node to tell which message is which yet your latest flow still does not set different msg.topics.

I think that setting the node to initialize all inputs to false may be wrong too.

---

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

</div>

Thanks guys, putting different words in the topic makes it work as expected!  
That's not quite as simple as a logic gate. Probably should be called something else.

---

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

</div>

> [@usernamepasswordBS](#):
>
> That's not quite as simple as a logic gate.

I don't understand what you mean by that. It needs a way of distinguishing which input is which. Using the topic to identify the data source is the way that many, many nodes work in node red.

---

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

</div>

Also notice that I pointed out that you needed topics in my very first post. If you do not understand something in a post then you please ask, do not not just ignore it.

---

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

</div>

> [@usernamepasswordBS](#):
>
> That's not quite as simple as a logic gate.

A NR node only has one input point.  
Two successive messages could be from the same source rather than two different sources.  
Thus it needs to distinguish the source using msg.topic.

---

<div class="post-metadata">

### Author: ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)
#### Post date: [3 July 2023 11:22 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/36 "2023-07-03T11:22:16Z")

</div>

I had the same problems because NR only has one input.  
The solution was to count the number of incoming messages and  
link the messages with logical operations until all messages have arrived.  
The result of logical operations is then sent to the output.

![AND](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14dc635a006fb1522148197d811a83654b5a4d01.png)  
 ![XOR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e17971a1f7ac5b894a1fdc685a7ef9959964783b.png)

The contact node are inputs of Raspberry Pi GPIO for example.

---

<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: [3 July 2023 11:49 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/37 "2023-07-03T11:49:21Z")

</div>

Yes I made a mistake in assuming the node could differentiate between the two inputs. To be honest I'm not sure yet about the meaning and importance of topics nor am I ready for Javascript but hopefully with a bit of help I will get it eventually!  
The flow worked ok when I use the Victron nodes but they already have different topics I suppose. I didn't get the importance.

Is there actually a node with two separate inputs? Just out of interest.

---

<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: [3 July 2023 12:04 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/38 "2023-07-03T12:04:21Z")

</div>

I noticed in the given flow that in switch node `Soc 50%` , the evaluationis done on _string_ and not number, how does that work?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e100bfe98dc52de46ceee8a6643d39d7a5b43ae.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: [3 July 2023 12:07 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/39 "2023-07-03T12:07:37Z")

</div>

Ah! Thanks for that. I had no idea what I was doing!  
I don't know how it works but it seems to once I made the topics different.

---

<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: [3 July 2023 12:23 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/40 "2023-07-03T12:23:40Z")

</div>

may be NR treats strings that look like numbers as numbers?

---

<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: [3 July 2023 12:51 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/41 "2023-07-03T12:51:29Z")

</div>

> [@smanjunath211](#):
>
> NR treats strings that look like numbers as numbers?

No! Strings are compared character by character.  
"51" is greater than "50" but "100" is less than "50"

---

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

</div>

Ah! That's good to know.

In fact it would have worked for all numbers in the range (a percentage) up to but not including 100. At that point it would have failed and I would be scratching my head again.

---

<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: [3 July 2023 15:29 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/43 "2023-07-03T15:29:57Z")

</div>

Well yes except "49" \< "7" etc.

---

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

</div>

From 10 to 99 then?

---

<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 11:00 UTC](https://discourse.nodered.org/t/logic-gate-doesnt-seem-to-behave-logically/79531/45 "2023-07-05T11:00:22Z")

</div>

If you want to compare numbers then you should convert them from String type to Number first.

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

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