Ich hab mal in dein Script einen Code eingefügt der das Zeitliche Steuern ermöglichen soll. Es funktioniert soweit, schaltet über Zeit ein und aus und über die Last vom Heizschwert, aber ich bin mir nicht sicher ob das was ich da mit ChatGP gebastet hab gut ist!? Könntest Du bitte mal wenn Du Zeit, Lust und Laune hast das Script überfliegen ob das so Sinn mach? Die Zeilen 17-49 wurden eingefügt/modifiziert. Vieln Dank!
//Config
let heizschwert_Url= 'http://192.168.1.54/data.jsn';
var minLimit= 100; //Turn off Limit <= aPower
var maxLimit= 100; //Turn on Limit > aPower
var cid= 0; //Switch channel id
var updateInterval= 3; //JSON Update Interval in seconds.
var debug= false; //Debug output
function CheckPower(result){
try{
if(!result || typeof result.body !== 'string' || !result.body.length) throw new Error('wrong answer, result -->'+Str(result)); //Exit if useless data
let aPower= JSON.parse(result.body).power; //Parsing Json String into js object
let output= Status('switch',cid).output; //Get Switch output
if(debug) print('Debug: apower-->',aPower,', output-->',output); //Create debug msg
if(!output && aPower > maxLimit) Call('Switch.Set',{id:cid,on:true},null,null,debug); //Turn on
if(output && aPower <= minLimit) Call('Switch.Set',{id:cid,on:false},null,null,debug); //Turn off
ScheduleSocket();
} catch (e) {
ErrorMsg(e, 'CheckPower()');
}
}
function ScheduleSocket() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
// Zeitpunkte für das Ein- und Ausschalten
const schedule = [
{ hour: 5, minute: 00, action: 'on' },
{ hour: 7, minute: 30, action: 'off' },
{ hour: 12, minute: 00, action: 'on' },
{ hour: 13, minute: 00, action: 'off' },
{ hour: 17, minute: 30, action: 'on' },
{ hour: 19, minute: 30, action: 'off' },
];
// Überprüfen, ob zu einer geplanten Zeit eine Aktion durchgeführt werden soll
for (const entry of schedule) {
if (hours === entry.hour && minutes === entry.minute) {
if (entry.action === 'on') {
Call('Switch.Set', { id: cid, on: true }, null, null, debug);
} else if (entry.action === 'off') {
Call('Switch.Set', { id: cid, on: false }, null, null, debug);
}
break; // Nur eine Aktion pro Durchlauf
}
}
}
var tH1= 0; //Global Timer Handel
function Main(){
tH1= Timer.set(1000*updateInterval,true,function(){Call('http.get',{url: heizschwert_Url, timeout:2},CheckPower,null,debug);}) //Getting Json Data, (endles loop)
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
function SwitchM(cID,rM,iT,deBug){ //Change rM=RelayMode, iT=InputMode, cID=Relay_ID and/or Input_ID,
try{ //More Info, rM='detached'->detached/flip/momentary/follow,iT='switch'->button/switch
let r= 0;
if(Config('switch',cID).initial_state === 'match_input') Call('Switch.SetConfig',{id:cID,config:{initial_state:'restore_last'}});
if(rM && Config('switch',cID).in_mode !== rM){Call('Switch.SetConfig',{id:cID,config:{in_mode: rM}});if(!r){r= '';} r+='rM, ';}
if(iT && Config('input',cID).type !== iT){Call('Input.SetConfig',{id:cID,config:{type: iT}});if(!r){r= '';} r+='iT, ';}
if(deBug){r='Debug: tryed to change-> '+(r||'nothing,')+' Oldconfig: rM-> '+Config('switch',cID).in_mode+', iT-> '+Config('input',cID).type; print(r);}
return r;}catch(e){ErrorMsg(e,'SwitchM()');}}
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 && p.device.length > 0 && p.device.indexOf(d.info.component) === -1) fR.useless= true;
if(p.device && p.device.length > 0 && !fR.useless && !p.filterKey && !p.filterValue) fR= d.info;
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 Blink(bc,dl,c,st){//Call Blink funktion, bc=blinkCount, dl=blinkDelay, c=call->as_arry->[methode,parameter], st=startCount
try{
if(tH8) Timer.clear(tH8); tH8= 0; if(!Str(st)) st= 0; if(st < bc*2){st++;}else{st= 0; return;}
if(!tH8) tH8= Timer.set(1000*dl,0,function(){Call(c[0],c[1],function(){tH8= 0; Blink(bc,dl,c,st);},c[3],c[4]);});
}catch(e){ErrorMsg(e,'Blink()');}}
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 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 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.3-Alpha(full), Shelly FW >1.0.2
Setup();
Alles anzeigen