Node-RED as Alternative Rule Engine

Hi

Thanks for your help.

There is a good use case for including the RELEASED events into this logic, as there are some situations where an action is needed when the button / input is pressed and another when it is released.

For example…

Start on Press
Stop on Release

What I am trying to create is a Generic Solution that I can save in a library and adapt for real world flows.

It looks like I can use either solution, but I can’t use both in the same IF Function.

These are the solutions I have running.

The IF statement is this

var event = msg.payload.event;
var channel = msg.payload.channel;
var newMsg = { payload: msg.payload.event };
var button = "velbus:vmbgp2:c5053467:24:input#CH1"

if  (channel === button )  {
	if (event === "PRESSED") {
		return [newMsg, null, null];
	}
	if (event === "LONG_PRESSED") {
		return [null, newMsg, null];
	}
	if (event === "RELEASED") {
		return [null, null, newMsg];
	}
}
return null;

With the nodes being configured like this

[
    {
        "id": "ce1cbddc.92129",
        "type": "openhab2-events",
        "z": "aa997a33.1a22b8",
        "name": "openHAB events in House",
        "controller": "4a048897.2cd4b8",
        "itemname": "",
        "x": 130,
        "y": 700,
        "wires": [
            [
                "a5971cdc.6521b",
                "9fed8fe8.b0ddb"
            ]
        ]
    },
    {
        "id": "a5971cdc.6521b",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 370,
        "y": 700,
        "wires": []
    },
    {
        "id": "9fed8fe8.b0ddb",
        "type": "function",
        "z": "aa997a33.1a22b8",
        "name": "Filter Button 1",
        "func": "var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar newMsg = { payload: msg.payload.event };\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH1\"\n\nif  (channel === button )  {\n\tif (event === \"PRESSED\") {\n\t\treturn [newMsg, null, null];\n\t}\n\tif (event === \"LONG_PRESSED\") {\n\t\treturn [null, newMsg, null];\n\t}\n\tif (event === \"RELEASED\") {\n\t\treturn [null, null, newMsg];\n\t}\n}\nreturn null;\n",
        "outputs": 3,
        "noerr": 0,
        "x": 380,
        "y": 800,
        "wires": [
            [
                "64fd8f79.05a8c"
            ],
            [
                "74a144dc.11271c"
            ],
            [
                "dd9a0948.c5f888"
            ]
        ]
    },
    {
        "id": "64fd8f79.05a8c",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 630,
        "y": 740,
        "wires": []
    },
    {
        "id": "74a144dc.11271c",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 630,
        "y": 800,
        "wires": []
    },
    {
        "id": "dd9a0948.c5f888",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 630,
        "y": 860,
        "wires": []
    },
    {
        "id": "4a048897.2cd4b8",
        "type": "openhab2-controller",
        "z": "",
        "name": "House openHAB2 controller",
        "protocol": "http",
        "host": "127.0.0.1",
        "port": "8080",
        "path": "",
        "username": "",
        "password": ""
    }
]

The version with the IF Function to filter the button and a Switch to filter the button states, then with some triggers to reset the Debugs so that new events can be seen in the Flow, looks like this

The IF Function is simply this

var channel = msg.payload.channel;
var button = "velbus:vmbgp2:c5053467:24:input#CH1";
var newMsg = { payload: msg.payload.event };

if (channel === button) {
    return [newMsg];
}
return null;

With the Nodes configured like this

[
    {
        "id": "c1c183f4.eb661",
        "type": "openhab2-events",
        "z": "aa997a33.1a22b8",
        "name": "",
        "controller": "4a048897.2cd4b8",
        "itemname": "",
        "x": 100,
        "y": 993.3333320617676,
        "wires": [
            [
                "a4c00000.ed111",
                "83e9bf19.58393"
            ]
        ]
    },
    {
        "id": "a4c00000.ed111",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 290,
        "y": 960,
        "wires": []
    },
    {
        "id": "83e9bf19.58393",
        "type": "function",
        "z": "aa997a33.1a22b8",
        "name": "Filter Button",
        "func": "var channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH1\";\nvar newMsg = { payload: msg.payload.event };\n\nif (channel === button) {\n    return [newMsg];\n}\nreturn null;\n\n",
        "outputs": 1,
        "noerr": 0,
        "x": 290,
        "y": 1040,
        "wires": [
            [
                "95913777.5fd898",
                "36abae9e.736c92"
            ]
        ]
    },
    {
        "id": "95913777.5fd898",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 470,
        "y": 1000,
        "wires": []
    },
    {
        "id": "36abae9e.736c92",
        "type": "switch",
        "z": "aa997a33.1a22b8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "LONG_PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "RELEASED",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 450,
        "y": 1080,
        "wires": [
            [
                "f4027050.bc3f9"
            ],
            [
                "a85767f8.14f988"
            ],
            [
                "4aec6972.c7a7e8"
            ]
        ]
    },
    {
        "id": "f4027050.bc3f9",
        "type": "trigger",
        "z": "aa997a33.1a22b8",
        "op1": "",
        "op2": "RESET",
        "op1type": "pay",
        "op2type": "str",
        "duration": "250",
        "extend": false,
        "units": "ms",
        "reset": "",
        "bytopic": "all",
        "name": "",
        "x": 660,
        "y": 1020,
        "wires": [
            [
                "97ff3822.52ccf8"
            ]
        ]
    },
    {
        "id": "a85767f8.14f988",
        "type": "trigger",
        "z": "aa997a33.1a22b8",
        "op1": "",
        "op2": "RESET",
        "op1type": "pay",
        "op2type": "str",
        "duration": "1",
        "extend": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "name": "",
        "x": 640,
        "y": 1100,
        "wires": [
            [
                "7db22549.5acb4c"
            ]
        ]
    },
    {
        "id": "4aec6972.c7a7e8",
        "type": "trigger",
        "z": "aa997a33.1a22b8",
        "op1": "",
        "op2": "RESET",
        "op1type": "pay",
        "op2type": "str",
        "duration": "300",
        "extend": false,
        "units": "ms",
        "reset": "",
        "bytopic": "all",
        "name": "",
        "x": 660,
        "y": 1180,
        "wires": [
            [
                "aaf5edf3.a83a5"
            ]
        ]
    },
    {
        "id": "97ff3822.52ccf8",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 830,
        "y": 1020,
        "wires": []
    },
    {
        "id": "7db22549.5acb4c",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 830,
        "y": 1100,
        "wires": []
    },
    {
        "id": "aaf5edf3.a83a5",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 830,
        "y": 1180,
        "wires": []
    },
    {
        "id": "4a048897.2cd4b8",
        "type": "openhab2-controller",
        "z": "",
        "name": "House openHAB2 controller",
        "protocol": "http",
        "host": "127.0.0.1",
        "port": "8080",
        "path": "",
        "username": "",
        "password": ""
    }
]