Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
// B. Nelissen
// Loop detection and dimmer preperation
// timeouts
let msLoopDetection = 100;
let msWithinDimming = 2000;
let currentTime=Math.round(Date.now());
// setting variables
let previousTime=context.get('time') || 0;
let previousIson=context.get('ison') || false;
let timeDifference=currentTime-previousTime;
// catch loop and exit
if (msLoopDetection > timeDifference) return;
// dimmer function
function changeBrightness(brightness) {
var step0 = 5;
var step1 = 30;
var step2 = 55;
var step3 = 100;
if (!(brightness >= 0 && brightness <= 100)) {
node.warn('Invalid brightness, set to 42')
brightness=step1;
}
switch (true) {
case (brightness < step0):
brightness=step0;
break;
case (brightness < step1):
brightness=step1;
break;
case (brightness < step2):
brightness=step2;
break;
case (brightness < step3):
brightness=step3;
break;
default: // when 100
brightness=step0;
}
return brightness;
}
// dimmer when state goes from OFF to ON
if(msg.payload.ison && (msg.payload.ison !== previousIson)){
switch (true) {
case (msLoopDetection > timeDifference): // loops should be catched already
msg = null;
break;
case (msWithinDimming > timeDifference): // dimmer
msg.payload.brightness = changeBrightness(msg.payload.brightness)
break;
default: //do nothing
break;
}
}
context.set('time',currentTime);
context.set('ison',msg.payload.ison);
return msg;
lightOn = msg.payload.ison;
if (lightOn){
msg.payload = 'on';
}else{
msg.payload = 'off';
}
return msg;
brightness = msg.payload;
msg.payloadold = msg.payload;
// set the brightness, turn the light on
msg.payload = {
"turn": "on",
"brightness": + brightness
};
return msg;