Shelly Script for Seamless HomeAssistant Failover

I’ve been exploring ways to enhance my home automation setup, especially ensuring reliability when unexpected issues arise. One of the key components in my system is the Shelly Plus 2 PM, which I’ve been using to control my window blinds. Initially, I had it set up in the detached mode, relying on HomeAssistant to handle the positioning through button commands. However, I noticed that during network outages or when HomeAssistant crashes (which happens occasionally on my Raspberry Pi 4), the button commands would go unresponsive, leaving me unable to manually adjust the blinds.

To address this, I decided to create a script that automatically switches the Shelly device to dual mode when HomeAssistant becomes unreachable. This ensures that I can still manually control the blinds even during an outage. Once HomeAssistant is back online, the script seamlessly switches the device back to detached mode, maintaining the integration with my smart home setup.

The script works by continuously monitoring the status of HomeAssistant. If it detects an outage, it triggers the mode switch to dual. When connectivity is restored, it switches back to detached mode. This solution has been tested and works reliably, providing peace of mind knowing my system remains functional even during unexpected downtime.

For those interested in implementing similar functionality, I recommend checking out the Shelly documentation and adapting the script to your specific needs. While I’ve focused on the Shelly Plus 2 PM, the principles can be applied to other Shelly devices as well, depending on their capabilities.

Here’s the script I developed, which you can customize by replacing the HomeAssistant IP address with your own setup:
javascript
// Function to activate detached mode
function activateDetachedMode() {
print(“Attempting to switch to detached mode…”);
Shelly.call(“Cover.GetConfig”, {“id”: 0}, function(res, error) {
if (error) {
print("Error checking current mode: " + JSON.stringify(error));
} else {
if (res.in_mode !== “detached”) {
print("Current mode: " + res.in_mode);
Timer.set(1000, false, function() {
Shelly.call(“Cover.SetConfig”, {
“id”: 0,
“config”: {
“in_mode”: “detached”,
“swap_inputs”: false
}
}, function(res, error) {
if (error) {
print("Error switching to detached mode: " + JSON.stringify(error));
} else {
print(“Successfully switched to detached mode.”);
}
});
});
} else {
print(“Already in detached mode.”);
}
}
});
}

// Function to switch to dual mode
function switchBackToDualMode() {
print(“Switching to dual mode…”);
Shelly.call(“Cover.GetConfig”, {“id”: 0}, function(res, error) {
if (error) {
print("Error checking current mode: " + JSON.stringify(error));
} else {
if (res.in_mode !== “dual”) {
print("Current mode: " + res.in_mode);
Timer.set(1000, false, function() {
Shelly.call(“Cover.SetConfig”, {
“id”: 0,
“config”: {
“in_mode”: “dual”,
“swap_inputs”: false
}
}, function(res, error) {
if (error) {
print("Error switching to dual mode: " + JSON.stringify(error));
} else {
print(“Successfully switched to dual mode.”);
}
});
});
} else {
print(“Already in dual mode.”);
}
}
});
}

// Function to check HomeAssistant status
function checkHomeAssistantStatus() {
print(“Checking HomeAssistant connectivity…”);
Shelly.call(“http.get”, {
url: “http://192.168.179.150:8123
}, function(response, error) {
if (error) {
print(“HomeAssistant is currently unreachable.”);
switchBackToDualMode();
} else {
print(“HomeAssistant is online.”);
activateDetachedMode();
}
Timer.set(10000, false, checkHomeAssistantStatus);
});
}

// Start monitoring HomeAssistant status
checkHomeAssistantStatus();

This script has been a game-changer for my setup, ensuring uninterrupted functionality regardless of HomeAssistant’s status. I hope this helps others looking to enhance their system’s reliability!