Hello,
its a little groovy script to switch another switch on/off when DoubleTap.
Coding with fonction again! Much better
your switch need to send its status quickly. Some switch send their status after 20-25 minutes, this will not work with this script.
double_tap.groovy
import org.openhab.core.jsr223.internal.shared.*
import org.openhab.core.items.ItemRegistry
import org.joda.time.DateTime
import org.openhab.core.library.types.*
import java.util.timer.*
//double tap on a switch will control another switch
class DoubleTapImpl implements Rule {
//delay to ignore commands when the script is sending a command
//and delay between tap in milliseconds
static tapDelay = 500
//var that take your configuration, see init in DoubleTapImpl()
static switchToControl = []
static ItemRegistry itemRegistry
static Class pe
def logger = Openhab.getLogger('DoubleTap')
DoubleTapImpl() {
logger.info('DoubleTap init')
//You need to add your light to control here!
//onVsOff: 'on' = only turn ON the other switch, 'off' = only turn OFF, 'all' = turn ON and OFF
//invertControl: true = double tap on switchB will control switchA also.
//do not change lastTap, timer and sending
//do not make multiple entry with the same light
switchToControl << [switchA:'Light_A',\
switchB:'Light_B', \
onVsOff:'all', \
invertControl:true, \
lastTap:new DateTime().minusMinutes(2), \
timer:null, \
sending:false ]
switchToControl << [switchA:'Light_Bed1', \
switchB:'Light_Bed2', \
onVsOff:'all', \
invertControl:false, \
lastTap:new DateTime().minusMinutes(2), \
timer:null, \
sending:false ]
}
java.util.List<EventTrigger> getEventTrigger() {
def myTrigger = []
switchToControl.each {
myTrigger << new UpdatedEventTrigger(it.switchA)
myTrigger << new UpdatedEventTrigger(it.switchB)
}
return myTrigger
}
void execute(Event event) {
def switchIndex = returnIndex(event.getItem().getName())
if(switchIndex!=null) {
if(switchToControl[switchIndex].sending == false) {
checkDelay(switchIndex,event)
}
}
}
def returnIndex(String theSwitch) {
def returnIndex = null
switchToControl.eachWithIndex { item, index ->
if(item.switchA == theSwitch || (item.invertControl == true && item.switchB == theSwitch)) {
returnIndex = index
}
}
return returnIndex
}
void checkDelay(def switchIndex, Event event) {
if(switchToControl[switchIndex].lastTap.plusMillis(tapDelay).afterNow) {
if(switchNeedCommand(switchIndex,event)) {
sendToSwitch(switchIndex,event)
}
}else{
switchToControl[switchIndex].lastTap = new DateTime()
}
}
boolean switchNeedCommand(def switchIndex, Event event) {
return switchToControl[switchIndex].onVsOff=='all' || \
switchToControl[switchIndex].onVsOff==convertState(event.getNewState())
}
void sendToSwitch(def switchIndex, Event event) {
switchToControl[switchIndex].sending = true
Openhab.sendCommand(getSwitch(switchIndex, event), event.getNewState())
removeSending(switchIndex)
}
def getSwitch(int index, Event event) {
if(switchToControl[index].switchA == event.getItem().getName()) {
return itemRegistry.getItem(switchToControl[index].switchB)
}else {
return itemRegistry.getItem(switchToControl[index].switchA)
}
}
String convertState(OnOffType status) {
return (status == OnOffType.ON) ? 'on' : 'off'
}
void removeSending(int index) {
switchToControl[index].timer = Openhab.createTimer(new DateTime().plusMillis(tapDelay),{
switchToControl[index].sending = false
} as TimerTask )
}
}
RuleSet getRules() {
return new RuleSet(new DoubleTapImpl())
}
DoubleTapImpl.itemRegistry = this.ItemRegistry
DoubleTapImpl.pe = this.PersistenceExtensions