Kurzes zwischenfazit.
Ich hatte meinen Anfang mit dem Script den ich weit vor diesem Post verfolgt hatte glaub ich zu früh verworfen.
Eure Motivation zu dem Thema hat glaub ich ein bisher ganz gut funktionierendes Script hervorgebracht. Im Moment fehlt mir eigentlich nur noch der Hold Modus. Das ist so aber glaub ich auch kein Problem mehr...
Hier mal der POC bis auf die Hold action.... Doof wenn man beim Programmieren auf einmal die Anouncer aus Unreal Tournament hört xD
// Objecttree for Shelly I3
const shelly_i3 = 'mqtt.0.stat.tasmota_243EBA';
// timeframe for multipress actions
const multipress_time = 300;
// Debug flag to increase verbosity
var DEBUG = false;
var timer1 = null;
var timer2 = null;
var timer3 = null;
var timer1_count = 0;
var timer2_count = 0;
var timer3_count = 0;
on({ id: shelly_i3 + '.SWITCH1', change: "any" }, async function (obj) {
if (!existsState('0_userdata.0.' + obj.id + '_timestamp')) {
createState('0_userdata.0.' + obj.id + '_timestamp', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_multipress_count')) {
createState('0_userdata.0.' + obj.id + '_multipress_count', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_hold')) {
createState('0_userdata.0.' + obj.id + '_hold', false,{
type: 'boolean',
});
}
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_timestamp', getState(obj.id).ts);
setState('0_userdata.0.' + obj.id + '_multipress_count',getState('0_userdata.0.' + obj.id + '_multipress_count').val + 1);
var diff = getState(obj.id).ts - getState('0_userdata.0.' + obj.id + '_timestamp').val;
clearInterval(timer1);
timer1_count = 0;
if (DEBUG == true) console.log('Switch1: Starte Timer...');
timer1 = setInterval(function() {
if (timer1_count != 0) {
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_hold',true);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch1: HOLD!!!!');
break;
case 2:
if (DEBUG == true) console.log('Switch1: Double press HOLD!!!!');
break;
case 3:
if (DEBUG == true) console.log('Switch1: Triple press HOLD!!!!');
break;
case 4:
if (DEBUG == true) console.log('Switch1: MULTI press HOLD!!!!');
break;
case 5:
if (DEBUG == true) console.log('Switch1: MO MO MO MO MONSTER PRESS HOLD!!!!');
break;
default:
if (DEBUG == true) console.log('Switch1: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times and held...');
break;
}
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
clearInterval(timer1);
return;
}
if (DEBUG == true) console.log('Switch1: ' + obj.id + ': ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val)
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch1: Single press');
break;
case 2:
if (DEBUG == true) console.log('Switch1: Double press');
break;
case 3:
if (DEBUG == true) console.log('Switch1: Triple press');
break;
case 4:
if (DEBUG == true) console.log('Switch1: MULTI press');
break;
case 5:
if (DEBUG == true) console.log('Switch1: MO MO MO MO MONSTER PRESS!!!');
break;
default:
if (DEBUG == true) console.log('Switch1: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times...');
break;
}
if (DEBUG == true) console.log('Switch1: Stoppe Timer...');
clearInterval(timer1);
}
timer1_count++;
}, multipress_time / 2);
if (DEBUG == true) console.log('Switch1: Time between presses on ' + obj.id + ': ' + diff);
} else {
if (getState('0_userdata.0.' + obj.id + '_hold').val == true) {
setState('0_userdata.0.' + obj.id + '_hold',false);
if (DEBUG == true) console.log('Switch1: Hold released...');
}
}
});
on({ id: shelly_i3 + '.SWITCH2', change: "any" }, async function (obj) {
if (!existsState('0_userdata.0.' + obj.id + '_timestamp')) {
createState('0_userdata.0.' + obj.id + '_timestamp', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_multipress_count')) {
createState('0_userdata.0.' + obj.id + '_multipress_count', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_hold')) {
createState('0_userdata.0.' + obj.id + '_hold', false,{
type: 'boolean',
});
}
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_timestamp', getState(obj.id).ts);
setState('0_userdata.0.' + obj.id + '_multipress_count',getState('0_userdata.0.' + obj.id + '_multipress_count').val + 1);
var diff = getState(obj.id).ts - getState('0_userdata.0.' + obj.id + '_timestamp').val;
clearInterval(timer2);
timer2_count = 0;
if (DEBUG == true) console.log('Switch2: Starte Timer...');
timer2 = setInterval(function() {
if (timer2_count != 0) {
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_hold',true);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch2: HOLD!!!!');
break;
case 2:
if (DEBUG == true) console.log('Switch2: Double press HOLD!!!!');
break;
case 3:
if (DEBUG == true) console.log('Switch2: Triple press HOLD!!!!');
break;
case 4:
if (DEBUG == true) console.log('Switch2: MULTI press HOLD!!!!');
break;
case 5:
if (DEBUG == true) console.log('Switch2: MO MO MO MO MONSTER PRESS HOLD!!!!');
break;
default:
if (DEBUG == true) console.log('Switch2: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times and held...');
break;
}
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
clearInterval(timer2);
return;
}
if (DEBUG == true) console.log('Switch2: ' + obj.id + ': ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val)
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch2: Single press');
break;
case 2:
if (DEBUG == true) console.log('Switch2: Double press');
break;
case 3:
if (DEBUG == true) console.log('Switch2: Triple press');
break;
case 4:
if (DEBUG == true) console.log('Switch2: MULTI press');
break;
case 5:
if (DEBUG == true) console.log('Switch2: MO MO MO MO MONSTER PRESS!!!');
break;
default:
if (DEBUG == true) console.log('Switch2: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times...');
break;
}
if (DEBUG == true) console.log('Switch2: Stoppe Timer...');
clearInterval(timer2);
}
timer2_count++;
}, multipress_time / 2);
if (DEBUG == true) console.log('Switch2: Time between presses on ' + obj.id + ': ' + diff);
} else {
if (getState('0_userdata.0.' + obj.id + '_hold').val == true) {
setState('0_userdata.0.' + obj.id + '_hold',false);
if (DEBUG == true) console.log('Switch2: Hold released...');
}
}
});
on({ id: shelly_i3 + '.SWITCH3', change: "any" }, async function (obj) {
if (!existsState('0_userdata.0.' + obj.id + '_timestamp')) {
createState('0_userdata.0.' + obj.id + '_timestamp', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_multipress_count')) {
createState('0_userdata.0.' + obj.id + '_multipress_count', 0,{
type: 'number',
});
}
if (!existsState('0_userdata.0.' + obj.id + '_hold')) {
createState('0_userdata.0.' + obj.id + '_hold', false,{
type: 'boolean',
});
}
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_timestamp', getState(obj.id).ts);
setState('0_userdata.0.' + obj.id + '_multipress_count',getState('0_userdata.0.' + obj.id + '_multipress_count').val + 1);
var diff = getState(obj.id).ts - getState('0_userdata.0.' + obj.id + '_timestamp').val;
clearInterval(timer3);
timer3_count = 0;
if (DEBUG == true) console.log('Switch3: Starte Timer...');
timer3 = setInterval(function() {
if (timer3_count != 0) {
if (getState(obj.id).val == "1") {
setState('0_userdata.0.' + obj.id + '_hold',true);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch3: HOLD!!!!');
break;
case 2:
if (DEBUG == true) console.log('Switch3: Double press HOLD!!!!');
break;
case 3:
if (DEBUG == true) console.log('Switch3: Triple press HOLD!!!!');
break;
case 4:
if (DEBUG == true) console.log('Switch3: MULTI press HOLD!!!!');
break;
case 5:
if (DEBUG == true) console.log('Switch3: MO MO MO MO MONSTER PRESS HOLD!!!!');
break;
default:
if (DEBUG == true) console.log('Switch3: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times and held...');
break;
}
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
clearInterval(timer3);
return;
}
if (DEBUG == true) console.log('Switch3: ' + obj.id + ': ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val)
setState('0_userdata.0.' + obj.id + '_multipress_count',0);
switch (getState('0_userdata.0.' + obj.id + '_multipress_count').val) {
case 1:
if (DEBUG == true) console.log('Switch3: Single press');
break;
case 2:
if (DEBUG == true) console.log('Switch3: Double press');
break;
case 3:
if (DEBUG == true) console.log('Switch3: Triple press');
break;
case 4:
if (DEBUG == true) console.log('Switch3: MULTI press');
break;
case 5:
if (DEBUG == true) console.log('Switch3: MO MO MO MO MONSTER PRESS!!!');
break;
default:
if (DEBUG == true) console.log('Switch3: pressed ' + getState('0_userdata.0.' + obj.id + '_multipress_count').val + ' times...');
break;
}
if (DEBUG == true) console.log('Switch3: Stoppe Timer...');
clearInterval(timer3);
}
timer3_count++;
}, multipress_time / 2);
if (DEBUG == true) console.log('Switch3: Time between presses on ' + obj.id + ': ' + diff);
} else {
if (getState('0_userdata.0.' + obj.id + '_hold').val == true) {
setState('0_userdata.0.' + obj.id + '_hold',false);
if (DEBUG == true) console.log('Switch3: Hold released...');
}
}
});
EDIT: In der Zwischenzeit hab ich Hold (als kleines Gimmick noch double press + Hold usw. da ich schon mal dabei war) auch noch eingebaut.
Macht es sinn das ganze hier im Forum als Plug and Play Lösung anzubieten? Vllt. auch erweitert um States im 0_userdata die jeweilige Aktionen nur auf true setzen, damit man es mit anderen Skripten leichter auswerten kann?
Und ja, ich weiß eigentlich könnte ich das ganze noch in weitere Funktionen aufbrechen. Mir reicht es so aber, und die arbeit in schöneren code möchte ich mir (zumindest heute) nicht mehr machen...