Rob's Node-Red Template and Example Flow Thread!

I have NEVER understood this concept. I read about it, try it … it breaks or gives me results that I didn’t expect or understand. Maybe one day when we have a few minutes, you could explain that to me and others. Never too late to learn abut them.

This is part of why I love NR, there are so many ways to do the same thing, and there is no “right” or “wrong” way to do something, just ways to do it :slight_smile:

MY biggest issue with starting out with the boolean nodes, aside from the spotty performance history with XOR operations, is that It can keep you from having to fully understand and grasp some important concepts in NR, and without you even realizing it you will be limiting your automations potential.

Sure use it to get started, help simplify your flows and make them easier to troubleshoot and understand.

But PLEASE don’t do yourself a disservice, make sure you still take the time to understand the logic and structures behind the manual way of doing it!!!

This goes for pretty much any node in NR. Use them to make your life easier, that’s their job, but make sure you take the time to understand how and why the node does what it does. You wont regret it!

2 Likes

Think of it like digital circuit design - after covering all the background on transistors and impedances and all that good stuff, you get to the first real practical devices: flip-flops, AND, and OR gates. These are boolean comparitors: inputs are either on/off (true/false) and the output is similarly binary.

In words, and AND gate gives you a true value if all inputs are true. For any other combination of inputs the output value is false. Here’s a picture I stole from the Internet:

image

An OR gate will give you a true output when ANY of the inputs is true, up to and including the case where all inputs are true. The only time an OR gate will give a false output is if all inputs are false. Another stolen picture:
image

The AND and OR gates don’t change in concept with a larger number of inputs: AND gives a true output only when all inputs are true. OR gives a true output when any input is true.

Let’s transition to motion lighting: I have three motion sensors and a door contact sensor for my basement hallway zone. That hallway is windowless and pitch black if the lights are off. Motion sensors are positioned to detect anyone entering the zone, so that means that I want to turn on the lights any time motion is detected - I don’t care which motion sensor trips, I want the lights on. Same with the door sensor - someone is coming into the hall from the garage and the lights need to be on. This is the perfect scenario for an OR gate: if sensor 1 OR sensor 2 OR sensor 3 OR sensor 4 are true, flip on the lights.

The AND gate could be used (poorly, imo) for occupancy sensing: if this sensor AND that sensor trip, someone must be in the area, so do X. Too reliant on both sensors working properly. You could use a flow variable or a global as one input to an AND, for example: if someone is present at home (use whatever means you have to figure this out), a global variable is set true. Now add an AND gate to your motion lighting: at the end of the sequence, where you’re commanding the lights to turn on, put the AND gate. One input is the lights on command “true” and the other input is the presence state “true”. The lights will only come on if the normal motion lighting sequence triggers it AND somebody is home. Pets not allowed.

XOR, NAND, and NOR are other common gate types that you could dig into, but I think home automation tasks probably wouldn’t need them. Was this helpful in any way?

3 Likes

Hi there.
I’ve been trying to do the very same and have been struggling to get it to work. I have the boolean used installed.
Would you mind sharing the flow so I can see how you have done it.
Many thanks.

My very first flow export, so I’m uncertain if I did it right or not. Let me know if this works:

[{"id":"75af6a15836294e0","type":"comment","z":"474cb7d41eb3ceb5","name":"Basement Hallway Light","info":"","x":380,"y":480,"wires":[]},{"id":"fbf8ec15af1d0ecb","type":"mqtt in","z":"474cb7d41eb3ceb5","name":"MS-2","topic":"zwave/Basement/Basement_Hallway_MS-2/48/0/Motion","qos":"2","datatype":"auto-detect","broker":"a99b67b86f3f3e3e","nl":false,"rap":true,"rh":0,"inputs":0,"x":70,"y":580,"wires":[["351b2a6ddf5faaaa"]]},{"id":"351b2a6ddf5faaaa","type":"change","z":"474cb7d41eb3ceb5","name":"value","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"MS-2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":580,"wires":[["64ec30adc2697f2e"]]},{"id":"97300c0e415a41c8","type":"rbe","z":"474cb7d41eb3ceb5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":370,"y":540,"wires":[["b39c680b5b5ed8c9"]]},{"id":"9e5a6e3e9c17040e","type":"zigbee2mqtt-in","z":"474cb7d41eb3ceb5","name":"MS-1","server":"ac659ac3d7fc300e","friendly_name":"Basement Hallway MS-1 (SNZB-03)","device_id":"0x00124b001f44ebf8","state":"occupancy","outputAtStartup":true,"filterChanges":false,"enableMultiple":false,"x":70,"y":540,"wires":[["93c7656ac2313c99"]]},{"id":"93c7656ac2313c99","type":"change","z":"474cb7d41eb3ceb5","name":"Topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"MS-1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":540,"wires":[["97300c0e415a41c8"]]},{"id":"64ec30adc2697f2e","type":"rbe","z":"474cb7d41eb3ceb5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":370,"y":580,"wires":[["b39c680b5b5ed8c9"]]},{"id":"126a64d36474acf2","type":"rbe","z":"474cb7d41eb3ceb5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":370,"y":620,"wires":[["b39c680b5b5ed8c9"]]},{"id":"b39c680b5b5ed8c9","type":"BooleanLogicUltimate","z":"474cb7d41eb3ceb5","name":"MotionDoor","payloadPropName":"payload","filtertrue":"both","persist":true,"sInitializeWith":"false","triggertopic":"trigger","outputtriggeredby":"all","inputCount":"4","topic":"BMTmotion","restrictinputevaluation":true,"delayEvaluation":0,"x":560,"y":600,"wires":[[],["952bb276a749f54a"],[]]},{"id":"054acfd1fd83e7ba","type":"mqtt in","z":"474cb7d41eb3ceb5","name":"MS-3","topic":"zwave/Basement/Basement_Hallway_MS-3/48/0/Motion","qos":"2","datatype":"auto-detect","broker":"a99b67b86f3f3e3e","nl":false,"rap":true,"rh":0,"inputs":0,"x":70,"y":620,"wires":[["8c880437b185bdde"]]},{"id":"8c880437b185bdde","type":"change","z":"474cb7d41eb3ceb5","name":"value","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"MS-3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":620,"wires":[["126a64d36474acf2"]]},{"id":"12d831310d207a4c","type":"mqtt in","z":"474cb7d41eb3ceb5","name":"Basement Door","topic":"zwave/Basement/Basement_Door_Entry_Sensor/48/0/Any","qos":"2","datatype":"auto-detect","broker":"a99b67b86f3f3e3e","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":660,"wires":[["ef4dec473d7d1b37"]]},{"id":"ef4dec473d7d1b37","type":"change","z":"474cb7d41eb3ceb5","name":"value","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"Basement_Door","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":660,"wires":[["c9fcb2d1f332856c"]]},{"id":"c9fcb2d1f332856c","type":"rbe","z":"474cb7d41eb3ceb5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":370,"y":660,"wires":[["b39c680b5b5ed8c9"]]},{"id":"952bb276a749f54a","type":"rbe","z":"474cb7d41eb3ceb5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":730,"y":600,"wires":[["a2dc8204fbb368cc"]]},{"id":"a2dc8204fbb368cc","type":"switch","z":"474cb7d41eb3ceb5","name":"active?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":540,"wires":[["4e04389e0fbc5306","a2e588cb49418997"],["a44d801ffb56c27f"]]},{"id":"4e04389e0fbc5306","type":"change","z":"474cb7d41eb3ceb5","name":"STOP","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":500,"wires":[["a44d801ffb56c27f"]]},{"id":"a44d801ffb56c27f","type":"stoptimer3","z":"474cb7d41eb3ceb5","duration":"5","durationType":"num","units":"Minute","payloadtype":"num","payloadval":"0","name":"5 minute timer","x":840,"y":520,"wires":[["a2e588cb49418997"],[]]},{"id":"a2e588cb49418997","type":"switch","z":"474cb7d41eb3ceb5","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":580,"wires":[["4218d056cc733269"],["774b5f492b9b771c"]]},{"id":"89650f2a773344c2","type":"mqtt out","z":"474cb7d41eb3ceb5","name":"Send to switch","topic":"zwave/Basement/Basement_Hallway_Light/37/0/targetValue/set","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a99b67b86f3f3e3e","x":1240,"y":580,"wires":[]},{"id":"774b5f492b9b771c","type":"change","z":"474cb7d41eb3ceb5","name":"Reset payload false","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1050,"y":600,"wires":[["89650f2a773344c2"]]},{"id":"4218d056cc733269","type":"change","z":"474cb7d41eb3ceb5","name":"Reset payload true","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1050,"y":560,"wires":[["89650f2a773344c2"]]},{"id":"a99b67b86f3f3e3e","type":"mqtt-broker","name":"Core MQTT","broker":"10.10.2.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"ac659ac3d7fc300e","type":"zigbee2mqtt-server","name":"Core z2m","host":"10.10.2.1","mqtt_port":"1883","mqtt_username":"","mqtt_password":"","mqtt_qos":"0","tls":"","usetls":false,"base_topic":"zigbee2mqtt"}]
1 Like

Another significant benefit with using these boolean nodes is that they translate various states to true or false i.e.:

You can use the invert node to toggle the state of the sensor if needed.

IMHO, using the boolean node has made my flow much easier to create and understand; Especially as the boolean nodes shows you the results of all three operations under the node. :slight_smile:

4 Likes

Yes, you did it correctly. :clap:

1 Like

This is all great information. I would just really like to suggest again we have separate threads for flows instead of one massive topic. At least for the basic get started stuff. It just makes it overwhelming trying to find the info you want.

3 Likes

It imported OK. Thankyou.
Well, I had a look and can confidently say, “I’m completely lost”. :slight_smile:

I was sort of on the right track with my original effort but this has just confused me with all these topics and stuff!!! :wink: As you can tell I’m just learning.
I will keep persevering.

EDIT: On further examination I can see what each node is doing. It is starting to make a bit of sense and I think I may be able to work it out. Thanks again.

2 Likes

We can fire up a teams later on Node-RED if anyone wants to join. Screen recordings or any type are explicitly forbidden.

1 Like

I would be able to join anytime after 8pm EST. Would love if someone can go over the fundamentals (like global variables, flow overrides, etc)
I made a few flows and they seem to work, so any input on what to improve would also be great!

1 Like

Done, we’ll work in a Teams meeting tonight. Come and go, but don’t leave without saying goodbye. We’ll post a fresh topic with the meeting details later when we fire things up.

2 Likes

Yeah the topics thing is just a way for the boolean node to distinguish different inputs, since the node doesn’t dynamically create an appropriate # of input terminals. Would be super cool if it did…

Edit: and for those of you wondering, I use the filter node liberally because I’m lazy. I assume my devices will send updates even when there’s no state change, so the filter node is my go-to signal cleanup circuit. I don’t want to trigger more than once for a single valued sensor reading. I haven’t actually verified if this is needed on a per-device basis.

1 Like

Well your flow has filled in a couple of parts I was missing. I now have it working as I want, I think.

Just using 2 motion sensors as a test and sending a pushover message, when sensors go active I get the following messages.
1 active
AND = false
OR = true
XOR = true

2 active
AND = true
OR = true
XOR = false

So I’m assuming I can use the O/P’s as I need.
Either sensor then OR must = true.
If I need both then AND must = true.
For all inactive then I will need OR AND and XOR = false.
I think I can do that.
Any thoughts on my theory?

1 Like

Good up to the last line… no need to check the AND or XOR outputs for an all-false condition. The OR gate gives this to you on its own. Glad you got it to this point! It’s pretty smooth sailing once you’ve got the topic-delimited inputs figured out…

Yes. I was thinking about this after I posted. I don’t think I really need the XOR. Can work things without using that function.
Thanks again for the pointers though.
I’m gradually moving everything from webCoRE onto NR but still using my HE hubs.
I have migrated about 25 devices to CC from HE, 15 of which were LightwaveRF stuff which are now running faster as I have taken out the middle man. An RPi.
I must say the more I’m getting my head around NR on CC I can see this as being the way to go. I may have to get another for backup. :slight_smile: :grin:

EDIT: Anyone want a C3hub? :wink:

1 Like

There used to be a forum specifically for Node-Red and this was supposed to just be a personal thread for me to share flows and find out what things people wanted future tutorials on, but it kinda went off the rails. :joy:

I actually removed all of these types of sub-forums awhile back because they weren’t being used and a lot of topics belong in multiple forums, it was become a logistics nightmare trying to figure out where each post best fit.

Thus I implemented TAGS on the forum to make sharing, searching for, and finding the information you are after easy.

Click Tags here and then select a tag related to what you are looking for.
image

When posting click tags here and add appropriate tags.

If there is a tag you feel should exist that doesn’t use the features requests sub-forum to let april and markus know what tags you would like added!

1 Like

No sweat. Just thought it may be easier to search with separate threads. Thank you @RRodman

A post was split to a new topic: New Hue Firmware Coming