Using commands with MQTT and NodeRed to operate a multi-relay unit from a single button
This example uses a combination of event handlers in OpenBeken to send button presses to NodeRed, and a NodeRed node to track the button presses and flash a single LED on the device to provide a simple 'menu' of operations on the device.
The intent is to flash an LED for the number of flashes representing how many times the button has been pressed as feedback to the user to know which relay they will toggle by a double press.
As the LED flashing is done over MQTT by NodeRed, the result is not perfect, but perfectly useable.
The device is a Calex power strip with 5 'relays' (4 power sockets and 1 controlled USB outlet). It has two LEDs and a single button.
The configuration is:
P6 Rel 5
P7 Rel 2
P8 Rel 3
P9 Rel 1
P10 LED 6
P14 Btn 8
P24 LED 7
P26 Rel 4
Note how I have separated the button on channel 8, so that it has no default action.
Commands to issue to OpenBeken:
Buttons provide the events OnClick, OnDblClick, OnToggle, OnHold - but these are published against PIN numbers.
In my case, my single button is on pin 14, and I want to publish the button presses to MQTT. Publishing to MQTT can be done with the command `publish <topic> <value>` which will result in `<value>` being sent to the topic `<devicename>/<topic>/get`.
I issued these commands to the OpenBeken command line:
Then, in NodeRed, subscribe to to receive the MQTT which will be
We also need to capture the current relay values, which look like
Once tested, the commands can be run at boot by adding this startup command line in `Change Startup Command Text`:
Controlling the LED
I chose to use the LED on channel 6 as the feedback to the 'menu' state. To control this over MQTT, we send
NodeRed flow
The simple flow in NodeRed looks like this:
The 200msTick inject sends an empty payload on topic 'tick' every 200ms to operate our LED flash logic.
Button presses are received and acted upon. Each time the OnClick event is received, a variable context.selected is incremented, mod 6 (so it takes values of 0-5). Each time a tick is received, a variable context.counter is incremented mod (7*2) (so range 0-13). If context.selected is non-zero and (context.selected < context.counter/2) the led is turned on if !(context.counter & 1), else the led is turned off. So, the led flashes for the count of context.selected, with a flash period of 2 x 200ms, and overall period of 14 x 200ms.
Relay set/get MQTT packets are used to know the current state, and if dblclick is received and context.selected != 0, then the relevant relay is toggled.
Content of CalexButton function node:
This example uses a combination of event handlers in OpenBeken to send button presses to NodeRed, and a NodeRed node to track the button presses and flash a single LED on the device to provide a simple 'menu' of operations on the device.
The intent is to flash an LED for the number of flashes representing how many times the button has been pressed as feedback to the user to know which relay they will toggle by a double press.
As the LED flashing is done over MQTT by NodeRed, the result is not perfect, but perfectly useable.
The device is a Calex power strip with 5 'relays' (4 power sockets and 1 controlled USB outlet). It has two LEDs and a single button.
The configuration is:
P6 Rel 5
P7 Rel 2
P8 Rel 3
P9 Rel 1
P10 LED 6
P14 Btn 8
P24 LED 7
P26 Rel 4
Note how I have separated the button on channel 8, so that it has no default action.
Commands to issue to OpenBeken:
Buttons provide the events OnClick, OnDblClick, OnToggle, OnHold - but these are published against PIN numbers.
In my case, my single button is on pin 14, and I want to publish the button presses to MQTT. Publishing to MQTT can be done with the command `publish <topic> <value>` which will result in `<value>` being sent to the topic `<devicename>/<topic>/get`.
I issued these commands to the OpenBeken command line:
addEventHandler OnClick 14 publish button click
addEventHandler OnDblClick 14 publish button dblclick
Then, in NodeRed, subscribe to
<device>/#
<device>/button/get click
<device>/button/get dblclick
We also need to capture the current relay values, which look like
<device>/<channel>/get <value 0|1>
Once tested, the commands can be run at boot by adding this startup command line in `Change Startup Command Text`:
backlog addEventHandler OnClick 14 publish button click; addEventHandler OnDblClick 14 publish button dblclick
Controlling the LED
I chose to use the LED on channel 6 as the feedback to the 'menu' state. To control this over MQTT, we send
<device>/6/set <value 0|1>
NodeRed flow
The simple flow in NodeRed looks like this:

The 200msTick inject sends an empty payload on topic 'tick' every 200ms to operate our LED flash logic.
Button presses are received and acted upon. Each time the OnClick event is received, a variable context.selected is incremented, mod 6 (so it takes values of 0-5). Each time a tick is received, a variable context.counter is incremented mod (7*2) (so range 0-13). If context.selected is non-zero and (context.selected < context.counter/2) the led is turned on if !(context.counter & 1), else the led is turned off. So, the led flashes for the count of context.selected, with a flash period of 2 x 200ms, and overall period of 14 x 200ms.
Relay set/get MQTT packets are used to know the current state, and if dblclick is received and context.selected != 0, then the relevant relay is toggled.
Content of CalexButton function node:
Code: Javascript
Cool? Ranking DIY