Switch on for 1 second or 3 seconds => 2 different rules

Hi,

I have a physical switch at home and when I press it, in the log, I can see clearly his status going to ON and when I release the physical switch, I can see it going to OFF.

Now I would like to execute 1 action if I press the physical switch for 1 second and another action if I press it for 3 seconds.

How can I achieve this?

Thanks,

John.

rule x
when
		Item buttonX changed from OFF to ON
then
		Thread::sleep(2000)
		if(buttonX.state == ON) {...}
		else {...}
end

I think this sould work.

Hi,

Thanks for the answer. I don’t think this would work because if I wait 3 seconds, it should not do the think for 1 seconds.

With your proposal, it would then do the thing after one second then the one after 3 seconds so it would do both. I would like to do either the first action either the second action…

It’s a bit more complicated than that, but first you’ll have to clarify what you want to happen -
If button pressed less than one second (as most of us do)
If button pressed twice within 3 seconds
If the 1-sec action should be carried out whether or not the button is still being held down (for a possible 3 secs)

Some ideas here

1 Like

If you’re happy for the rule to execute when the switch turns off:

var long whenStarted

rule "Button Pressed"
when
  Item buttonX changed from OFF to ON
then
  whenStarted=now.millis
end

rule "Button Released"
when
  Item buttonX changed from ON to OFF
then
  val whenStopped = now.millis
  val timeTaken = whenStopped-whenStarted
   
  if (timeTaken < 3000) {
    // Do something
  } else {
    // Do something else
  }
end
3 Likes