logo elektroda
logo elektroda
X
logo elektroda

OpenBeken Command use case -a 'menu' using one button and one LED for 5 relays

btsimonh  15 3666 Cool? (+2)
📢 Listen (AI):
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:

addEventHandler OnClick 14 publish button click
addEventHandler OnDblClick 14 publish button dblclick


Then, in NodeRed, subscribe to
<device>/#
to receive the MQTT which will be
<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:

NodeRed flow with nodes for handling buttons and LED.

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
Log in, to see the code

About Author
btsimonh wrote 66 posts with rating 14 , helped 1 times. Been with us since 2022 year.

Comments

ferbulous 09 Oct 2022 09:09

Hi, thanks for the instructions with node red. Is it possible to configure the actions for the buttons press that triggers the led directly on the device similarly to tasmota rules? [Read more]

btsimonh 09 Oct 2022 11:23

I don't think so at the moment, although it may be fun to try!. I was thinking that we could use unused channel values as variables. One thing I'm not sure about is if the 'regular' commands can be sub-second... [Read more]

p.kaczmarek2 09 Oct 2022 16:56

Very nice mechanism, @btsimonh . @ferbulous It would indeed not be possible just in OpenBeken in the current state of things, but my scripting addon is almost ready and will be released soon. It will... [Read more]

ferbulous 25 Oct 2022 15:36

@btsimonh hi, I wanna use this for custom action for lights thru nodered (eg single press = toggle, double press = change temp, long = dim) How can I set the relay wired to the wifi bulbs as always on?... [Read more]

p.kaczmarek2 25 Oct 2022 17:08

Hello @ferbulous , the OUTPUT HIGH is a good idea so I have added it for you: https://github.com/openshwprojects/OpenBK7231T_App/commit/b62d45e7d29246a9b4714ce1208d750915d0ed7e NOTE: Please test, but... [Read more]

ferbulous 25 Oct 2022 17:28

Thanks for adding this Following @btsimonh instructions , i assigned different button number for the pin so it doesn't trigger the relay and sends mqtt command Device is 4 gang button switch: Button... [Read more]

p.kaczmarek2 25 Oct 2022 19:14

@ferbulous unless I'm much mistaken, you want to have a fallback behaviour in case that MQTT is offline. You will need a conditional for that. I have a simple idea how I can handle it, please wait at most... [Read more]

p.kaczmarek2 26 Oct 2022 07:43

@ferbulous UPDATE - I'm working on it alias mybri backlog led_dimmer 100; led_enableAll alias mydrk backlog led_dimmer 10; led_enableAll if MQTTOn then mybri else mydrk You will be able to... [Read more]

ferbulous 26 Oct 2022 14:56

Noted, thanks for the example. I can think of 2 approach for this 'detached' mode 1) P7 - OUTPUT HIGH P16 - Button 2 Using OUTPUT HIGH for P7, how can i set in the command for P16 (Button 2) to... [Read more]

p.kaczmarek2 26 Oct 2022 15:19

Wait, editing. EDIT: From a brief glance at things, it seems that you really could use a "MQTT Connected" and "MQTT Disconnected" events. They are not present in the code yet, but I will look into it. That's... [Read more]

ferbulous 26 Oct 2022 17:02

I see, for option (1) & (2) would require a script to change the pin assignment when it's offline. Is there list of actions available in the command other than led_dimmer? i haven't explored much... [Read more]

ferbulous 27 Oct 2022 19:17

@pkaczmarek2 I think I've got it working now for the detached mode & fallback to normal relay when homeassistant/mqtt offline backlog addEventHandler OnClick 16 publish button click; addEventHandler... [Read more]

p.kaczmarek2 28 Oct 2022 06:25

Wow, this is very good job, @ferbulous ! I am happy that you managed to get it running. Is there anything else I can help you with? (I remember that I still have on N crash report from you pending) Very... [Read more]

ferbulous 31 Oct 2022 14:54

I just tested with T device, and same crash issue also occurs it seems. I wonder if there's anything else I need to enable in the settings to prevent this crash. For now, I'm only controlling openbkt... [Read more]

p.kaczmarek2 01 Nov 2022 08:06

T device LWIP library has been updated today and will be merged soon. Please check later today or tomorrow. It would be good to know if it changes anything, but new LWIP library should handle even 20 MQTT... [Read more]

FAQ

TL;DR: OpenBeken can cycle pin roles ~100 000 times (≈27 years at 10 disconnects/day) without flash wear, "a very flexible conditional command is coming" [Elektroda, p.kaczmarek2, post #20253458] Use MQTTState events and Node-RED to build a single-button, single-LED menu for 5 relays.

Why it matters: You can create robust, local fall-back control for smart-relay strips even when MQTT or Wi-Fi dies.

Quick Facts

• Button events available: OnClick, OnDblClick, OnHold, OnToggle [Elektroda, btsimonh, post #20226671] • LED feedback tick rate: 200 ms, 14-tick cycle (2.8 s) [Elektroda, btsimonh, post #20226671] • Flash endurance: ≥100 000 erase/write cycles per sector [Elektroda, p.kaczmarek2, post #20256296] • New LWIP lets OpenBeken publish up to 20 MQTT msgs / s [Elektroda, p.kaczmarek2, post #20262258] • OUTPUT_HIGH role added 25 Oct 2022 for permanent-on relays [Elektroda, p.kaczmarek2, post #20252611]

How do I publish button clicks from pin 14 to MQTT?

Enter: 1. addEventHandler OnClick 14 publish button click 2. addEventHandler OnDblClick 14 publish button dblclick. Messages appear at /button/get with payload “click” or “dblclick” [Elektroda, btsimonh, post #20226671]

Can the LED feedback loop run inside OpenBeken only?

Not yet. “It would indeed not be possible just in OpenBeken in the current state” [Elektroda, p.kaczmarek2, post #20228427] Node-RED or future scripting add-on is required for multi-flash menus.

What is the simplest Node-RED flow for five-relay selection?

  1. Inject node sends empty tick every 200 ms. 2. MQTT-in node captures button and relay topics. 3. Function node (CalexButton) handles counters and publishes LED or relay commands [Elektroda, btsimonh, post #20226671]

Is rapid LED flashing over MQTT reliable?

Yes for human feedback. 200 ms ticks give usable flashes, though timing is “not perfect” due to network latency [Elektroda, btsimonh, post #20226671]

Three-step: create an always-on relay with local fallback

  1. backlog setPinRole 7 OUTPUT_HIGH; setPinRole 16 Button2. 2. addEventHandler MQTTState 0 setPinRole 7 Rel. 3. addEventHandler OnClick 16 toggleChannel 2. The relay stays on during cloud uptime and toggles locally when offline [Elektroda, ferbulous, post #20255713]
Generated by the language model.
%}