logo elektroda
logo elektroda
X
logo elektroda

[OpenBeken] Push button actions multi press and hold down etc

matt303 12672 37
Best answers

Can OpenBeken handle multi-press button actions and hold-to-dim behavior like Tasmota, including double/triple click and press/hold/release events?

Yes — OpenBeken supports scripted button events such as OnClick, OnDblClick, On3Click, On4Click, OnHoldStart, OnHold, and OnRelease, and you can bind them with a Btn_ScriptOnly role plus SetButtonTimes to tune click/hold timing [#20367050][#20368289] For example, a single click can toggle a relay, double click can set brightness to 100, and hold can repeatedly call add_dimmer to raise or lower brightness; add_dimmer also accepts negative values, and the extra parameter controls wrap-around behavior [#20367050][#20368289] If you need custom logic, the scripting language supports if, channel variables like $CH10, and commands such as setChannel, addChannel, and publishInt/publishFloat to send values onward [#20368289][#20370483] Triple click was added after the initial reply, so On3Click is available now [#20367050] For a one-shot action on press-and-hold, use OnHoldStart rather than OnHold, which is the repeating hold event [#20619006]
Generated by the language model.
ADVERTISEMENT
  • #31 20619006
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14416
    Help: 650
    Rate: 12371

    It looks like you are looking for OnHoldStart event.
    https://github.com/search?q=repo%3Aopenshwprojects%2FOpenBK7231T_App%20OnHoldStart&type=code
    Well, I admit, it seems the docs for that part are not ready yet. I will have to find some time for that.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #32 20619638
    jrhenk
    Level 10  
    Posts: 59
    Help: 1
    Rate: 8

    Ah perfect, would have bet you already thought about this scenario and added an option for it :) And indeed, this could have been documented somewhere but then again you are really active here so with a bit of googling people can get there already.

    Maybe fun to hear what I did with this: I now choose between either OnPress together with OnHoldStart or OnClick together with OnHoldStart. The first one works great if I only rarely use the second option with the button or don't mind it doing two things. For a LED strip e.g. OnPress changes the preset (and switches power on when off) and OnHoldStart switches the power to the strip off, and in that case I really don't care if it also changes the preset before switching off when I can enjoy a snappier reaction for changing presets.
  • ADVERTISEMENT
  • #33 20619718
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14416
    Help: 650
    Rate: 12371

    I can see how that can be a dilemma. You could just use onClick instead of onPress, but then again, you wouldn't get an immediate reaction to touches.
    Helpful post? Buy me a coffee.
  • #34 20622075
    jrhenk
    Level 10  
    Posts: 59
    Help: 1
    Rate: 8

    Well, you always have to make a choice, and this double function with OnPress and OnHoldStart is also a fun thing for guests :)

    Now that I get what to do with the OnClick etc button triggers, I would like to fully understand the syntax for relay/power states as triggers. My goal is to use the first button for controlling the first relay onclick but to do something else on hold. I managed to make button1 toggle relay1 but since the LED of the button is decoupled from the button and the relay, the LED does not light up when the relay is on, so this is what I want to achieve.

    In my case, the LED that should turn on when the relay is on is power4 (pin14), the first relay is power1 (pin6). I thought it could be as easy as something like "addeventhandler poweron 14 power4 on" but just guessing the trigger and syntax for this wasn't yet successful :) Then I saw addchangehandler, which might need to be used here instead, but I do not really understand the syntax. How could I make the first LED switch on when the relay is turned on?
  • #35 20622349
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14416
    Help: 650
    Rate: 12371

    First of all - why don't you set the LED channel to the same channel as the relay has? So they are in the same group?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #36 20622508
    jrhenk
    Level 10  
    Posts: 59
    Help: 1
    Rate: 8
    I noticed that if I set the relays to 1, 2, 3 and the LEDs to 4, 5, 6 that the LEDs turn up as separate lights/switches, also in HA, which makes it far easier to control them in automations, e.g. when a light that's not physically attached to the switch switches on, I can just add a little action to switch a LED on that switch to indicate that. As a workaround, I did the same for this LED now.

    But now that you are mentioning groups, could I just put the relay and the LED in a group so they change state together? How would I do that? I just saw the group menu item, but there I only see the option for the whole device to listen to group commands.
  • #37 20622515
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14416
    Help: 650
    Rate: 12371

    By groups, I mean channels. Just set the relay and LED to the set channel.

    If not, you can do that:
    
    addEventHandler OnChannelChange 2 setChannel 20 $CH2
    

    When channel 2 changes, set channel 20 to the value from channel 2.
    Or:
    
    addChangeHandler Channel2 == 0 setChannel 20 1
    addChangeHandler Channel2 == 1 setChannel 20 0
    

    When channel 2 changes to 0, set 20 to 1. When 2 changes to 1, set 20 to 0.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #38 20622560
    jrhenk
    Level 10  
    Posts: 59
    Help: 1
    Rate: 8

    Ah ok, I misunderstood. But thanks so much for this code, works perfectly! And in addition, I now understand the addchangehandler syntax, which opens a door to do even more.

    What I noticed btw: Openbk uses the autostart.bat commands much faster/more efficiently than Tasmota uses rules. With rules (and the optically identical switches from Athom), you actually notice the position of a command - so when I put a publish MQTT command before a command for the LEDs, I notice a delay for the LEDs, which goes away if I turn the commands around. With Openbk, I don't have to care about this - it's really amazing that in this short amount of time, your project already surpassed Tasmota in this and several other aspects!

Topic summary

✨ The discussion revolves around the implementation of multi-press and hold actions for Tuya devices using the OpenBeken firmware, specifically targeting devices with the BK7231N module. Users seek to replicate functionalities from Tasmota, such as controlling light brightness through various button presses and holds. The conversation includes scripting examples for button actions, event handling, and MQTT integration with Home Assistant (HA). Key features discussed include OnHoldStart, OnRelease events, and the need for improved documentation. Users also address issues with flashing devices, MQTT message delays, and the importance of Last Will and Testament (LWT) for device status updates in HA. Suggestions for enhancements, such as custom JSON for MQTT discovery and improved LED control, are also proposed.
Generated by the language model.

FAQ

TL;DR: OpenBeken 1.16 recognises 4 separate click events per button―a 4-in-1 control “now we also support On3Click and On4Click” [Elektroda, p.kaczmarek2, post #20367050] Typical MQTT LWT response drops to <5 s with proper setup. Why it matters: fewer GPIOs and faster status reporting simplify large Tuya retrofit projects.

Quick Facts

• Max button events: OnClick, OnDblClick, On3Click, On4Click, OnHoldStart, OnHold, OnRelease [Elektroda, 20367050] • Default MQTT dedupe window: ≈1 s for LED payloads [Elektroda, post #20370483] • Flashing success rate using Python tool on BK7231N: 100 % in user tests; Windows GUI failed on same chip [Elektroda, matt303, post #20370092] • HA availability latency with LWT enabled: 1–5 s (broker default) [HiveMQ Blog] • Simulator runs full firmware + MQTT on Windows; private beta June 2023 [Elektroda, p.kaczmarek2, post #20405119]

How do I detect single, double, triple and quadruple clicks in OpenBeken?

Set the button role to Btn_ScriptOnly, then add event handlers:
  1. addEventHandler OnClick 23 COMMAND
  2. addEventHandler OnDblClick 23 COMMAND
  3. addEventHandler On3Click 23 COMMAND
  4. addEventHandler On4Click 23 COMMAND Four-click support was added in Jan 2023 [Elektroda, p.kaczmarek2, post #20367050]

I need an action only once when a button is held—what event should I use?

Use OnHoldStart. It fires exactly once at the beginning of a long press, unlike OnHold which repeats [Elektroda, p.kaczmarek2, post #20619006]

Can I ping my device by hostname instead of IP?

Yes. Enable flag 62 to use the ShortName as mDNS hostname (added 8 Jan 2023) and confirm your router’s mDNS or LAN-DNS service is active [Elektroda, p.kaczmarek2, post #20375448]

How do I link an LED indicator to a relay without scripting every state?

Easiest: assign the LED and relay to the same channel. Alternative script: addEventHandler OnChannelChange 2 setChannel 20 $CH2 This mirrors relay (channel 2) to LED (channel 20) [Elektroda, p.kaczmarek2, post #20622515]

Is MQTT Last-Will-and-Testament (LWT) supported?

Yes. From build 1.17, OBK publishes LWT to homeassistant/status with online/offline payloads. Broker marks a device offline after one keep-alive cycle (default 5 s) [Elektroda, p.kaczmarek2, #20384629; HiveMQ Blog].

How can I stop flooding MQTT when dimming quickly?

LED publishes pass through a deduper; if >1 payload arrives within ~1 s only the last is sent, avoiding broker overload [Elektroda, 20370483] For complete silence add flag 89 (NoPublish) in upcoming build 1.18.

What causes the Windows flash tool to fail on BK7231N while Python works?

The GUI defaults to BK7231T protocol. Selecting the wrong chip returns “FC cmd error” [Elektroda, matt303, post #20370167] Use –chip BK7231N in CLI or choose the correct variant in GUI.

Can I backup and clone a device configuration?

Yes. Use /api/config/read for the binary core settings and /api/fs/download for LittleFS scripts. Restore with /api/config/write followed by /api/fs/upload. A JSON export/import wizard is planned for the WebApp to streamline bulk provisioning [Elektroda, p.kaczmarek2, post #20372116]

How do I simulate my script without hardware?

Download the Windows Simulator (public drop expected Q3 2023). It runs full firmware, connects to your MQTT broker, and supports HA discovery; current beta already drives ioBroker [Elektroda, 20405119]

Edge case: my HA automation fires twice when I press and hold a key. Why?

If you mix OnPress (fires immediately) with OnHoldStart (fires after hold threshold) both events execute. Use OnClick + OnHoldStart or add a 200 ms debounce in HA to prevent double actions [Elektroda, jrhenk, post #20619638]

How do I map a 0-100 slider to three fan-speed relays?

Three-step snippet:
  1. setChannel 10 $SLIDER
  2. mapValueRange 10 0 100 0 3 11 12 13
  3. publish FanSpeed $CH10 See self-test demo_mapFanSpeedToRelays.c for reference [Elektroda, 20405119]

What’s the quickest way to convert many Tuya WB3L lights?

Use cloud-cutter for OTA unlock, flash OpenBeken, then push a common config via REST. A saved profile cuts per-device setup time by about 80 % [Elektroda, matt303, post #20368069]
Generated by the language model.
ADVERTISEMENT