# NAND gate 4 flow Booleans

**URL:** <https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162>\
**Category:** General\
**Created:** [25 November 2023 00:28 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162 "2023-11-25T00:28:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eerke2021](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eerke2021/32/83528_2.png) [@eerke2021](https://discourse.nodered.org/u/eerke2021)\
**Post date:** [25 November 2023 00:28 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/1 "2023-11-25T00:28:02Z")

</div>

Hi,

I have 4 separate Booleans on 4 different flows, each is called the same (flow.occupancy), each of which relates to a section of my house.

If none of the 4 areas have anyone in them (eg occupancy = false) it means the house is empty. I’m about to tunnel between flows repeatedly but then thought there maybe a more elegant way of doing this.  
My plan was if zone one becomes false it could check zone 2 was false and then zone 3 and zone 4, but also 2 becomes not empty and checks 1, 3 and 4, zone 3 checks 1, 2 and 4 and zone 4 checks 1, 2, 3 although I may be able to minimise it a bit by knowing it is impossible for zone 1 to empty before 2 etc. Would need to think about it.  
Is there a more elegant way of doing this? I guess they could be saved to file and then read in flows outside their usual scope, but that sounds like as much work as my current plan. Or adjusting all the nodes so they have different names and setting the context for all to global…  
Not sure I planned this very well.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [25 November 2023 08:32 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/2 "2023-11-25T08:32:15Z")

</div>

You have not stated if using any boolean node collection.  
if not, I highly recommend the below.

as it allows to create AND, OR, XOR gates.  
I use it myself, to check if any doors are open before arming the sensors, when leaving the home

> **[node-red-contrib-boolean-logic-ultimate](https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate)**
>
> A set of Node-RED enhanced boolean logic and utility nodes, flow interruption, blinker, invert, filter, toggle etc.., with persistent values after reboot. Compatible also with Homeassistant values.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 November 2023 09:26 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/3 "2023-11-25T09:26:21Z")

</div>

You can also use a join node followed by a switch node using a JSONata expression for the NAND (or change node if an boolean is required).  
e.g

```auto
[{"id":"34f859394b66e1e1","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone1","payload":"true","payloadType":"bool","x":180,"y":1400,"wires":[["4487f4117f1be690"]]},{"id":"4487f4117f1be690","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":1480,"wires":[["0f138aed999b59b0","5c88e750c1328b74"]]},{"id":"7699491c2b4841bc","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone1","payload":"false","payloadType":"bool","x":190,"y":1440,"wires":[["4487f4117f1be690"]]},{"id":"a3166e7879679525","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone2","payload":"true","payloadType":"bool","x":180,"y":1480,"wires":[["4487f4117f1be690"]]},{"id":"95ef21c5a8c1ad9d","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone2","payload":"false","payloadType":"bool","x":190,"y":1520,"wires":[["4487f4117f1be690"]]},{"id":"a220f4cfdc095426","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone3","payload":"true","payloadType":"bool","x":180,"y":1560,"wires":[["4487f4117f1be690"]]},{"id":"c3fb69d4f3bec2e3","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone3","payload":"false","payloadType":"bool","x":190,"y":1600,"wires":[["4487f4117f1be690"]]},{"id":"0f138aed999b59b0","type":"switch","z":"d1395164b4eec73e","name":"","property":"false in [$$.payload.*]","propertyType":"jsonata","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":530,"y":1480,"wires":[["823bab56dd6ca759"],["50dfaab40ecb65b3"]]},{"id":"5c88e750c1328b74","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$not(false in [$$.payload.*])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1580,"wires":[["8adb971c2a53219a"]]},{"id":"50dfaab40ecb65b3","type":"debug","z":"d1395164b4eec73e","name":"not occupied","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":1520,"wires":[]},{"id":"823bab56dd6ca759","type":"debug","z":"d1395164b4eec73e","name":"occupied","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":1460,"wires":[]},{"id":"8adb971c2a53219a","type":"debug","z":"d1395164b4eec73e","name":"bool","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":1580,"wires":[]}]

```

You could also use link call nodes to do the check in each flow  
e.g

```auto
[{"id":"9d1662a5a8db42b8","type":"debug","z":"d1395164b4eec73e","name":"zone1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":1400,"wires":[]},{"id":"34f859394b66e1e1","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone1","payload":"true","payloadType":"bool","x":180,"y":1380,"wires":[["54056971ed073457"]]},{"id":"7699491c2b4841bc","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone1","payload":"false","payloadType":"bool","x":190,"y":1420,"wires":[["54056971ed073457"]]},{"id":"95ef21c5a8c1ad9d","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone2","payload":"false","payloadType":"bool","x":190,"y":1540,"wires":[["bc7a0d9881780919"]]},{"id":"a3166e7879679525","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone2","payload":"true","payloadType":"bool","x":180,"y":1480,"wires":[["bc7a0d9881780919"]]},{"id":"4487f4117f1be690","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":350,"y":1600,"wires":[["5c88e750c1328b74"]]},{"id":"5c88e750c1328b74","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$not(false in [$$.payload.*])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1600,"wires":[["d2b31733bde8f579"]]},{"id":"54056971ed073457","type":"link call","z":"d1395164b4eec73e","name":"","links":["bbb1681e9ad67793"],"linkType":"static","timeout":"30","x":410,"y":1400,"wires":[["9d1662a5a8db42b8"]]},{"id":"bc7a0d9881780919","type":"link call","z":"d1395164b4eec73e","name":"","links":["bbb1681e9ad67793"],"linkType":"static","timeout":"30","x":410,"y":1520,"wires":[["303927168087d09b"]]},{"id":"303927168087d09b","type":"debug","z":"d1395164b4eec73e","name":"zone1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":1520,"wires":[]},{"id":"bbb1681e9ad67793","type":"link in","z":"d1395164b4eec73e","name":"zone check","links":[],"x":225,"y":1600,"wires":[["4487f4117f1be690"]]},{"id":"d2b31733bde8f579","type":"link out","z":"d1395164b4eec73e","name":"link out 80","mode":"return","links":[],"x":675,"y":1600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![eerke2021](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eerke2021/32/83528_2.png) [@eerke2021](https://discourse.nodered.org/u/eerke2021)\
**Post date:** [25 November 2023 09:51 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/4 "2023-11-25T09:51:29Z")

</div>

Thanks. That could help a bit and more so in the future. The main problem is I have lots of variables with change nodes that set flow context variables to true or false. I could use those setting nodes to also set msg.payload to true or false and then tunnel to a new flow simply called ‘house occupied’ with a single 4 input AND node from that palette. As, I think, those flows only have 2 change nodes setting their flow context variables to true or false and there are 4 zones it is 8 tunnels instead of my idea that uses 12. The main problem seems to be that I have created lots of flow context variables and now want to access them outside the flow, so bad planning really, but 8 is still 33% less than 12.  
Do you know of a node that runs on boot so I can set variable defaults? I am thinking I need to reinstall fileBrowser so it can see my node-Red settings file (I’m in a docker) and then altering the settings file as per the node Red context guide, but maybe there is a trigger node I can use that activates on (re) boot?

---

<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:** [25 November 2023 10:14 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/5 "2023-11-25T10:14:33Z")

</div>

> [@eerke2021](#):
>
> The main problem is I have lots of variables with change nodes that set flow context variables to true or false.

I advise to avoid using context in this way wherever practical. Do what you need to do by passing values along wires, that is the node red way. I virtually never use flow or global context.

> [@eerke2021](#):
>
> Do you know of a node that runs on boot so I can set variable defaults

The Inject node can trigger on startup so you can initialise data.

---

<div class="post-metadata">

**Author:** ![eerke2021](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eerke2021/32/83528_2.png) [@eerke2021](https://discourse.nodered.org/u/eerke2021)\
**Post date:** [25 November 2023 10:51 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/6 "2023-11-25T10:51:20Z")

</div>

How do I use an inject node to do this? I looked at that node. I thought I could maybe set it to inject every minute a string and then a filter node, because I guessed the filter wasn't persistant, but did I miss an option?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [25 November 2023 11:29 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/7 "2023-11-25T11:29:47Z")

</div>

At the bottom of the `inject` node.  
this will result in it triggering when the flow is deployed - in my example after 100 ms

you can then piggy back onto this to carry out some initialisation

![Screenshot 2023-11-25 at 11.29.00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3ed833a4c79aa58c52f69f7522ded50601ddecdb.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 January 2024 11:29 UTC](https://discourse.nodered.org/t/nand-gate-4-flow-booleans/83162/8 "2024-01-24T11:29:54Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
