Skip to content

Instantly share code, notes, and snippets.

@nobodyguy
Last active February 13, 2024 17:03
Show Gist options
  • Save nobodyguy/1eea64200edc97fb96637793db3ae394 to your computer and use it in GitHub Desktop.
Save nobodyguy/1eea64200edc97fb96637793db3ae394 to your computer and use it in GitHub Desktop.
Shelly Plus Plug S script to automatically turn off the plug after 3D printing has finished and cooling delay has elapsed
// This script detects printing start and stop events based on power consumption.
// When the stop event is detected, it waits for 15 minutes to enable printer to cool down and shutdown event is triggered.
let CONFIG = {
cooldownDelay: 15 * 60 * 1000, // 15 minutes
idlePowerMax: 12, // 12 Watts (in reality 7-8W)
};
let cooldownTimer = null;
let printingStarted = false;
Shelly.addStatusHandler(function (event, user_data) {
//print(JSON.stringify(event));
if (typeof event.delta.apower !== "undefined") {
let currentPower = event.delta.apower;
if (currentPower > CONFIG.idlePowerMax && printingStarted === false) {
print("Printing has started");
printingStarted = true;
stopTimer();
}
if (currentPower < CONFIG.idlePowerMax && printingStarted) {
print("Printing has ended");
printingStarted = false;
startDelayedShutdown();
}
}
}, null);
function startDelayedShutdown() {
print("Starting delayed switch shutdown");
cooldownTimer = Timer.set(
CONFIG.cooldownDelay,
false,
function (ud) {
turnOffSwitch();
},
null
);
}
function stopTimer() {
if (cooldownTimer) {
Timer.clear(cooldownTimer);
cooldownTimer = null;
}
}
function turnOffSwitch() {
print("Switch shutdown");
Shelly.call(
"switch.set",
{ id: 0, on: false },
function (result, code, msg, ud) { },
null
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment