Short and long press differentiation

358 views
Skip to first unread message

derc...@googlemail.com

unread,
Jun 11, 2014, 4:59:15 PM6/11/14
to ope...@googlegroups.com
Hi folks,

I want to use something to distinguish between short and long presses of buttons. There are rules like:

rule "Schalter_EG_Bereitschaft"
when
    Item Schalter_EG_Bereitschaft changed
then
    if(Schalter_EG_Bereitschaft.state == 1)
    {
        timer = createTimer(now.plusSeconds(3))
        [|
            if (bereitschaft == false)
            {
                bereitschaft = true
                sendCommand(Lichter, OFF)
                sendCommand(Steckdosen, OFF)
                sendCommand(Hinweis_STANDBY, ON)
            }
            else
            {
                bereitschaft = false
                sendCommand(Strom_EG_WZ_HiFi, ON)
                sendCommand(Hinweis_STANDBY, OFF)
            }
        ]
    }
    else
    {
        if(timer!=null)
        {
            timer.cancel
            timer = null
        }
    }
end


Now I'd like to create a rule that does a different action on long and short press:

Example:
Press button for 1 second, excute rule 1, but keeping the button hold for >3seconds execute rule 2 without influencing rule 1.

Thanks for help,
Carsten
Message has been deleted

Baramykov Alexandr

unread,
Jun 14, 2014, 9:52:38 AM6/14/14
to ope...@googlegroups.com, derc...@googlemail.com
I am pretty new for openHAB, but maybe it can be done in these ways (unfortunately I don't have openHAB installed now, so it is just an idea, not tested).
1. Both short-click and long press reacting on button release and we manually counting how long it was pressed:

import java.util.Date

var long lastButtonPressTime

rule "short_long_press_v1"
when
  Item MyButton changed
then
  if ( MyButton.state == 1) {
    lastButtonPressTime = new Date().time
  } else {
    if (new Date().time < lastButtonPressTime + 3000 ) {
      // do short-click stuff
    } else {
     // do long-click stuff
    }
  }
end

2. Short-click react for button release but long-press react for timeout:

import org.openhab.model.script.actions.*

var Timer longPressTimer = null

rule "short_long_press_v2"
when
  Item MyButton changed
then
  if ( MyButton.state == 1) {
    longPressTimer = createTimer(now.plusSeconds(3)) [| /* long-press stuff */]
  } else {
    if (globalLihghtOffTimer != null) {
      var boolean cancelled = longPressTimer.cancel
      globalLihghtOffTimer == null
      if (cancelled) {
        // do short-click stuff
      }
    }
  }
end


P.S. Seems like GoogleGroups don't work good with Opera, so I don't know if my previous reply wasn't sent or this is just premoderation. I'm sending it in the second time, sorry if it will be double.



среда, 11 июня 2014 г., 23:59:15 UTC+3 пользователь derc...@googlemail.com написал:

Baramykov Alexandr

unread,
Jun 14, 2014, 9:57:10 AM6/14/14
to ope...@googlegroups.com, derc...@googlemail.com
Found a mistake, in the second example globalLihghtOffTimer should be replaced by longPressTimer.

derc...@googlemail.com

unread,
Jun 16, 2014, 4:08:33 PM6/16/14
to derc...@googlemail.com
Hey Alexandr,

thank you very much for your examples. I've implemented the second one cause I need immediate response if timer expires. And it works like a charm!

Some minor changes to make it more compact. The result looks like this:


import org.openhab.model.script.
actions.*


var Timer longPressTimer = null

rule "short_long_press_v2"
when
  Item MyButton changed
then
  if (MyButton.state == 1) {
    longPressTimer = createTimer(now.plusSeconds(3)
) [| /* long-press stuff */]
  } else {
    if (
longPressTimer != null) {
      if (
longPressTimer.cancel == true) {
         // do short-click stuff 
      } 
     
longPressTimer = null
    }
  }
end


Thanks again,
Carsten
Reply all
Reply to author
Forward
0 new messages