Hallo zusammen,
hier im Forum hat jemand einen Code geschrieben für die Lüftersteuerung im Bad wo auch sehr gut funktioniert
wäre jemand so nett und könnte den Code erweitern so das der Lüfter nur zwischen 8 und 22 uhr läuft, nachts also nicht ?
Vielen Dank im Voraus
Code
//Config
relay_ID_Fan= 1;
relay_ID_Light= 0;
min_LightTime= 3; //Delay in Minutes, that your light needs to be on.
delay_Fan= 0; //Delay in Minutes, that your fan should keep spinning after the Light is turned off.
debug= false; //Create Debug output
let tH1= 0; //Timer Handel
function Check_Light(data){
let result= Efilter(data,{device:['switch:'+relay_ID_Light], filterKey:['state']}); //Filter Event data
if(!result) return;
if(!result.state){ //Reset light Timer and turn off Fan
Timer.clear(tH1);
tH1= 0; //Reset Timer Handel;
Timer.set(1000*60*delay_Fan, false, function(){tH1= 0; Call('Switch.set',{id:relay_ID_Fan, on: false},0,0,debug);});
}
if(!tH1 && result.state){ //Reset FanDelay, Set light Timer and turn on Fan
Timer.clear(tH1);
tH1= 0; //Reset Timer Handel;
tH1= Timer.set(1000*60*min_LightTime, false, function(){tH1= 0; Call('Switch.set',{id:relay_ID_Fan, on: true},0,0,debug);});
}
}
function Main(){
Shelly.addEventHandler(Check_Light); //Add EventHander with Async Callback
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
function Str(d){ //Upgrade JSON.stringify
if(d === null || d === undefined)return null;
if(typeof d === 'string')return d;
return JSON.stringify(d);}
function Efilter(d,p,deBug) { //Event Filter, d=eventdata, p={device:[], filterKey:[], filterValue:[], noInfo:true, inData:true}->optional_parameter
try{
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){print('Error: Efilter(), ',e);}}
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 === -104) print('Info: no connection, maybe wrong URL, for local Shelly use local Method-> ["Switch.Toggle",{id:0}]');
if(e) throw new Error(Str(m));
if(Str(r) && Str(r.code) && r.code !== 200) throw new Error(Str(r));
}catch(e){print('Error: ErrorChk(), call Answer: ',e);}}
function Call(m,p,CB,uD,deBug){ //Upgrade Shelly.call
try{
let d={};
if(deBug) print('Debug: calling:',m,p); if(typeof CB === 'function') d.CB= CB; if(Str(uD)) d.uD= uD;
if(m === null && p === null){CB(uD); return;}
if(aC < callLimit){aC++; Shelly.call(m,p,ErrorChk,d); return 0;}else{print('Status: Call(), to many Calls in use, drop Call:',m,p);}
}catch(e){print('Error: Call(), ',e); return 1;}}
function Setup(){ //Wating 2sek, to avoid a Shelly FW Bug
if(Main !== undefined && !tH9){tH9 = Timer.set(2000,false,function(){tH9 = null; print('Status: started Script _[', scriptN,']_');
if(callLimit > 5){callLimit=5;} try{Main();}catch(e){print('Error: Main(), ',e); Setup();};});}}
var tH8= 0, tH9= 0, callLimit= 5, aC= 0, callList=[], //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 v1.8-Alpha(Cut), Shelly FW >1.2!
Setup();
Alles anzeigen