r/SmartThings • u/camplate • 5d ago
Devices Using motion sensor and switch for light
Kitchen has three entrances. Right now have motion sensor that covers most of the room, turns off light if no activity five minutes.
I bought a switch that also controls the light, but the light will turn off if motion sensor was triggered and then nothing for five minutes. Want the switch to be dominate, if it was pressed then motion sensor on/off is ignored.
I'm going to create second post about a different motion sensor and will put link here.
2
Upvotes
1
u/DarthOldMan 5d ago
Have you considered using a presence sensor? Then as long as someone is in the room, the light won’t go off, so no worries about leaving someone in the dark after 5 minutes. You’ll probably never touch the switch when using a presence sensor.
1
u/Jaffa0813 Enthusiast 5d ago
I'm not entirely sure if I understand your question. But I think what you need is to trigger a virtual switch in Smart Things that allows you to override default behavior.
And you put the virtual switch on a timer to reset to not enabled after a set time
1
u/calligood 4d ago
Hey I did this exact setup a couple weeks ago. Full disclosure: I had ChatGPT write the code for me but below is what it created. The code is probably bloated, but it sure was easy to copy and paste and it works great. If you don't object to using AI, you may want to run it through yourself to see if it will work.
The script assumes that the physical wall switch already controls the Shelly relay through its device configuration. It monitors the switch state to prevent motion automation from overriding manual operation.
It also requires Bluetooth and the Bluetooth observer to be enabled on the Shelly.
// ------------------------------------------------------------
// Shelly 1 Mini Gen3 + BLU Motion
// Five-minute automatic lighting
// Physical wall switch has priority
var SENSOR_MAC = "aa:bb:cc:dd:ee:ff"; // Replace with your sensor MAC
var DELAY = 300000; // 5 minutes
var autoOwned = false;
var offTimer = null;
var lastPacket = -1;
function wallSwitchOn() {
var s = Shelly.getComponentStatus("input:0");
return s && s.state === true;
}
function relayOn() {
var s = Shelly.getComponentStatus("switch:0");
return s && s.output === true;
}
function cancelTimer() {
if (offTimer !== null) {
Timer.clear(offTimer);
offTimer = null;
}
}
function stopAutomation() {
cancelTimer();
autoOwned = false;
}
function turnOnFromMotion() {
if (wallSwitchOn()) {
stopAutomation();
print("Wall switch ON - manual priority");
return;
}
if (!relayOn()) {
autoOwned = true;
Shelly.call("Switch.Set", {
id: 0,
on: true
});
print("LIGHTS ON - motion");
}
if (autoOwned) {
// Fallback timer in case a clear
// notification is missed
startOffTimer();
}
}
function startOffTimer() {
cancelTimer();
if (!autoOwned) return;
offTimer = Timer.set(DELAY, false, function() {
offTimer = null;
if (!autoOwned) return;
if (wallSwitchOn()) {
stopAutomation();
print("Manual switch ON - staying on");
return;
}
autoOwned = false;
Shelly.call("Switch.Set", {
id: 0,
on: false
});
print("LIGHTS OFF - 5 minute timeout");
});
}
function motionCleared() {
print("Motion cleared");
if (autoOwned) {
startOffTimer();
print("Five-minute timer started");
}
}
// Track changes at physical wall switch
Shelly.addStatusHandler(function(e) {
if (e.component !== "input:0") return;
if (!e.delta) return;
if (typeof e.delta.state === "undefined") return;
if (e.delta.state === true) {
print("Physical switch ON");
stopAutomation();
} else {
print("Physical switch OFF");
stopAutomation();
}
});
// Bluetooth scanner
function onScan(event, result) {
if (event !== BLE.Scanner.SCAN_RESULT) return;
if (!result || !result.addr) return;
if (result.addr.toLowerCase() !== SENSOR_MAC) {
return;
}
if (!result.service_data ||
!result.service_data["fcd2"]) {
return;
}
var data = result.service_data["fcd2"];
if (!data || data.length < 2) return;
if ((data.charCodeAt(0) & 1) !== 0) {
print("Encrypted packet ignored");
return;
}
var i = 1;
var packet = -1;
var motion = -1;
while (i < data.length) {
var type = data.charCodeAt(i++);
if (type === 0x00 && i < data.length) {
packet = data.charCodeAt(i++);
}
else if (type === 0x01 && i < data.length) {
i += 1;
}
else if (type === 0x21 && i < data.length) {
motion = data.charCodeAt(i++);
}
else if (type === 0x64 && i < data.length) {
i += 1;
}
else if (type === 0x3a && i < data.length) {
i += 1;
}
else if (type === 0xf0 && i + 2 <= data.length) {
i += 2;
}
else {
break;
}
}
if (packet >= 0 && packet === lastPacket) {
return;
}
if (packet >= 0) lastPacket = packet;
if (motion === 1) {
print("*** MOTION DETECTED ***");
turnOnFromMotion();
}
else if (motion === 0) {
motionCleared();
}
}
BLE.Scanner.Subscribe(onScan);
if (!BLE.Scanner.isRunning()) {
BLE.Scanner.Start({
duration_ms: BLE.Scanner.INFINITE_SCAN,
active: false
});
}
print("BLU Motion automation running");
print("Auto-off: 5 minutes");
print("Physical switch priority enabled");
// ------------------------------------------------------------
1
u/madskvistkristensen 4d ago
The trick is an override flag that the motion routine checks before it turns the light off.
The motion routine turns the light on too, so SmartThings can't tell "light is on" from "someone wants it to stay on." The switch press has to set something the routine can see.
A simple setup:
If your switch has double tap or a second button, use that as the "keep it on" press. Then a normal press still works like normal.
One gotcha: if the switch and the light are the same device, Routine 1 will also fire when motion turns the light on. In that case a separate button or a double tap is the way to go.
I wrote up the logic for this here: www.homeautomationcookbook.com/automation/lighting/lights-off-after-motion.html
1
u/bowlcut 4d ago
Funny cause up until a year or two ago, what you want was the default action and pissed me off. LIke Id have a time setting to turn on the lights but motion would turn them off if you happened to trigger motion right at the exact 2min window. So now it knows which altered state and which should take priority. Just seems yours is backwards of your wants/needs
1
u/Horror-Degree-8663 3d ago
virtual switch. if button pressed, virtual switch on and lights on. if virtual switch off, motion sensors control lights. that's how i did it
0
u/kanbak 5d ago
I posted your question into Google Gemini and it says yes and told me how to set it to. I used to use SmartThings but switched to home assistant this year and when I switched to home assistant I was overwhelmed with all the options so that's when I started asking an AI like chatGPT to help program it. I never thought to do that until home assistant. So that could be an option. But there i also like asking for help from real people on Reddit.