ich hab die IP, URL und das Auslesen des Wertes im Skript vom DeKat angepaßt.
Sonst habe ich nichts geändert.
Patrick
var intervall = 60, //Humidity Checking Intervall, in Seconds
remoteIP = "192.168.0.33", //Remote Shelly IP, as String
BTH_H_id = "201", //BTHomeSensor-Id for humidity
hum_delta_min = -20, //Hum_min negativ Delta, to turn on, 0 if not needed
hum_delta_max = -5, //Hum_max postiv Delta, to turn off, 0 if not needed
debug = true;
//Wenn die Luftfeuchtigkeit um einen gewissen Wert außen niedriger ist, wie in der Garage, soll das Relay des Shelly Plus 1 in der Garage geschlossen werden.
function Check_Loop(res) {
try {
if (res.code !== 200) return; //Exit if wrong Response
// let extern_hum = Number(JSON.parse(res.body).rh); //Getting remote Humidity Value
let extern_hum = Number(JSON.parse(res.body).value); //Getting remote Humidity Value from BTHomeSensor
let intern_hum = Number(Status('Humidity', 100).rh); //Getting local Humidity Value
let hum_Delta = extern_hum - intern_hum; //Calc hum Delta
if (debug) print('Debug: Hum delta-->', hum_Delta, 'ext_h-->', extern_hum, 'int_h-->', intern_hum); //Debug output
if (hum_delta_min && hum_Delta < hum_delta_min) Call("Switch.set", { id: 0, on: true }, null, null, debug); //Switching Relay Channed 0 on
if (hum_delta_max && hum_Delta > hum_delta_max) Call("Switch.set", { id: 0, on: false }, null, null, debug); //Switching Relay Channel 0 off
} catch (e) { ErrorMsg(e, 'Check_Loop()'); } //Error Handler
}
let tH1 = 0; //Timer Handler
function Main() {
if (!tH1) Timer.set(1000 * intervall, true, function () { //Endless Loop
Call("http.get", { url: "http://" + remoteIP + "/rpc/BTHomeSensor.GetStatus?id=" + BTH_H_id, timeout: 5 }, Check_Loop, null, debug); //Call Remote Shelly
// Call("http.get", { url: "http://" + remoteIP + "/rpc/Humidity.GetStatus?id=100", timeout: 5 }, Check_Loop, null, debug); //Call Remote Shelly
});
}
// Dekats Toolbox, a universal Toolbox for Shelly scripts
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;
while (cCache[0] && aC < callLimit) {
if (cCache[0] && !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 (tH9) { Timer.clear(tH9); tH9 = 0; }
if (nCall[0] || cCache[0]) if (cSp <= 0) cSp = 0.1; tH9 = Timer.set(1000 * cSp, 0, function () { tH9 = 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() { //Wating 2sek, to avoid a Shelly FW Bug
try {
if (Main && !tH9) {
tH9 = Timer.set(2000, 0, function () {
print('\nStatus: started Script _[', scriptN, ']_');
if (callLimit > 5) { callLimit = 5; } try { Main(); } catch (e) { ErrorMsg(e, 'Main()'); tH9 = 0; Setup(); }
});
}
} catch (e) { ErrorMsg(e, 'Setup()'); }
}
function ErrorMsg(e, s, deBug) { //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()' || deBug) 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 tH8 = 0, tH9 = 0, aC = 0, cCache = [], nCall = [], callLimit = 5, cacheLimit = 40, cSp = 0.1; //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 v3.5 -Alpha(cut), Shelly FW >1.0.8
Setup();
Alles anzeigen