Last active
May 4, 2025 22:45
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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