Hello community,
I’m working on a rule which offers multiplie function by using one switch. The Switch is an LCN-switch, witch drives to virtual relays - one toggles each time, the switch is pressed short, the other will be ‘ON’ if the switch is pressed long and will be released to ‘OFF’ when the switch is no mor pressed. This actions drive a HUE color lamp …
Within the rule I first detect wheater a key was pressed twice or only on time … so I got swtich states for (pressed once short, pressd once long, pressed short twice, pressed short plus long and relase) → (keyPressedShort, Long, DoubleShort, ShortPlusLong, Released).
Do discover the double press action I use one timer (KeyTimer) and measure the time between to trigger actions … so far so good, this works pretty well.
In the second part of the rule I handle the ‘actionState’ … means interpreting what the user have done with the switch (single pressed, double pressed …). For the states i defined the following
DoubleShort … shitch between brightness- and color-mode
long … change brightess (this part works) or change color (won’t work)
So now we come to the problem … the color change. For this I planed to use a second timer, which should be retriggered until the long pressed switch will be released. Within the rule I increment or decrement the current color (hue) - you can find this part of the code within the switch statement in the case ‘Long’. My problem is, that it seams that the timer will not start and I dispair, becouse I don’t understand why. I think this is a simple problem, but I’m not able to see whats going wrong.
var keyPressedShort = 'Module11KGStef_Relais7';
var keyPressedLongOrRelease = 'Module11KGStef_Relais8';
var doublePressedTime = 600 ; // Unit = ms
var hueLightItem = 'Huecolorlamp2TestLeuchte_Farbe' ;
var hueLightAddress = 'hue:0210:02648f68d9:2';
var DIM_TIME = 4000; // defines the time for an dim-cycle from 0 to 100%
var defaultBrightness = 60; // Unit = %
var hueStepTime = 50 ; // Unit = ms
var hueChangeStep = 5 ; // Unit = degree
var triggerTime = Java.type('java.time.Instant') ;
var lastActionTime, currentActionTime, deltaActionTime lastBrigthness;
var actionState
// Brightness/Color Dimmer VARs
var things = Java.type('org.openhab.core.model.script.actions.Things');
var currentHueState = Java.type('org.openhab.core.library.types.HSBType');
var Brightness, Saturation, Hue, dimDirection, dimTime, fadeToBrightness, HSBstring;
var BrightnessShort, SaturationShort, HueShort ;
var dimmerMode;
var currentState;
var instantTime = Java.type('java.time.Instant') ;
var zdt = Java.type('java.time.ZonedDateTime');
var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var hueScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
if (typeof this.timers === 'undefined') {
this.timers = [];
}
// Initialize VARs once
if (lastActionTime == NaN) lastActionTime = 0;
if (lastBrigthness == NaN) lastBrigthness = defaultBrightness;
if ((dimmerMode != 'brightness') && (dimmerMode != 'color')) dimmerMode = 'brightness';
// estimate the time between to trigger-events
currentActionTime = triggerTime.now().toEpochMilli() ;
deltaActionTime = currentActionTime - lastActionTime ;
lastActionTime = currentActionTime ;
//switch (triggerItemName = event.itemName) {
switch (event.itemName) {
case keyPressedShort : if (deltaActionTime < doublePressedTime){
// Timer is running/defined --> second time pressed within the doublePressedTime
if (typeof this.timers['KeyTimer'] !== 'undefined') {
this.timers['KeyTimer'].cancel();
this.timers['KeyTimer'] = undefined;
actionState = 'DoubleShort' ;
}
} else {
// Not running Timer --> first hit ... so start a timer for doublePressedTime
if ((typeof this.timers['KeyTimer'] === 'undefined') || (this.timers['KeyTimer'].hasTerminated())) {
actionState = 'FirstAction';
this.timers['KeyTimer'] = scriptExecution.createTimer(zdt.now().plusNanos(doublePressedTime*1000000), function () {
if (typeof this.timers['KeyTimer'] !== 'undefined') {
this.timers['KeyTimer'].cancel();
this.timers['KeyTimer'] = undefined;
actionState = 'Short' ;
print ('. ') ; // DEBUG !!!!!!!!!!!!!!!!
var currentState = itemRegistry.getItem(hueLightItem).getState();
var HueShort = currentState.getHue();
var BrightnessShort = currentState.getBrightness();
var SaturationShort = currentState.getSaturation();
if (BrightnessShort == 0) {
Brightness = lastBrigthness;
events.sendCommand(hueLightItem, HueShort.toString() + "," + SaturationShort.toString() + "," + Brightness.toString());
} else {
events.sendCommand(hueLightItem, HueShort.toString() + "," + SaturationShort.toString() + ", 0");
lastBrigthness = BrightnessShort;
}
}
})
} else {
this.timers['KeyTimer'].cancel();
this.timers['KeyTimer'] = undefined;
actionState = 'TimerProblems' ;
}
}
break ;
case keyPressedLongOrRelease : if (actionState == 'FirstAction') {
if ((typeof this.timers['KeyTimer'] !== 'undefined') && ((this.timers['KeyTimer'].isRunning()) || this.timers['KeyTimer'].hasTerminated())) {
this.timers['KeyTimer'].cancel();
this.timers['KeyTimer'] = undefined;
}
if (itemRegistry.getItem(keyPressedLongOrRelease).getState() == 'ON') {
actionState = 'ShortPlusLong' ;
} else {
actionState = "'Don't Know" ;
}
} else {
if (itemRegistry.getItem(keyPressedLongOrRelease).getState() == 'ON') {
actionState = 'Long' ;
} else {
actionState = 'Released' ;
}
}
break ;
default : break ;
};
switch (actionState) {
case 'DoubleShort' : {print('..')} ; // DEBUG !!!!!!!!!!!!!!!!
if (dimmerMode == 'color') {
dimmerMode = 'brightness' ;
} else {
dimmerMode = 'color' ;
}
print (dimmerMode) ; // DEBUG !!!!!!!!!!!!!!!!
break ;
case 'ShortPlusLong' : {print('.-')} ;
break ;
case 'Long' : {print('- ')} ;
currentState = itemRegistry.getItem(hueLightItem).getState();
Hue = currentState.getHue();
Brightness = currentState.getBrightness();
Saturation = currentState.getSaturation();
print (dimmerMode); // DEBUG !!!!!!!!!!!!!!!!
if (dimmerMode == 'brightness') {
print('BRI') ; // DEBUG !!!!!!!!!!!!!!!!
if ((Brightness == 0) || (dimDirection == 'up')){
fadeToBrightness = 100 ;
dimTime = (100-Brightness)*DIM_TIME/100 ;
} else {
fadeToBrightness = 0 ;
dimTime = Brightness*DIM_TIME/100 ;
}
startTime = instantTime.now().toEpochMilli() ;
things.getActions('hue',hueLightAddress).fadingLightCommand("color", new PercentType(fadeToBrightness), new DecimalType(dimTime));
} else {
print('COL') ; // DEBUG !!!!!!!!!!!!!!!!
if ((typeof this.timers['hueTimer'] === 'undefined') || (this.timers['hueTimer'].hasTerminated())) {
this.timers['hueTimer'] = hueScriptExecution.createTimer(zdt.now().plusNanos(hueStepTime*1000000), function () {
if (dimDirection == 'up') {
if ((Hue += hueChangeStep) > 359) {
Hue = 0 ;
}
} else {
if ((Hue -= hueChangeStep) < 0) {
Hue = 359 ;
}
}
print(Hue); // DEBUG !!!!!!!!!!!!!!!!
events.sendCommand(hueLightItem, Hue.toString() + "," + Saturation.toString() + "," + Brightness.toString());
if (itemRegistry.getItem(keyPressedLongOrRelease).getState() == 'ON') {
print('Reschedule'); // DEBUG !!!!!!!!!!!!!!!!
this.timers['hueTimer'].reschedule(zdt.now().plusNanos(hueStepTime*1000000)) ;
} else {
print ('Cancel'); // DEBUG !!!!!!!!!!!!!!!!
this.timers['hueTimer'].cancel();
this.timers['hueTimer'] = undefined;
}
print('TheEnd'); // DEBUG !!!!!!!!!!!!!!!!
})
} else {
print ('Reschedule2'); // DEBUG !!!!!!!!!!!!!!!!
this.timers['hueTimer'].reschedule(zdt.now().plusNanos(hueStepTime*1000000));
}
}
break ;
case 'Released' : {print('**')} ; // DEBUG !!!!!!!!!!!!!!!!
if (dimmerMode == 'brightness') {
deltaTime = (instantTime.now().toEpochMilli() - startTime)/DIM_TIME * 100;
if (dimDirection == 'up') {
dimDirection = 'down' ;
if ((Brightness = Brightness + deltaTime) > 100) Brightness = 100 ;
} else {
dimDirection = 'up' ;
if ((Brightness = Brightness - deltaTime) < 0) Brightness = 0 ;
}
events.sendCommand(hueLightItem, Hue.toString() + "," + Saturation.toString() + "," + Brightness.toString());
} else {
if (dimDirection == 'up') {
dimDirection = 'down' ;
} else
dimDirection = 'up';
}
break ;
default : break ;
}
I hope that someone of you can help me with a simple … “this ist wrong, you have to do it that what …”
Okay, thanks for your help
Stef