Looking for an Scripts that detects Single, Double or Triple Click of an Switch

I am looking for a script that evaluates a single, double or triple click of a switch.
I prefere Javscript ECMAScript-2021.

The background is that I want to trigger the following actions:

  • single click : switch on all lights
  • double click : switch on all lights and start playing favorite music of my wife
  • triple click : switch on all lights and start playing my favorite music

The actions themselves are not my problem, but how do I distinguish between single, double and triple click.

What type of switch? I have zigbee switches that can react differently to up to 5 clicks, hold and release

I have ABB/Busch-Jäger Free@Home switches.

I am interested in a hardware independent Solution, that works with an Item, that recieves the commands.

The Tutorials and Solutions category is only for posting your own code and demonstrations, not for requesting assistance. I’ve moved your post to a better category this time, please try remember for next time.

1 Like

@ML1982
I found this
https://community.openhab.org/t/control-of-a-set-of-switches-by-special-use-of-one-switch/53765/9?u=tougharms
(not my work, obviously😄). It looks like rules DSL but can you re-write it in javascript?

@Tougharms here is my first approach, these are my first experiences with timers, it seems to work.
I’m still testing. Any suggestions for improvement?

'use strict';
{
  const runtime = require('@runtime'); //we need this to import Datatypes like PercentType
  var myTimer; //it works with "var" andit doesn't work with "let", must be something about hoisting
  
  function evaluate() 
  {
    console.warn("Counted: " + cache.private.get("CachedCounter"));
    cache.private.put("CachedCounter", 0);
    myTimer = null;    
  }  
    
  console.warn("Start Timer Test Script");
  
  let MyCounter = cache.private.get("CachedCounter");
  if( MyCounter == null) //if counter doesn't exists, create cached counter 
  {
    MyCounter = {times: 0};
    cache.private.put("CachedCounter", MyCounter);
  }
  
  console.warn("Timer Info " + myTimer);
  if(myTimer == null) // no timer started, so startup timer
  {
      cache.private.put("CachedCounter", 1);
      myTimer = setTimeout(evaluate, 10000); 
      console.warn("Timer started");
  }
  else
  {
      const NewCounterValue =  cache.private.get("CachedCounter") + 1;  
      cache.private.put("CachedCounter", NewCounterValue);
      console.warn("new Counter value: " + NewCounterValue);
  }
   
   
  console.warn("Finished Timer Test Script");
}  

You are more advanced than me. I dont understand any of that!:flushed:

I’ve just made some Updates:

'use strict';
{
  const timeout = 10000;
  const runtime = require('@runtime'); //we need this to import Datatypes like PercentType
  var myTimer; //it works with "var" andit doesn't work with "let", must be something about hoisting
  var myCounter; //it works with "var" andit doesn't work with "let", must be something about hoisting
  
  function evaluate() 
  {
    console.warn("Counted: " +  myCounter + " within a time period of " + timeout + "ms"); 
    myCounter = 0; //reset the counter
    clearTimeout(myTimer); 
    myTimer   = null;      
  }  
    
  console.warn("Start Timer Test Script");
  console.warn("Timer id: " + myTimer);
  if(myTimer == null) // no timer started, so startup timer
  {
      myCounter = 0;
      myTimer   = setTimeout(evaluate, timeout); 
      console.warn("Timer started");
  }
  else
  {
     myCounter = myCounter  + 1;
     console.warn("new Counter value: " + myCounter);
    //here I can catch the "tripple Click immidiatly"    
  }   
   
  console.warn("Finished Timer Test Script");
}  
1 Like