Configure openhab to read a long and short button press

I have flashed tasmota onto a wall switch and set it to output a mqtt state of either SHORT for a short press or LONG for a long press. However now I am stuck as how I can get openhab to toggle my patio light for a short press and toggle my security light for a long press.
I’ve had it working using one light and just ON OFF States.
If somebody could point me in the right direction any help would be greatly appreciated.

Use the search function and you will find several examples/solutions for your issue.

Example links to check:

Like a lot of things, go one step at a time.

Maybe begin with a “hello world” rule that responds to short press.
Be careful with stuff like button presses, not to confuse states and events.

When that works, get the rule to command stuff you want to happen.

Use that as a pattern to do a long press version.

Using the MQTT messages just for ON/OFF allowed to directly use a switch, now your device does send different messages on the topic relating to that device. As suggested by @rossko57 you would need the to trigger a rule when such a message is recieved and react according to the received content of the message.

So i am able to turn the light on with the following rule

rule "Toggle Patio Light"
    Item WallSwitchPatioSwitch1State received update
    if (WallSwitchPatioSwitch1State.state == (2)) {
    else {

Is it possible to be able to turn it off using the same Switch State of “2” so the light would toggle on and off by pressing the button

Such is possible, however you should react on commands only since state updates are sent frequently.

You might need to clarify what results you see in openHAB from this, how you’ve configured your Items, etc.
Does this WallSwitchPatioSwitch1State Item take up different states for long or short or press or unpress, or there is another Item, or?

Ive tried using SHORT AND LONG however openhab gave me an error, could only use ON OFF or 1 , 2 ect.

When pressing the button i recieve a MQTT payload of either SHORT or LONG however when i press a second time to turn off i recieve the same SHORT message

But we’ve absolutely no idea what you’re on about. An error? Using what where?

Okay, so is that related to some openHAB Item in some way? You need to share a bit more detail.

That’s fine, after all you did exactly the same thing (pressed the button).
It’s pretty easy to make a toggle rule that looks at the state of the “target” Item, and then chooses what to do to it based on its current state.
If the lamp is on, send it “off”, else send it “on”.

What i am trying to achieve is to be able to turn my patio lights on and off with a short press and turn my security lights on and off with a long press.


/* Wall SwitchPlates */
Switch   WallSwitchPatioRelay1    "Relay1"     {channel="mqtt:topic:c05e17db:Relay1"}  //Patio Lights
Switch   WallSwitchPatioSwitch1   "Switch 1"   {channel="mqtt:topic:c05e17db:Switch1"}

/* Security Lights */
Switch   SecurityLight_BackyardLeft          "Security Light Backyard Left"         <switch>            ["switchable"]      {channel="mqtt:topic:846b6b01:SecurityLight_BackyardLeft"}


rule "Toggle Patio Light"
    Item WallSwitch_Patio_Switch received update
    if (WallSwitch_Patio_Switch.state == (2)) {
    else {

this is as far as i have gotten, when i send a MQTT topic of

SwPatio/Press = SHORT

I get this error in the openHAB log

22:48:52.693 [WARN ] [hab.binding.mqtt.generic.ChannelState] - Command 'SHORT' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.SHORT

Sorry i am unfamilar with the correct termonolgy, and ive been doing a bit of searching and googling but in not sure what i should be searching for.
Thanks in advance

Okay, so this Item represents this your wall pushbutton ? It’s a guess.

We don’t know how your MQTT thing/channel is configured, so we don’t know how your Item works.
Maybe we don’t need to know how it’s configured … but it’s difficult to advise how to use your Item when we don’t know what happens to it when you press buttons.

Your chosen Item is a Switch type. They only work with ON/OFF.
It is possible to configure your MQTT channel to convert a payload like “SHORT” into on or off.
But let’s look ahead, with the limited info you’ve shared. Are you expecting payload LONG also on this topic? Does your switch tell you when it is un-pressed? What happens if you press short, and later press short again, will there be any state change for you to detect?

Depending how your wallswitch device works, I think you might be better off treating these incoming payloads as events not Item states.

Yes payload LONG will also be on this topic.
Switch does not change state when un pressed.
every time i press it quickly i sends a SHORT payload and every time i press and hold i get the LONG payload.

13:13:58 RUL: SWITCH1#STATE=2 performs "publish SwPatio/Press SHORT"
13:13:58 MQT: SwPatio/Press = SHORT
13:14:06 RUL: SWITCH1#STATE=3 performs "publish SwPatio/Press LONG"
13:14:06 MQT: SwPatio/Press = LONG
13:17:43 RUL: SWITCH1#STATE=2 performs "publish SwPatio/Press SHORT"
13:17:43 MQT: SwPatio/Press = SHORT
13:17:57 RUL: SWITCH1#STATE=2 performs "publish SwPatio/Press SHORT"
13:17:57 MQT: SwPatio/Press = SHORT

the switch is a tuya type wall plate that has been flashed with tasmota.

I have this turning the patio lights on and off however it dosent matter if i send a LONG or SHORT payload.

rule "light check"

	Item WallSwitchPatioSwitch1 received update
	if (WallSwitchPatioRelay1.state == OFF)
		sendCommand(WallSwitchPatioRelay1, ON)
	else if (WallSwitchPatioRelay1.state == ON) {
		sendCommand(WallSwitchPatioRelay1, OFF)

Then I strongly recommend you give up on the idea of linking the wallswitch actions to an OH Item state.
On the basis that your device is sending “events”, i.e. button was pressed, and not states, i.e. button is not pressed at the moment.

You can configure your MQTT channel as an trigger/event channel
See trigger option for channel.

That doesn’t get linked to any Item, because Items cannot represent transient events.
But you can trigger rules from event channel triggers - and look for different triggers, in your case SHORT or LONG.

If that all feels weird, there is an alternate approach, which is a bit easier to grasp…
Link your MQTT channel to a String type Item -now it can take states/commands “LONG” etc.
Use the MQTT channel postCommand option to convert incoming payload into an openHAB command to the Item.
Now you can build rules listening for Item commands, rather like “events”.
(You can pretty much ignore the Item state, which has only historic value here)

@rossko57 Thankyou I have it working

rule "Patio Switch SHORT"

	Item WallSwitchPatioSwitch1SHORT received command 
	if (WallSwitchPatioRelay1.state == OFF)
		sendCommand(WallSwitchPatioRelay1, ON)
	else if (WallSwitchPatioRelay1.state == ON) {
		sendCommand(WallSwitchPatioRelay1, OFF)

rule "Patio Switch LONG"

	Item WallSwitchPatioSwitch1LONG received command 
	if (SecurityLight_BackyardLeft.state == OFF)
		sendCommand(SecurityLight_BackyardLeft, ON)
	else if (SecurityLight_BackyardLeft.state == ON) {
		sendCommand(SecurityLight_BackyardLeft, OFF)

This is what i endeed up with by linking MQTT to string and using post Command

If your LONG and SHORT truly arrive on the same topic, you can link to just one Item and distinguish the commands (remembering they are string commands)

	Item someItem received command "SHORT"

in what i want to achieve would it be better to see if i can have my switch device output different topics, for example
SwPatio/ShortPress = ON
SwPatio/LongPress = ON

Well … why? What advantage would that bring? It can only do one thing at a time.

i thought it would make config simpler

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.