Flow to get output from a logical sequence of events

Hi,
I need help in writing a flow to achieve the following logic:
there are 2 boolean inputs. D1, D2 and they work in the following manner:

step0: D2 = 0 AND D1 = 0 or 1, OUTPUT = 0
step1: D2 = 1 AND D1 = 0, OUTPUT = 0 (since OUTPUT = 0 in step0, D1 can change between 1 and 0 several times, but OUTPUT should always be 0)
step2: D2 = 1 AND D1 = 1, OUTPUT = 1
step3: D2 = 1 AND D1 = 0, OUTPUT = 1 (since OUTPUT is 1 in step2, D1 can change between 1 and 0 several times, but OUTPUT should always be 0)
step4: D2 = 0 AND D1 =0, OUTPUT = 0

step0 repeats from here

Hi,

Did you search for 'boolean' nodes in the 'Manage Palette' menu?

I'd suggest: node-red-contrib-boolean-logic-ultimate as it is recently updated.

Cheers,

Paul

yeah. i already did that. following is the flow I have created. but, i am not getting the required output at step 3.

I tried without the function node and used the RBE node after D2. but still not successful.

[
    {
        "id": "f8929a36b3d98fcf",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "6cf34fa456b9c31d",
        "type": "debug",
        "z": "f8929a36b3d98fcf",
        "name": "debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 140,
        "wires": []
    },
    {
        "id": "30f6364d3e8028bf",
        "type": "inject",
        "z": "f8929a36b3d98fcf",
        "name": "",
        "props": [
            {
                "p": "payload1",
                "v": "true",
                "vt": "bool"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "x": 130,
        "y": 80,
        "wires": [
            [
                "afccee4f639f7f75"
            ]
        ]
    },
    {
        "id": "bbae1e44a79c8ed5",
        "type": "inject",
        "z": "f8929a36b3d98fcf",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "true",
        "payloadType": "bool",
        "x": 140,
        "y": 160,
        "wires": [
            [
                "0bbaafc7dd3d3a14"
            ]
        ]
    },
    {
        "id": "0bbaafc7dd3d3a14",
        "type": "BooleanLogicUltimate",
        "z": "f8929a36b3d98fcf",
        "name": "",
        "payloadPropName": "payload",
        "filtertrue": "both",
        "persist": true,
        "sInitializeWith": "WaitForPayload",
        "triggertopic": "2",
        "outputtriggeredby": "onlyonetopic",
        "inputCount": 2,
        "topic": "5",
        "restrictinputevaluation": false,
        "delayEvaluation": 0,
        "x": 540,
        "y": 160,
        "wires": [
            [
                "6cf34fa456b9c31d"
            ],
            [],
            []
        ]
    },
    {
        "id": "afccee4f639f7f75",
        "type": "function",
        "z": "f8929a36b3d98fcf",
        "name": "function 5",
        "func": "msg.payload = (msg.payload1 == context.get('hold')) ? false: true;\ncontext.set('hold', msg.payload1)\nmsg.topic = 3\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 80,
        "wires": [
            [
                "0bbaafc7dd3d3a14"
            ]
        ]
    }
]

Admin edit: wrap flow in triple backticks

In simple terms, I guess you want

If D0 and D1 are BOTH on SET FLAG
If D0 and D1 are BOTH off RESET FLAG

Dont change FLAG at any other time

Correct?

This should get you close...

chrome_KteRjfl8XX

[{"id":"20fb0cee8ea2a2ef","type":"inject","z":"ccdc4f1f78201a08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D1","payload":"true","payloadType":"bool","x":1190,"y":80,"wires":[["8f4edbe65aa91e82"]]},{"id":"6e90f6c6cac202aa","type":"inject","z":"ccdc4f1f78201a08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D1","payload":"false","payloadType":"bool","x":1200,"y":120,"wires":[["8f4edbe65aa91e82"]]},{"id":"e78180a864673fb8","type":"inject","z":"ccdc4f1f78201a08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D2","payload":"true","payloadType":"bool","x":1190,"y":180,"wires":[["8f4edbe65aa91e82"]]},{"id":"049bfceb212465ed","type":"inject","z":"ccdc4f1f78201a08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D2","payload":"false","payloadType":"bool","x":1200,"y":220,"wires":[["8f4edbe65aa91e82"]]},{"id":"8f4edbe65aa91e82","type":"join","z":"ccdc4f1f78201a08","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1390,"y":140,"wires":[["1f15ccceeb177a3c"]]},{"id":"e6780c198ec5ae2a","type":"debug","z":"ccdc4f1f78201a08","name":"Outputs when changes from true->false->true only","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2030,"y":180,"wires":[]},{"id":"1f15ccceeb177a3c","type":"change","z":"ccdc4f1f78201a08","name":"both same?","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.D1 = payload.D2 ? payload.D1 : null","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"result","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1430,"y":220,"wires":[["5767a98b344e8ffd"]]},{"id":"5767a98b344e8ffd","type":"switch","z":"ccdc4f1f78201a08","name":"both on \\n both off \\n different","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1600,"y":220,"wires":[["3a5ad5e34599d71e"],["3a5ad5e34599d71e"],["b92fc579cc8d19b1"]]},{"id":"b92fc579cc8d19b1","type":"debug","z":"ccdc4f1f78201a08","name":"no match / not valid","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1830,"y":240,"wires":[]},{"id":"3a5ad5e34599d71e","type":"rbe","z":"ccdc4f1f78201a08","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":1770,"y":180,"wires":[["e6780c198ec5ae2a"]]}]

NOTE: I used boolean (true/false) but it can easily be changed to 1/0

That was also what I came up with. Here's my take:

[{"id":"38412910c32a18b1","type":"tab","label":"Test Logic","disabled":false,"info":"","env":[]},{"id":"43d0d99b5d2f0d3d","type":"function","z":"38412910c32a18b1","name":"function 20","func":"let msg1 = {}\nlet msg2 = {}\nlet step = context.get('step') || 0\nlet output = context.get('output') || 0\nlet D1 = context.get('D1') || false\nlet D2 = context.get('D2') || false\n\nlet invar = msg.payload\nlet topic = msg.topic\nif(topic === 'D1'){D1 = invar}\nif(topic === 'D2'){D2 = invar}\n\nswitch (step) {\n    case 0:\n        if(D1 && D2) {\n            step = 1\n            output = 1\n        }\n        break\n    case 1:\n        if (!D1 && !D2) {\n        step = 0\n        output = 0\n        }\n        break\n    default:\n        step = 0\n}\nmsg1.payload = step\nmsg2.payload = output\nnode.send([msg1, msg2]);\n\ncontext.set('step', step)\ncontext.set('output', output)\ncontext.set('D1', D1)\ncontext.set('D2', D2)\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":200,"wires":[["81c4ba6731e62855"],["4679d13c2f22cdea"]]},{"id":"445a0cf0c46fd714","type":"ui_switch","z":"38412910c32a18b1","name":"D1","label":"D1","tooltip":"","group":"edde913e86badb5a","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":90,"y":160,"wires":[["8e71b1311a204d9f"]]},{"id":"965edb6a4621c41c","type":"ui_switch","z":"38412910c32a18b1","name":"","label":"D2","tooltip":"","group":"edde913e86badb5a","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":90,"y":240,"wires":[["5545be1b77608ba3"]]},{"id":"81c4ba6731e62855","type":"ui_text","z":"38412910c32a18b1","group":"edde913e86badb5a","order":2,"width":0,"height":0,"name":"","label":"Step","format":"{{msg.payload}}","layout":"row-spread","x":690,"y":160,"wires":[]},{"id":"4679d13c2f22cdea","type":"ui_gauge","z":"38412910c32a18b1","name":"","group":"edde913e86badb5a","order":1,"width":0,"height":0,"gtype":"gage","title":"Output","label":"units","format":"{{value}}","min":0,"max":"1","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":690,"y":240,"wires":[]},{"id":"8e71b1311a204d9f","type":"function","z":"38412910c32a18b1","name":"function 21","func":"msg.topic = 'D1'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":160,"wires":[["43d0d99b5d2f0d3d"]]},{"id":"5545be1b77608ba3","type":"function","z":"38412910c32a18b1","name":"function 22","func":"msg.topic = 'D2'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":240,"wires":[["43d0d99b5d2f0d3d"]]},{"id":"2730b91a132b3ffe","type":"comment","z":"38412910c32a18b1","name":"dashboard-evi","info":"","x":470,"y":320,"wires":[]},{"id":"edde913e86badb5a","type":"ui_group","name":"Test","tab":"36910c33c3a088cb","order":8,"disp":true,"width":"6","collapse":false},{"id":"36910c33c3a088cb","type":"ui_tab","name":"Test Logic","icon":"dashboard","order":3,"disabled":false,"hidden":false}]
1 Like

Thanks @Steve-Mcl @Frida

Both your solutions work fine for me. If I inject D1 and D2 with 0/1 continuously at 10 sec interval, then the one from @Steve-Mcl gives one output only and next output comes only when the state changes. while the one from @Frida gives continuous output of the last state. Either solutions are fine for me.

thanks once again. it is a big help!

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