Skip to content

Instantly share code, notes, and snippets.

@blindguynar
Last active February 4, 2026 13:30
Show Gist options
  • Select an option

  • Save blindguynar/ccc86d2a2025bc050bc4a3d9e1a7c513 to your computer and use it in GitHub Desktop.

Select an option

Save blindguynar/ccc86d2a2025bc050bc4a3d9e1a7c513 to your computer and use it in GitHub Desktop.
Shelly Script
let switchId = 1;
let checkPeriod = 60 * 1000; // in ms
let haUrl = 'http://192.168.50.10:8123';
let mode = "detached"; // Could be "detached", "flip", "follow" or "momentary". Use "detached" when HA is in control
function updateConfig(oldConfig) {
//Apply the new config only if the switch is not already working in the desired mode
if (oldConfig.in_mode !== mode) {
print("switching it up");
Shelly.call("Switch.SetConfig", { id: switchId, config: { in_mode: mode } });
if (mode === "detached") {
print("set switch on and hooks off");
Shelly.call("Switch.Set", { id: switchId, on: true });
}
} else {
print("no change");
}
}
function setSwitchFromResponse(response) {
print("response");
// If HA is accessible the response code is 200 update the current mode and the new configuration
if (response === undefined) {
mode = "flip";
print("default flip");
} else {
if (response.code === 200) {
mode = "detached";
print("detached");
} else {
mode = "flip";
print("flip");
}
}
// Get the current output configuration and pass it to updateConfig
print("update config");
Shelly.call("Switch.GetConfig", {id: switchId}, updateConfig);
print("update config done")
}
function testHA() {
// For RPC command HTTP.GET, both url and body are required.
print("test ha server");
Shelly.call("HTTP.GET", {url: haUrl, body:{}}, setSwitchFromResponse);
}
print("Starting HA Monitor");
// Timer.set(period_in_ms, reapeate_indefinitely, function)
Timer.set(checkPeriod, true, testHA);
@blindguynar
Copy link
Author

This is originally not my code. I found it somewhere long ago. Sorry i can't find where, but know i'm not responsible for v1 codebase. All that credit goes to original coder, whomever they were.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment