Hallo De Kat,
Danke - habe das nochmal angepasst und jetzt macht das Script was es soll. Ist unten nochmal angehängt - vielleicht fällt Dir ja noch was auf...
Meine Änderungen:
- Allgemein etwas vereinfacht da ich nur ein Gerät überwachen möchte
- Kein Fake-Event, der fehlende Event beim Script-Start ist bei mir eigentlich kein Problem und den Code verstehe ich noch nicht ganz
- Beim ProEM ist das Device "em1:0" (bzw. "em1:1" für den zweiten Kanal) und der Last-Parameter heisst "act_power"
Danke nochmal für die Hilfestellung
Michael
Code
var debug= false; //Create Debug output
var config= {
powerLimit: 30, //Limit in Watt, power needs tp be > Limit for sending a Call
powerTime: 10, //Time in seconds, how long the power needs to be over the powerLimit, before sending a call
}
var tHandle=0, called=false; //global Variables
function DoCall(){ //Do Webhook
try{
called= true;
tHandle= 0; //Reset Timer Handle
Shelly.call("HTTP.POST",{url: "https://api.pushover.net/1/messages.json",body: {"token":"myTokenKey","user":"myUserKey","message":"Laufzeitalarm"}},
function (response, error_code, error_message, ud) {if(debug) print(JSON.stringify(response))},null);
}catch(e){ErrorMsg(e,'DoCall()');}
}
function CheckPower(d){
let r= Efilter(d,{device:['em1:0'], filterKey:['act_power','id','component']},0); //Filter event data
if(debug) print(Str(r));
if(!r || !Str(r.act_power)) { //Exit if usless data
if(debug) print ('Useless Data') return;
}
if(r.act_power >= config.powerLimit && !tHandle && !called){ //Check if over Powerlimit
if(debug) print('Timer armed');
tHandle= Timer.set(1000*config.powerTime,false,function() {DoCall()});
}
if(r.act_power < config.powerLimit){ //Check if under Powerlimit
if(debug) print('Timer cleared');
Timer.clear(tHandle); //Reset Timer
tHandle= 0;
called= false; //Reset Called Flag
}
if(debug) print('Debug: found Event ',r,' Timer active:',tHandle>0,' Called:',called === true); //Debug output
}
function Main(){ //Main Code
Shelly.addEventHandler(CheckPower); //Add EventHandler with Asyn CallBack
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
function Str(d){ //Upgrade JSON.stringify
try{
if(d === null || d === undefined) return null; if(typeof d === 'string')return d;
return JSON.stringify(d);}catch(e){ErrorMsg(e,'Str()');}}
function Cut(f,k,o,i){ //Upgrade slice f=fullData, k=key-> where to cut, o=offset->offset behind key, i=invertCut
try{
let s= f.indexOf(k); if(s === -1) return; if(o) s= s+o.length || s+o; if(i) return f.slice(0,s);
return f.slice(s);}catch(e){ErrorMsg(e,'Cut()');}}
function Efilter(d,p,deBug) { //Event Filter, d=eventdata, p={device:[], filterKey:[], filterValue:[], noInfo:true, inData:true}->optional_parameter
try{
let fR= {};
if(p.noInfo){fR= d; d= {}; d.info= fR; fR= {};} if(p.inData && d.info.data){d.info= d.info.data; delete d.info.data;}
if(!d.info) fR.useless= true; if(p.device.length > 0 && p.device.indexOf(d.info.component) === -1) fR.useless= true;
if(p.filterKey && !fR.useless) for(f of p.filterKey) for(k in d.info) if(f === k) fR[k]= d.info[k];
if(p.filterValue && !fR.useless) for(f of p.filterValue) for(v of d.info) if(Str(v) && f === v) fR[Str(v)]= v;
if(deBug) print('\nDebug: EventData-> ', d, '\n\nDebug: Result-> ', fR, '\n');
if(Str(fR) === '{}' || fR.useless){return;} return fR;}catch(e){ErrorMsg(e,'Efilter()');}}
function ErrorChk(r,e,m,d){ //Shelly.call error check
try{
aC--; if(aC<0) aC= 0;
if(d.CB && d.uD) d.CB(r,d.uD); if(d.CB && !d.uD) d.CB(r);
if(!d.CB && d.uD) print('Debug: ',d.uD); if(e) throw new Error(Str(m));
if(Str(r) && Str(r.code) && r.code !== 200) throw new Error(Str(r));
}catch(e){ErrorMsg(e,'ErrorChk(), call Answer');}}
function Cqueue(){ //Shelly.call queue
try{
if(!cCache[0] && !nCall[0]) return; if(!nCall[0]){nCall= cCache[0]; cCache.splice(0,1);}
if(nCall[0] && aC < callLimit){Call(nCall[0],nCall[1],nCall[2],nCall[3],nCall[4]); nCall= [];}
if((nCall[0] || cCache[0]) && !tH7) tH7= Timer.set(1000*cSp,0,function(){tH7= 0; Cqueue();});}catch(e){ErrorMsg(e,'Cqueue()');}}
function Call(m,p,CB,uD,deBug){ //Upgrade Shelly.call
try{
let d= {};
if(deBug) print('Debug: calling:',m,p); if(CB) d.CB= CB; if(Str(uD)) d.uD= uD; if(!m && CB){CB(uD); return;}
if(aC < callLimit){aC++; Shelly.call(m,p,ErrorChk,d);}else if(cCache.length < cacheLimit){
cCache.push([m,p,CB,uD,deBug]); if(deBug) print('Debug: save call:',m,p,', call queue now:',cCache.length); Cqueue();
}else{throw new Error('to many Calls in use, droping call: '+Str(m)+', '+Str(p));}}catch(e){ErrorMsg(e,'Call()');}}
function Setup(l){ //Wating 2sek, to avoid a Shelly FW Bug
try{
if(Main && !tH9){
tH9= Timer.set(2000,l,function(){print('\nStatus: started Script _[', scriptN,']_');
if(callLimit > 5){callLimit= 5;} try{Main();}catch(e){ErrorMsg(e,'Main()'); Setup();}});}}catch(e){ErrorMsg(e,'Setup()');}}
function ErrorMsg(e,s){ //Toolbox formatted Error Msg
try{
let i= 0; if(Cut(e.message, '-104: Timed out')) i= 'wrong URL or device may be offline';
if(s === 'Main()') i= e.stack; if(Cut(e.message, '"Main" is not')) i= 'define a Main() function before using Setup()';
print('Error:',s || "",'---> ',e.type,e.message); if(i) print('Info: maybe -->',i);}catch(e){print('Error: ErrorMsg() --->',e);}}
var tH7= 0, tH8= 0, tH9= 0, aC= 0, cCache= [], nCall= [], callLimit= 5, cacheLimit= 40, cSp= 0.2; //Toolbox global variable
var Status= Shelly.getComponentStatus, Config= Shelly.getComponentConfig; //Renamed native function
var info= Shelly.getDeviceInfo(), scriptID= Shelly.getCurrentScriptId(), scriptN= Config('script',scriptID).name; //Pseudo const, variabel
//Toolbox v2.1-Alpha(cut), Shelly FW >1.0.2
Setup();
Alles anzeigen