NEWS
Optimierung eines Anwesenheit-Scriptes
-
Ich möchte gerne die Peripherie meines Hauses je nach Anwesenheit steuern.
Der Ansatz war das ganze per IOT und IFTTT zu machen. Klappt grundsätzlich auch, jedoch verliert unvermittelt einer der DP der drei zu prüfenden Familienmitgliedern seinen Anwesendheit Status, der aber genauso unvermittelt zurück kehrt??????!!!!!!!
Daher war meine Überlegung zusätzlich den Bluetooth Status zu ermitteln und auszuwerten.
Ich möchte hier mein Laienhaftes Script vorstellen, dass ich mir mit Unterstützung dieses Forums und insbesondere des Einsatzes von Paul zusammen gebastelt habe.
Vielleicht fällt jemanden eine Idee ein, dass Ganze Script eleganter zu gestalten. BTW... die Anwesenheitsmeldung ist relevant für meine Alarmanlage, die bislang nur "quiet" und nur per Whatsapp und VIS einen möglichen Alarm anzeigt./*****Steuerung Anwesenheit AN/AUS ************************************* ** JAN 2023 ** TUYA ** ** erstellt: 17.01.23 ** *************************************************************************/ var logging = true; var text; const ids = $('iot.*.services.custom_CAS'); const idsBt = $('tr-064.*.devices.*'); const idCnt = "0_userdata.0.Haus.AW.Entered"; //IOT const idCntBt = '0_userdata.0.Haus.AW.EnteredBt' //Bt const idAnzFenster = "javascript.3.zählen_Fenster.anzahlFensterauf"; const idFensterText = "javascript.3.zählen_Fenster.textFensterauf"; const id_Michi = "tr-064.0.devices.S20-FE-von-Michael" const id_Heike = "tr-064.0.devices.S20-FE-von-Heike" const id_Noel = "tr-064.0.devices.IPhone-Noel" const id_AWMichi = "0_userdata.0.Haus.AW.Michi" const id_AWStute = "0_userdata.0.Haus.AW.Stute" const id_AWNoel = "0_userdata.0.Haus.AW.Noel" const id_AW = "0_userdata.0.Haus.AW.AW"; const id_WZOFF = 'fully-tablet-control.0.device.tablet_wohnzimmer.commands.screenOff' const id_OGOFF = 'fully-tablet-control.0.device.tablet_og.commands.screenOff' const id_KücheOFF = 'fully-tablet-control.0.device.tablet_kueche.commands.screenOff' const id_FlurOFF = 'fully-tablet-control.0.device.tablet_flur.commands.screenOff' const id_BüroOFF = 'fully-tablet-control.0.device.tablet_buero.commands.screenOff' const id_WZON = 'fully-tablet-control.0.device.tablet_wohnzimmer.commands.screenOn' const id_OGON = 'fully-tablet-control.0.device.tablet_og.commands.screenOn' const id_KücheON = 'fully-tablet-control.0.device.tablet_kueche.commands.screenOn' const id_FlurON = 'fully-tablet-control.0.device.tablet_flur.commands.screenOn' const id_BüroON = 'fully-tablet-control.0.device.tablet_buero.commands.screenOn' const id_a = 'tuya.1.332670202cf432d81c26.1' //Duftstecker Wohnzimmer 1 const id_b = 'tuya.1.332670202462ab3ad68e.1' //Wohnzimmer 2 Tablet const id_c = 'tuya.1.824307882462ab3b0506.1' //Wohnzimmer 3 Deko const id_d = 'tuya.1.88008560d8f15bd1c472.1' //Multimedia Wohnzimmer const id_e = 'tuya.1.88008560d8f15bd2a8a9.1' //Multimedia Keller const id_f = 'tuya.1.bfd95452a83fa720eeaarm.1' //Fernseher Wintergarten //*****FUNKTION ENTERED VIA IFTTT******************************************/ function cntCAS() { let cnt = 0; ids.each(function (id, i) { if (getState(id).val == 'entered') cnt++; }); setState(idCnt, cnt, true); } cntCAS(); // Skriptstart ids.on(cntCAS); //*****FUNKTION Bluetooth Geräte Zählen dir TRUE sind********************/ function cntBt() { let cntBt = 0; idsBt.each(function (id, i) { if (getState(id).val == true) cntBt++; }); setState(idCntBt, cntBt-3, true); } cntBt(); // Skriptstart idsBt.on(cntBt); //ANWESENHEIT MELDUNG VIA Whatsapp****************************************************************/ on({ id: idCnt, change: 'ne' }, function (dp) { log("Anwesend: " + dp.state.val); if ((dp.state.val) > 0) { setState(id_AW, true) log("Jemand ist zu Hause"); if ((dp.state.val) == 1) { var text = '\ud83d\ude4b *Anwesenheit*:\n' + dp.state.val + ' Person ist zu Hause' } else if ((dp.state.val) > 1) { var text = '\ud83d\ude4b *Anwesenheit*:\n' + dp.state.val + ' Personen sind zu Hause' } if (logging) sendTo('whatsapp-cmb.0', 'send', { text: text, phone: '+49xx' }); createEventlog("Anwesendheit", text) }; if ((dp.state.val) == 0) { setState(id_AW, false); log("Niemand ist zu Hause"); if (getState(idAnzFenster).val >= 1) { var Fenstertext = getState(idFensterText).val; var anz = getState(idAnzFenster).val; var text = anz + " Fenster ist offen: " + Fenstertext; log(text); var text = '\ud83d\udeb6\u200d\u2642\ufe0f *Abwesenheit*:\n _Niemand ist zu Hause,_\n _folgende Fenster sind offen_:\n' + Fenstertext //MAN createEventlog("Abwesendheit", text) if (getState(idAnzFenster).val == 1) { var text = '\ud83d\udeb6\u200d\u2642\ufe0f *Abwesenheit*:\n _Niemand ist zu Hause,_\n _folgendes Fenster ist offen_:\n' + Fenstertext //MAN createEventlog("Abwesendheit", text) } }; if ((dp.state.val) == 0) { if (getState(idAnzFenster).val == 0) { var text = '\ud83d\udeb6\u200d\u2642\ufe0f *Abwesenheit*:\n _Niemand ist zu Hause,_\n _alle Fenster sind geschlossen_\n' //MAN createEventlog("Anwesendheit", text) } }; if (logging) sendTo('whatsapp-cmb.0', 'send', { text: text, phone: '+49xxx' }); } }); //***Holt tr-064 Daten in 0_userdata**********************************************************/ on({ id: id_Michi, change: 'any', val: false }, function (dp) { setState(id_AWMichi, false) if (dp.state.val == true) { setState(id_AWMichi, true) } }); on({ id: id_Heike, change: 'any', val: false }, function (dp) { setState(id_AWStute, false) if (dp.state.val == true) { setState(id_AWStute, true) } }); on({ id: id_Noel, change: 'any', val: false }, function (dp) { setState(id_AWNoel, false) if (dp.state.val == true) { setState(id_AWNoel, true) } }); //***Bluetooth Auslöser setzt Anwesenheit*******************************************************/ const auslöser = [id_Michi, id_Heike, id_Noel]; on({ id: auslöser, change: 'any', val: true }, function (dp) { log("Auslöser: " + dp.name) setState(id_AW, true) var text = '\ud83d\ude4b *Anwesenheit*:\n' + dp.name + ' kommt nach Hause' if (logging) sendTo('whatsapp-cmb.0', 'send', { text: text, phone: '+49xxx' }); }); //***SETTINGS der Peripherie im Haus nach Anwesenheit Status**************************************/ on({ id: id_AW, change: 'any', val: true }, function (dp) { setState(id_WZON, true); setState(id_OGON, true); setState(id_FlurON, true); setState(id_KücheON, true); setState(id_BüroON, true); //setState(id_a,true); //setState(id_b,true); //setState(id_c,true); setState(id_d, true); setState(id_e, true); //setState(id_f,true); }); on({ id: id_AW, change: 'any', val: false }, function (dp) { setState(id_WZOFF, true); setState(id_OGOFF, true); setState(id_FlurOFF, true); setState(id_KücheOFF, true); setState(id_BüroOFF, true); setState(id_a, false); setState(id_b, false); setState(id_c, false); setState(id_d, false); setState(id_e, false); setState(id_f, false); });
Gruß aus dem Ruhrpott
-
@michihorn
Ich würde dein Handynummer im Script unsichtbar machen. -
@dimaiv Ups ist mir durch gegangen
-
@michihorn schon mal dran gedacht, die devices als Listen zu definieren und dann über die einzelnen elemente der Liste zu loopen?
// nicht so const id_Michi = "tr-064.0.devices.S20-FE-von-Michael" const id_Heike = "tr-064.0.devices.S20-FE-von-Heike" const id_Noel = "tr-064.0.devices.IPhone-Noel" on({ id: id_Michi, change: 'any', val: false }, function (dp) { setState(id_AWMichi, false) if (dp.state.val == true) { setState(id_AWMichi, true) } }); on({ id: id_Heike, change: 'any', val: false }, function (dp) { setState(id_AWStute, false) if (dp.state.val == true) { setState(id_AWStute, true) } }); on({ id: id_Noel, change: 'any', val: false }, function (dp) { setState(id_AWNoel, false) if (dp.state.val == true) { setState(id_AWNoel, true) } }); // sondern so var deviceList = [ { device: "tr-064.0.devices.Michael-Handy", user: "Michael", type: "tr-064" }, { device: "tr-064.0.devices.Heike-Handy", user: "Heike", type: "tr-064" }, { device: "tr-064.0.devices.IPhone-Noel", user: "Noel", type: "tr-064" } ]; for (var i = 0; i < deviceList.length; i++) { if deviceList[i].type == "tr-064" { on({ id: deviceList[i].device, change: "any", val: false }, function (dp) { setState(id_AWNoel, false) if (dp.state.val == true) { setState(id_AWNoel, true) } }); } }; Hat den Vorteil, dass du den Code nicht ändern musst, falls mal ein Handy dazukommt.