# Help with "OR" logic needed

**URL:** <https://discourse.nodered.org/t/help-with-or-logic-needed/16746>\
**Category:** General\
**Created:** [15 October 2019 19:40 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746 "2019-10-15T19:40:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [15 October 2019 19:40 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/1 "2019-10-15T19:40:05Z")

</div>

Strangely I am struggling with the seemingly simple flow.

I have two lights which can be switch on independently, they output "ON" or "OFF" on their own MQTT topic (left and right). I want it so that if either light is on, an 1 or 0 is passed onto the output .

(i.e. boolean OR).

I have installed a bool node which you can see, but I can't figure it out.

> **[node-red-contrib-bool-gate](https://flows.nodered.org/node/node-red-contrib-bool-gate)**
>
> Node RED node to include boolean logic gates

Any tips would be appreciated, thanks...

 ![OR%20logic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b415ae6cb8920a812f3ff3ed4e363a61424b1e80.png)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [15 October 2019 19:52 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/2 "2019-10-15T19:52:38Z")

</div>

Show us the flow that you use in the OR node.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2019 19:56 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/3 "2019-10-15T19:56:56Z")

</div>

Hi.

Don't fret, it can be done. Alas this problem is more a case of making the simple complicated.  
I'm good at doing that myself.

You really don't need the `or` gate. Just connect the output/s of the `mqtt` nodes to the input of the `change 2 rules` node.

All will work.

This is because Node-Red is controlled by message rather than logic states.

So when either node receives a signal, it - the signal - is just sent to the output which is then connected to the next node in the sequence. That being the `change 2 rules` node.

That is more like a **pulse** than an actual logic level.

Dumb question from me now:

Ok, I get that there are two _inputs_ to control the `gpio` pin.  
But why two `MQTT` nodes?

Really you only need one.

In/on the other part of the flow that generates the signal, you have nodes, which then go to the `MQTT` node to send the message.

That publishes to `turn on <light name>`

The other part of the flow:  
The `MQTT` node can publish to the same topic.

Here is a basic flow which shows what I mean: Your way and the way I suggested.  
Note: Neither is really any better, but just the way I suggest the "OR" function is done in MQTT.  
(Also note: Either way there needs to be an "OR" function done anyway.)

 ![Screenshot%20from%202019-10-16%2006-53-26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f5dcd1ff7cab41c88f0cafc9118b2e1027f6e776.png)

 ![Screenshot%20from%202019-10-16%2006-53-37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d9e16b511e6897be8599175d4c9d64090a25e832.png)

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [15 October 2019 19:58 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/4 "2019-10-15T19:58:26Z")

</div>

Hi edje11,

Below is how the OR node is configured, I think it is wrong.

But I'm happy with an alternative way, I thought maybe a function node but I am unsure how to handle the two inputs.

 ![OR%20node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/26e8b901659f1a11d73e40c0cbe0763990bbe091.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:** [15 October 2019 20:04 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/5 "2019-10-15T20:04:25Z")

</div>

Put a debug node showing what is going into the OR gate and check that the topics and payloads are as you have specified. If they look ok then post them here.  
Does the node status shown ever go true?

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [15 October 2019 20:07 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/6 "2019-10-15T20:07:27Z")

</div>

Hi @Trying_to_learn

Thank you very much for the comprehensive reply, I need to take some time to fully read and understand. I'm ot sure how you are configuring the change node to be honest.

I'm using two MQTT nodes as each light can be controlled by a local button, and so one or the other can be on (or both).

You give me an idea, could I use one MQTT node and use a wildcard?  
[https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/](https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/)

My MQTT topics are:  
house/kitchen/lights/left/stat/POWER  
house/kitchen/lights/right/stat/POWER

So I could use:  
house/kitchen/lights/+/stat/POWER

Or maybe I still need the OR as one can be off and the other ON.  
I will give it a try when back at my PC.

---

<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:** [15 October 2019 20:07 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/7 "2019-10-15T20:07:55Z")

</div>

The or gate is the best way. It is the right tool for this job.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 October 2019 20:16 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/8 "2019-10-15T20:16:32Z")

</div>

> [@877](#):
>
> My MQTT topics are:  
> house/kitchen/lights/left/stat/POWER  
> house/kitchen/lights/right/stat/POWER
> 
> So I could use:  
> house/kitchen/lights/+/stat/POWER
> 
> Or maybe I still need the OR as one can be off and the other ON.  
> I will give it a try when back at my PC.

That is different to what I originally read.

I read that you wanted to control ONE light from TWO points.  
That being the case, you have a flow which sends an "on/off" signal to MQTT with a topic to control that specific light.

Duplicate that part of the flow for the other switch (physical switch to press).

Then at the other side of the MQTT where the signal is received, you have only ONE `MQTT` (receive) node.

You can't use one switch (physical) to control TWO lights. Not easily anyway.  
Which is what you are describing now.

The original flow you showed (the screen shot) is confusing in that it only shows one GPIO pin being controlled.  
The MQTT nodes to the left show two different topics.  
So from that stand point, the message shouldn't really meet. They are about different things.

Rather than posting a screen shot of the `or` gate:  
Select it, go to the menu (top right) the Node-Red menu, not the browser menu, select `export` then `to clipboard` and paste that here.

But I feel that is now going way beyond the original question.

Maybe show a bigger picture of what you are doing. Both the _send_ and _receive_ parts of the flow and include both GPIO parts.

---

<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:** [15 October 2019 20:31 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/9 "2019-10-15T20:31:54Z")

</div>

> [@Trying\_to\_learn](#):
>
> That is different to what I originally read.

He/she has two lights controlled by two switches, but also wants to know when either light1 OR light2 is on. So he needs the OR gate or something that will do the same thing.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [15 October 2019 21:13 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/10 "2019-10-15T21:13:03Z")

</div>

That’s exactly correct.

I’m away from my PC but I will post full flows and debugs tomorrow, thank you all for the help so far!!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [15 October 2019 22:25 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/11 "2019-10-15T22:25:53Z")

</div>

I just played with that node (assuming its node-red-contrib-bool-gate) and I think what you need to do is use the msg.bool value its outputting and convert that to a 1 or 0 to pass on

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

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [16 October 2019 04:49 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/12 "2019-10-16T04:49:36Z")

</div>

I think your topics are a bit odd since Node-Red can poll the topics (can it?). So it's not a status. Instead when the on command is published to the Left Kitchen topic it gets forwarded to the or gate (probably not necessary). Or if a command is published to the Right Kitchen topic. So the change rules will still work and Pin V104 will either be 1 or 0 depending on if either Left or Right light sends on or off.

At least that's the way it works on my HA. 🙂

PS I usually don't use the retain flag on my home automation. But I also don't retain the state of my devices. I can poll the device to find out it's state (Tasmota and ESP-8266 or ZigBee devices).

---

<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:** [16 October 2019 07:27 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/13 "2019-10-16T07:27:30Z")

</div>

> [@ncherry](#):
>
> I think your topics are a bit odd since Node-Red can poll the topics (can it?).

I guess he is using the Tasmotta s/w in the device so whenever the switch is switched it sends the new state in a topic such as the one described. I believe that is a much better way than polling. Why poll when the device will tell you when things change?

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [16 October 2019 12:22 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/14 "2019-10-16T12:22:00Z")

</div>

Colin, you are absolutely correct, I forgot about the all the other topics that Tasmota has. I mostly use the one shot topics. The topics that send the status every so often can make problems like this a bit tough (one switch is on, one is off). And the many choices you have to communicate with the device is why Tasmota is so popular with the ESP8266 🙂

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [16 October 2019 13:58 UTC](https://discourse.nodered.org/t/help-with-or-logic-needed/16746/15 "2019-10-16T13:58:08Z")

</div>

@Colin correct again, yes both devices are Sonoff Basics running Tasmota.

Thanks everyone for all the replies, I have managed to get it working with help from @cymplecy 's post above too.

I'm not sure it's the most efficient so any suggestions are welcome 👍

Overview of flow:

 ![01%20overall%20flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/55e89a55e295a4c8703ced59d60a4529000ae554.png)

OR node configuration:

 ![02%20OR%20node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/ce2deb30d8b90484b3b6b12094cc57c825ff1c93.png)

Change node configuration (1 or 255 output for Blynk LED widget)

 ![03%20change%20node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/113bfd330f5d4c353351fd568d4c1f12c02ff034.png)

```auto
[{"id":"ae6011bb.25c54","type":"mqtt in","z":"fa1345d7.0957a8","name":"house/kitchen/lights/left/stat/POWER","topic":"house/kitchen/lights/left/stat/POWER","qos":"1","datatype":"auto","broker":"","x":240,"y":1580,"wires":[["7a0232e9.b0570c"]]},{"id":"263c0c3.67bd5f4","type":"mqtt in","z":"fa1345d7.0957a8","name":"house/kitchen/lights/right/stat/POWER","topic":"house/kitchen/lights/right/stat/POWER","qos":"1","datatype":"auto","broker":"","x":250,"y":1640,"wires":[["7a0232e9.b0570c"]]},{"id":"472b281a.a2cd28","type":"comment","z":"fa1345d7.0957a8","name":"Outputs \"ON\" or \"OFF\"","info":"","x":200,"y":1540,"wires":[]},{"id":"7a0232e9.b0570c","type":"or-gate","z":"fa1345d7.0957a8","name":"","rules":[{"t":"eq","v":"ON","vt":"str","propertyType":"msg","property":"payload","topic":"house/kitchen/lights/left/stat/POWER"},{"t":"eq","v":"ON","vt":"str","propertyType":"msg","property":"payload","topic":"house/kitchen/lights/right/stat/POWER"}],"outputTopic":"","gateType":"or","emitOnlyIfTrue":false,"x":550,"y":1600,"wires":[["30b94c1b.efd774"]]},{"id":"30b94c1b.efd774","type":"change","z":"fa1345d7.0957a8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"bool","tot":"msg"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"255","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":1600,"wires":[["48be3e04.c20d"]]},{"id":"48be3e04.c20d","type":"debug","z":"fa1345d7.0957a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":1600,"wires":[]}]
```
