logo elektroda
logo elektroda
X
logo elektroda

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

btsimonh 
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 12 , 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]

%}