NAND gate 4 flow Booleans

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.

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

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

[{"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

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

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?

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.

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

1 Like

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?

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

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