Help with "OR" logic needed

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.

Any tips would be appreciated, thanks...

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

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.)

2 Likes

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.

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?

1 Like

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/

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.

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

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.

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.

1 Like

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!!

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

1 Like

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. :slight_smile:

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).

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?

2 Likes

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 :slight_smile:

@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 :+1:

Overview of flow:

OR node configuration:

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

[{"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":[]}]
1 Like