-
Autor
Moinsen Leute đź‘‹
Ich möchte gerne meine Shelly's über die BLU Motion Sensoren steuern.
Ich versuche schon alles mögliche und nichts klappt so wirklich.
Jetzt hab ich es zuletzt hin bekommen einen Motion Sensor als BTHome hinzuzufĂĽgen. Wusste ich vorher auch nicht, dass man das extra machen muss.
Folgendes Script soll das Konzept erfĂĽllen:
Konzept
1. Jeder Shelly fĂĽhrt das Script lokal aus.
2. Alle Motion- und Lux-Sensoren pro Raum werden automatisch erkannt.
3. Letzte Werte jedes Sensors werden gespeichert.
4. Licht geht an, sobald irgendein Motion-Sensor aktiv ist und der zugehörige Lux-Wert < Threshold.
5. Licht geht aus, wenn kein Motion-Sensor mehr Bewegung meldet.
6. Timer/Hold/Instant-Modus.
7. Debug zeigt jede Sensoränderung und die Entscheidung für das Licht.
Quasi mehrere Motion Sensoren pro Raum aber auch manchmal nur einer.
// --- Konfiguration ---
let mode = "hold"; // "instant", "timer", "hold"
let timerSeconds = 30; // Timerdauer
let luxThreshold = 100; // Lux-Schwelle
let DEBUG = true;
let LOCAL_LIGHT_ID = 0;
// Zusätzliche Lichter via HTTP
let ADDITIONAL_LIGHTS = [
// { "ip": "192.168.1.100", "id": 0, "auth": "your_auth_key" }
];
// ----------------------
let timer = null;
let motionStates = {}; // motion sensorId -> Boolean
let luxStates = {}; // lux sensorId -> Number
// --- Lichtfunktionen ---
function turnLightOn() {
print("đź’ˇ Licht AN");
Shelly.call("Switch.Set", { id: LOCAL_LIGHT_ID, on: true });
for (let l of ADDITIONAL_LIGHTS) {
let url = "http://" + l.ip + "/rpc/Switch.Set?id=" + l.id + "&on=true";
if (l.auth) url += "&auth_key=" + l.auth;
Shelly.call("HTTP.GET", { url: url });
}
}
function turnLightOff() {
print("đź’ˇ Licht AUS");
Shelly.call("Switch.Set", { id: LOCAL_LIGHT_ID, on: false });
for (let l of ADDITIONAL_LIGHTS) {
let url = "http://" + l.ip + "/rpc/Switch.Set?id=" + l.id + "&on=false";
if (l.auth) url += "&auth_key=" + l.auth;
Shelly.call("HTTP.GET", { url: url });
}
}
// --- Hilfsfunktion ---
function parseValue(val) {
if (val === "true") return true;
if (val === "false") return false;
if (!isNaN(parseFloat(val))) return parseFloat(val);
return val;
}
// --- Lichtentscheidungs-Logik ---
function shouldLightBeOn() {
for (let motionId in motionStates) {
if (motionStates[motionId] === true) {
// Suche zugehörigen Lux-Sensor (alle Lux-Werte prüfen)
for (let luxId in luxStates) {
if (luxStates[luxId] < luxThreshold) return true;
}
}
}
return false;
}
// --- Event-Handler ---
Shelly.addEventHandler(function(event) {
if (event.component && event.component.indexOf("bthomesensor:") === 0) {
let sensorId = event.component.split(":")[1];
let val = parseValue(event.info && event.info.value);
if (val === true || val === false) {
motionStates[sensorId] = val;
if (DEBUG) print("Motion Sensor", sensorId, "=", val);
} else if (typeof val === "number") {
luxStates[sensorId] = val;
if (DEBUG) print("Lux Sensor", sensorId, "=", val);
}
// Lichtsteuerung
let lightShouldBeOn = shouldLightBeOn();
if (lightShouldBeOn) {
turnLightOn();
if (timer) { Timer.clear(timer); timer = null; }
} else {
if (mode === "instant") {
turnLightOff();
} else if (mode === "timer") {
if (timer) Timer.clear(timer);
timer = Timer.set(timerSeconds*1000, false, turnLightOff);
} else if (mode === "hold") {
if (timer) Timer.clear(timer);
timer = Timer.set(timerSeconds*1000, false, function() {
if (!shouldLightBeOn()) turnLightOff();
timer = null;
});
}
}
}
});
print("📡 Script gestartet – automatische Multi-Sensor-Logik, Mode: " + mode + ", Lux < " + luxThreshold);
Alles anzeigen
Kann mir jemand sagen warum nichts passiert?