Node-RED as Alternative Rule Engine

For those using node red as an openhab rule engine substitute I’d really recommend installing the time range switch node


image

It gives you a lot of flexibility routing the payload one of two ways depending on a time range.

7 Likes

Yep really useful…

@BrutalBirdie

Hi

Did you ever find a solution to this issue?

I would like to be able to use Channel trigger data in NodeRed flows too.

Something like this

   Channel 'velbus:vmbgpod:d212bf44:2C:input#CH1' triggered
or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH2' triggered
or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH3' triggered
or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH4' triggered 

Where the possible states are

RELEASED
PRESSED
LONG_PRESSED

The only thing I can think of so far is to create a blanket DSL rule that maps the state of these triggers to Virtual Items in openHAB2.

Using this thread as inspiration… - Passing receivedEvent and triggeringItem as parameters to a lambda: which type to use? - #3 by shutterfreak

I created this rule, which I might be able to use to map events to virtual items

rule "Button Event mapper"
when
	   Channel 'velbus:vmbgpod:d212bf44:2C:input#CH1' triggered
	or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH2' triggered
	or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH3' triggered
	or Channel 'velbus:vmbgpod:d212bf44:2C:input#CH4' triggered
then


//		receivedEvent.channel.toString // Full 'channel' that triggered the event
//		receivedEvent.channel.thingUID.toString // Thing ID portion of channel (everything before the last ':')
//		receivedEvent.channel.id.toString // ID portion of channel (everything after the last ':')

	logInfo("Button Event","This button "+receivedEvent.channel.toString.replace(":","_").replace("#","_")+" -- Had this Event "+receivedEvent.getEvent())

    sendCommand(receivedEvent.channel.toString.replace(":","_").replace("#","_"),receivedEvent.getEvent())

end

You can just use a function node to filter for the event connected to the Openhab events node. Just connect a debug node that shows the whole message object to the Openhab-events node and trigger your event. The events node passes all openhab events so be aware there will be alot happening. You will see what your event is called in the nodered debug once you find it there and you can than write a simple if function in a function node to only pass this payload and even map different outputs of the function node to different openhab events. No need for any DSL rules.

2 Likes

Okay…

I can’t pretend that I totally understand all that, but I’ll give it a try :slight_smile:

I think it will be the syntax of the Function node that will stop me :sob:

1 Like

If you have any questions just ask, I used this approach before I moved to Mqtt for connecting openhab and nodered and it worked quite good.

Thanks, that’s very kind of you.

Could you give me an example of the If statement syntax please?

Cheers,

Stuart

I’ve managed to capture this event data

 smarthome/channels/velbus:vmbgpod:d212bf44:2C:input#CH13/triggered : msg.payload : Object

{ event: "PRESSED", channel: "velbus:vmbgpod:d212bf44:2C:inp…" }

09/04/2019, 19:14:39[node: 55d18aa8.2a3b24](http://192.168.178.27:1880/#)smarthome/channels/velbus:vmbgpod:d212bf44:2C:thermostat#ALARM1/triggered : msg.payload : Object

{ event: "RELEASED", channel: "velbus:vmbgpod:d212bf44:2C:inp…" }

and

{"event":"RELEASED","channel":"velbus:vmbgp4pir:d212bf44:32:input#CH6"}

I think I need to filter to see only the channels I want, and pass the Event onwards

I did! But this setup is at work :smiley: and I think I did not use Node-RED but normal rules.

I can post the rule tomorrow.

1 Like

Hi

Okay, please don’t worry.

I can easily get all the Channel Triggers in DSL rules, what I’m trying to do is move some functionality to NodeRed.

Thanks for the offer :smile:

this in a function node only passes messages for a specific event:

var channel = msg.payload.channel;
var event = msg.payload.event;
if (channel === "triggerChannelyourlookingfor" && event === "eventyourlookingfor") {
    return msg;
}
return null;

Just insert the event and the channel you see in the debug in the if part.

1 Like

Oh I see.

I can do something with that :smile:

I read that it’s possible to have multiple outputs.

So does that mean multiple if statement could filter a single channel to each output?

1 Like

Just adapt for as many outputs as you want, you have to choose how many below the text input of the function node, for this example 2. When the first if is true it returns the payload on output 1 and if the second one is true on number 2.

var channel = msg.payload.channel;
var event = msg.payload.event;
if (channel === "triggerChannelyourlookingfor1" && event === "eventyourlookingfor1") {
    return [msg, null];
}
if (channel === "triggerChannelyourlookingfor2" && event === "eventyourlookingfor2") {
    return [null, msg];
}
return null;
1 Like

That’s truly fabulous :slight_smile:

Thank you so much :slight_smile:

I’ll post my working statement for others to work from.

Cheers
Stuart

UPDATE

So the && (AND Condition) in the IF statements didn’t work for me, but with a switch node after the IF Function, I can see the three different states from the Velbus buttons.

The premis is that there are Virtual switches in openHAB2 that can be set in the UI or Voice Commands etc

Each Scene switches OFF the other scene switches and set out ITEM states.

There are Feedback LEDs that can have their state changed.

Here is the Flow code for anyone that is interested.
(I’ve removed the actual items and openHAB controller, so you’ll need to tweak each node to suit your installation)

Please feel free to advise / revise this flow, as this is my First venture into NodeRed

[
    {
        "id": "3bd26249.fa856e",
        "type": "tab",
        "label": "Velbus & openHAB scenes",
        "disabled": false,
        "info": ""
    },
    {
        "id": "d4171d96.09e3e",
        "type": "openhab2-monitor",
        "z": "3bd26249.fa856e",
        "name": "",
        "controller": "",
        "x": 95,
        "y": 227,
        "wires": [
            [
                "519f6dbd.de7df4",
                "b03b8858.d72388",
                "6dc830ab.a3108",
                "a5f10519.bd8088"
            ],
            [],
            []
        ]
    },
    {
        "id": "6a30bf0c.813d4",
        "type": "openhab2-in",
        "z": "3bd26249.fa856e",
        "name": "Scene 1 V Switch",
        "controller": "",
        "itemname": "",
        "x": 100,
        "y": 840,
        "wires": [
            [
                "e8a7483a.e1d818"
            ],
            []
        ]
    },
    {
        "id": "e04e1b51.932588",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 2 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 581,
        "y": 545,
        "wires": [
            []
        ]
    },
    {
        "id": "b8b1baa2.51c0d8",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 3 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 581,
        "y": 585,
        "wires": [
            []
        ]
    },
    {
        "id": "e8a7483a.e1d818",
        "type": "switch",
        "z": "3bd26249.fa856e",
        "name": "ON / OFF Switch",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 840,
        "wires": [
            [
                "e04e1b51.932588",
                "b8b1baa2.51c0d8",
                "cecb8f93.c7612",
                "482b6f0f.0a833",
                "2de6dc8.bbf8824",
                "b468b32c.46814",
                "cec7acb3.fb163",
                "876c9cf0.a3cb6"
            ],
            [
                "9f43f427.be4d68"
            ]
        ],
        "outputLabels": [
            "ON",
            "OFF"
        ]
    },
    {
        "id": "cecb8f93.c7612",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 4 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 581,
        "y": 625,
        "wires": [
            []
        ]
    },
    {
        "id": "482b6f0f.0a833",
        "type": "delay",
        "z": "3bd26249.fa856e",
        "name": "",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 551,
        "y": 665,
        "wires": [
            [
                "72336934.5c1e48"
            ]
        ],
        "outputLabels": [
            "Delayed event"
        ]
    },
    {
        "id": "2de6dc8.bbf8824",
        "type": "delay",
        "z": "3bd26249.fa856e",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 541,
        "y": 705,
        "wires": [
            [
                "4a438d0c.306bc4"
            ]
        ]
    },
    {
        "id": "b468b32c.46814",
        "type": "delay",
        "z": "3bd26249.fa856e",
        "name": "",
        "pauseType": "delay",
        "timeout": "15",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 541,
        "y": 745,
        "wires": [
            [
                "4e5b7b05.125154"
            ]
        ]
    },
    {
        "id": "cec7acb3.fb163",
        "type": "delay",
        "z": "3bd26249.fa856e",
        "name": "",
        "pauseType": "delay",
        "timeout": "25",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 541,
        "y": 785,
        "wires": [
            [
                "9a098269.04ed2"
            ]
        ]
    },
    {
        "id": "72336934.5c1e48",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Dimmer 1",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "25",
        "x": 800,
        "y": 660,
        "wires": [
            []
        ]
    },
    {
        "id": "4a438d0c.306bc4",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Dimmer 2",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "50",
        "x": 800,
        "y": 700,
        "wires": [
            []
        ]
    },
    {
        "id": "4e5b7b05.125154",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Dimmer 3",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "75",
        "x": 800,
        "y": 740,
        "wires": [
            []
        ]
    },
    {
        "id": "9a098269.04ed2",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Dimmer 4",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "100",
        "x": 800,
        "y": 780,
        "wires": [
            []
        ]
    },
    {
        "id": "876c9cf0.a3cb6",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 1 LED ON",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "SET_LED",
        "x": 561,
        "y": 465,
        "wires": [
            []
        ]
    },
    {
        "id": "9f43f427.be4d68",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 1 LED OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "CLEAR_LED",
        "x": 570,
        "y": 880,
        "wires": [
            []
        ]
    },
    {
        "id": "519f6dbd.de7df4",
        "type": "function",
        "z": "3bd26249.fa856e",
        "name": "Velbus Scene Button 1",
        "func": "// var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH1\";\n\nif (channel === button) {\n    return [msg, null];\n}\n\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "x": 325,
        "y": 127,
        "wires": [
            [
                "843327ca.9cbda8"
            ]
        ]
    },
    {
        "id": "843327ca.9cbda8",
        "type": "switch",
        "z": "3bd26249.fa856e",
        "name": "Button Status",
        "property": "payload.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "RELEASED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "LONG_PRESSED",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 585,
        "y": 127,
        "wires": [
            [
                "f25b4ad3.e2f568"
            ],
            [
                "cfbf49f9.4fd118",
                "9827ea7b.5953f8",
                "a83f4652.7c5a58",
                "3a87e605.a805ea"
            ],
            []
        ],
        "outputLabels": [
            "Pressed",
            "Long Pressed",
            "Released"
        ]
    },
    {
        "id": "f25b4ad3.e2f568",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 1 V Switch ON",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "ON",
        "x": 825,
        "y": 87,
        "wires": [
            []
        ]
    },
    {
        "id": "59a2c940.9a26b8",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 2 V Switch ON",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "ON",
        "x": 825,
        "y": 187,
        "wires": [
            []
        ]
    },
    {
        "id": "c5daa0e7.8179a",
        "type": "switch",
        "z": "3bd26249.fa856e",
        "name": "Button Status",
        "property": "payload.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "RELEASED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "LONG_PRESSED",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 585,
        "y": 187,
        "wires": [
            [
                "59a2c940.9a26b8"
            ],
            [],
            []
        ],
        "outputLabels": [
            "Pressed",
            "Long Pressed",
            "Released"
        ]
    },
    {
        "id": "b03b8858.d72388",
        "type": "function",
        "z": "3bd26249.fa856e",
        "name": "Velbus Scene Button 2",
        "func": "// var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH2\";\n\nif (channel === button) {\n    return [msg, null];\n}\n\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "x": 325,
        "y": 187,
        "wires": [
            [
                "c5daa0e7.8179a"
            ]
        ]
    },
    {
        "id": "54a238dc.c100b8",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 3 V Switch ON",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "ON",
        "x": 825,
        "y": 247,
        "wires": [
            []
        ]
    },
    {
        "id": "6ce27812.ac3c48",
        "type": "switch",
        "z": "3bd26249.fa856e",
        "name": "Button Status",
        "property": "payload.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "RELEASED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "LONG_PRESSED",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 585,
        "y": 247,
        "wires": [
            [
                "54a238dc.c100b8"
            ],
            [],
            []
        ],
        "outputLabels": [
            "Pressed",
            "Long Pressed",
            "Released"
        ]
    },
    {
        "id": "6dc830ab.a3108",
        "type": "function",
        "z": "3bd26249.fa856e",
        "name": "Velbus Scene Button 3",
        "func": "// var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH3\";\n\nif (channel === button) {\n    return [msg, null];\n}\n\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "x": 325,
        "y": 247,
        "wires": [
            [
                "6ce27812.ac3c48"
            ]
        ]
    },
    {
        "id": "29febe0.008da42",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 4 V Switch ON",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "ON",
        "x": 825,
        "y": 307,
        "wires": [
            []
        ]
    },
    {
        "id": "2bdf42a2.b8873e",
        "type": "switch",
        "z": "3bd26249.fa856e",
        "name": "Button Status",
        "property": "payload.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PRESSED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "RELEASED",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "LONG_PRESSED",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 585,
        "y": 307,
        "wires": [
            [
                "29febe0.008da42"
            ],
            [],
            []
        ],
        "outputLabels": [
            "Pressed",
            "Long Pressed",
            "Released"
        ]
    },
    {
        "id": "a5f10519.bd8088",
        "type": "function",
        "z": "3bd26249.fa856e",
        "name": "Velbus Scene Button 4",
        "func": "// var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH4\";\n\nif (channel === button) {\n    return [msg, null];\n}\n\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "x": 325,
        "y": 307,
        "wires": [
            [
                "2bdf42a2.b8873e"
            ]
        ]
    },
    {
        "id": "cfbf49f9.4fd118",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 1 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 1085,
        "y": 107,
        "wires": [
            []
        ]
    },
    {
        "id": "9827ea7b.5953f8",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 2 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 1085,
        "y": 147,
        "wires": [
            []
        ]
    },
    {
        "id": "a83f4652.7c5a58",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 3 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 1085,
        "y": 187,
        "wires": [
            []
        ]
    },
    {
        "id": "3a87e605.a805ea",
        "type": "openhab2-out",
        "z": "3bd26249.fa856e",
        "name": "Scene 4 V Switch OFF",
        "controller": "",
        "itemname": "",
        "topic": "ItemCommand",
        "payload": "OFF",
        "x": 1085,
        "y": 227,
        "wires": [
            []
        ]
    },
    {
        "id": "45e52b76.6b0284",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "ALL openHAB events",
        "info": "This node provides ALL of the bus events within openHAB\"",
        "x": 100,
        "y": 40,
        "wires": []
    },
    {
        "id": "a5dbe336.659e7",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Filter Velbus  Events",
        "info": "These IF statements filter specific Velbus button events",
        "x": 310,
        "y": 40,
        "wires": []
    },
    {
        "id": "49f8431e.bdf8ec",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Filter Button states",
        "info": "Divides up the button status' into\n\nPressed\nLong_Pressed\nReleased",
        "x": 590,
        "y": 40,
        "wires": []
    },
    {
        "id": "8aa80ec6.488f5",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Actions on PRESSED events",
        "info": "",
        "x": 820,
        "y": 40,
        "wires": []
    },
    {
        "id": "f511e83f.d12d48",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Actions on Long Pressed Events",
        "info": "",
        "x": 1090,
        "y": 40,
        "wires": []
    },
    {
        "id": "3e53dcb3.809a14",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Scene ON events",
        "info": "",
        "x": 560,
        "y": 420,
        "wires": []
    },
    {
        "id": "ed0e1118.8dad",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Scene OFF events",
        "info": "",
        "x": 570,
        "y": 840,
        "wires": []
    },
    {
        "id": "3d8e650a.9621ea",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Delayed ON events",
        "info": "",
        "x": 810,
        "y": 620,
        "wires": []
    },
    {
        "id": "e37a475f.3e7ba8",
        "type": "comment",
        "z": "3bd26249.fa856e",
        "name": "Seperate ON and OFF states",
        "info": "",
        "x": 320,
        "y": 780,
        "wires": []
    }
]

For anyone that is interested, here is my “working out” :smile:

[
    {
        "id": "c1c183f4.eb661",
        "type": "openhab2-events",
        "z": "aa997a33.1a22b8",
        "name": "",
        "controller": "4a048897.2cd4b8",
        "itemname": "",
        "x": 120,
        "y": 1180,
        "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": 400,
        "y": 1340,
        "wires": []
    },
    {
        "id": "83e9bf19.58393",
        "type": "function",
        "z": "aa997a33.1a22b8",
        "name": "Filter Button",
        "func": "var event = msg.payload.event;\nvar channel = msg.payload.channel;\nvar button = \"velbus:vmbgp2:c5053467:24:input#CH3\";\nvar newMsg = { payload: msg.payload.event };\n\nif (channel === button) {\n    return [newMsg];\n}\n\nif (channel === button && event === \"PRESSED\") {\n    return [newMsg];\n}\n\nif (channel === button && event === \"LONG_PRESSED\") {\n    return [newMsg];\n}\n\nif (channel === button && event === \"RELEASED\") {\n    return [newMsg];\n}\nreturn null;",
        "outputs": 4,
        "noerr": 0,
        "x": 410,
        "y": 1200,
        "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": 790,
        "y": 1180,
        "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": 590,
        "y": 1300,
        "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": 820,
        "y": 1240,
        "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": 800,
        "y": 1300,
        "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": 820,
        "y": 1380,
        "wires": [
            [
                "aaf5edf3.a83a5"
            ]
        ]
    },
    {
        "id": "97ff3822.52ccf8",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 1130,
        "y": 1240,
        "wires": []
    },
    {
        "id": "7db22549.5acb4c",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 1130,
        "y": 1320,
        "wires": []
    },
    {
        "id": "aaf5edf3.a83a5",
        "type": "debug",
        "z": "aa997a33.1a22b8",
        "name": "",
        "active": true,
        "tosidebar": false,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "x": 1130,
        "y": 1400,
        "wires": []
    },
    {
        "id": "4a048897.2cd4b8",
        "type": "openhab2-controller",
        "z": "",
        "name": "openHAB2 controller",
        "protocol": "http",
        "host": "127.0.0.1",
        "port": "8080",
        "path": "",
        "username": "",
        "password": ""
    }
]

My Rule

rule "Xiaomi Switch"

when
	Channel "mihome:sensor_switch:158d00014a0770:button" triggered

then
	var actionName = receivedEvent.getEvent()
	switch(actionName) {
	    case "SHORT_PRESSED": {
	    if (v_Switch_Outdoor_Xiaomi_VirtualSwitch.state==ON) {
		    sendCommand(a_Licht_Outdoor_Sonoff4Channel_1_Fahrradstaender, OFF)
	            sendCommand(a_Licht_Outdoor_Sonoff4Channel_2_Biertisch, OFF)
	            sendCommand(a_Licht_Outdoor_Sonoff4Channel_3_Rauchertisch, OFF)
		    sendCommand(v_Switch_Outdoor_Xiaomi_VirtualSwitch, OFF)
		}
		else {
		    sendCommand(a_Licht_Outdoor_Sonoff4Channel_1_Fahrradstaender, ON)
	            sendCommand(a_Licht_Outdoor_Sonoff4Channel_2_Biertisch, ON)
	            sendCommand(a_Licht_Outdoor_Sonoff4Channel_3_Rauchertisch, ON)
		    sendCommand(v_Switch_Outdoor_Xiaomi_VirtualSwitch, ON)
		}
	    }
	    
	    case "DOUBLE_PRESSED": {
	    //v_Switch_Outdoor_Xiaomi_Ruleoverwrite
	    if (v_Switch_Outdoor_Xiaomi_VirtualSwitch.state==ON) {
	       sendCommand(a_Licht_Outdoor_Sonoff4Channel_1_Fahrradstaender, OFF)
	       sendCommand(a_Licht_Outdoor_Sonoff4Channel_2_Biertisch, OFF)
               sendCommand(a_Licht_Outdoor_Sonoff4Channel_3_Rauchertisch, OFF)
	       sendCommand(a_Licht_Outdoor_SonoffBasic_Grilllampe, OFF)
	       sendCommand(v_Switch_Outdoor_Xiaomi_VirtualSwitch, OFF)
	       sendCommand(v_Switch_Outdoor_Xiaomi_Ruleoverwrite, OFF)
	    }
	    else {
	    	 sendCommand(a_Licht_Outdoor_Sonoff4Channel_1_Fahrradstaender, ON)
	         sendCommand(a_Licht_Outdoor_Sonoff4Channel_2_Biertisch, ON)
	         sendCommand(a_Licht_Outdoor_Sonoff4Channel_3_Rauchertisch, ON)
	         sendCommand(a_Licht_Outdoor_SonoffBasic_Grilllampe, ON)
		 sendCommand(v_Switch_Outdoor_Xiaomi_VirtualSwitch, ON)
		 sendCommand(v_Switch_Outdoor_Xiaomi_Ruleoverwrite, ON)
	    	 }
	   }

	    
	    case "LONG_PRESSED": {
	    	 sendCommand(a_Licht_Outdoor_Sonoff4Channel_1_Fahrradstaender, OFF)
	       	 sendCommand(a_Licht_Outdoor_Sonoff4Channel_2_Biertisch, OFF)
               	 sendCommand(a_Licht_Outdoor_Sonoff4Channel_3_Rauchertisch, OFF)
	         sendCommand(a_Licht_Outdoor_SonoffBasic_Grilllampe, OFF)
	         sendCommand(v_Switch_Outdoor_Xiaomi_VirtualSwitch, OFF)
	       	 sendCommand(v_Switch_Outdoor_Xiaomi_Ruleoverwrite, OFF)
	    }
	    
//	    case "LONG_RELEASED": {
//	    }
	    
	}

That’s a full rule to toggle all outdoor Sonoffs that control lights to turn ON/OFF when a Xiamio is pressed, double pressed, long press or long release.

May approach would be mostly the same, assign a “virtual” item like

v_Switch_Outdoor_Xiaomi_VirtualSwitch_shortpress
v_Switch_Outdoor_Xiaomi_VirtualSwitch_doublepress
v_Switch_Outdoor_Xiaomi_VirtualSwitch_longpress
v_Switch_Outdoor_Xiaomi_VirtualSwitch_longrelease

and use these “virtual” items in Node-RED

1 Like

Can you show me your your function node? Just tested it in my setup. I have a 4way RockerSwitch with 1 Channel per side and this function node returns one of the possible events on each output no problem.

var channel = msg.payload.channel;
var event = msg.payload.event;
if (channel === "enocean:rockerSwitch:FT2VDC6O:FEF9093C:rockerswitchA" && event === "DIR1_PRESSED") {
    return [msg, null, null, null];
}
if (channel === "enocean:rockerSwitch:FT2VDC6O:FEF9093C:rockerswitchA" && event === "DIR2_PRESSED") {
    return [null, msg, null, null];
}
if (channel === "enocean:rockerSwitch:FT2VDC6O:FEF9093C:rockerswitchB" && event === "DIR1_PRESSED") {
    return [null, null, msg, null];
}
if (channel === "enocean:rockerSwitch:FT2VDC6O:FEF9093C:rockerswitchB" && event === "DIR2_PRESSED") {
    return [null, null, null, msg];
}
return null;

you can see the the events beeing sorted to each output, for example here the highlighted event came rightly through output number 3


here is the function node

Hi

Thanks for the help :slight_smile:

This is the text I have in the function I’m playing around with.

var event = msg.payload.event;
var channel = msg.payload.channel;
var button = "velbus:vmbgp2:c5053467:24:input#CH3";

var newMsg = { payload: msg.payload.event };

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


if (channel === button && event === "PRESSED") {

return [newMsg];
}

If (channel === button && event === "LONG_PRESSED") {

return [newMsg];
}

if (channel === button && event === "RELEASED") {
 return [newMsg];
}

return null;

And these are definately the events I’m trying to match

{ event: "PRESSED", channel: "velbus:vmbgp2:c5053467:24:input#CH3" }

{ event: "LONG_PRESSED", channel: "velbus:vmbgp2:c5053467:24:input#CH3" }


{ event: "RELEASED", channel: "velbus:vmbgp2:c5053467:24:input#CH3" }

Comparing to the IF statement you’ve posted, I seem to be missing a bunch of null

Would this be better?

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

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

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

If (channel === button && event === "LONG_PRESSED") { return [null, null, newMsg, null];
}

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

So much info here . Just adding tutorial on Telegram to the mix. Do hope it helps others.

1 Like

Try it like this. If I get you right you dont actually need four but only three outputs on your function node. One for each case. Another question is do actually need the released event even? Else eve 2 would be enough.

var event = msg.payload.event;
var channel = msg.payload.channel;
if  (channel === "velbus:vmbgp2:c5053467:24:input#CH3")  {
	if (event === "PRESSED") {
		return [msg, null, null];
	}
	if (event === "LONG_PRESSED") {
		return [null, msg, null];
	}
	if (event === "RELEASED") {
		return [null, null, msg];
	}
}
return null;

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

Sorry for stupid question, what is the difference in nodes:
node-red-contrib-openhab2 & node-red-contrib-openhab-v2