Shelly Fallback When Home Assistant Is Down

Overview

A clever but more costly way to manage having manual light switches with always on smart bulbs was to install a relay to allow the switch to be “detached” from the physical power so that the power remains on for the bulb and use automations to simulate a switch being pressed as detailed in Smart Bulbs With Light Switches

Another thought was what if Home Assistant went down? It would render the switch not operating because the automation to simulate the switch turning off the light is not working. Here comes Shelly scripts to fall back to similar to a traditional switch.

Requirements

Not all Shelly’s have this feature so please check your device. Fortunately, all my light relays are using the “plus” models or generation 2 relays.

I also found the mobile app to not work 100% with the scripting feature. I was able to create and start the script however it was not able to display the script contents. I would recommend using the web browser to do this instead and this post will be using the web version as the example.

Creating Script

To get to the script section:

  1. Using a web browser, put the IP address. This will load the web interface for your Shelly.
  2. Go to Scripts on the left hand navigation.
  3. Press Create script button.
  4. Give your script a name. In my example, it’s called HaWatchdog.
  5. Copy the script from below. Make sure you change the script to your setup.
  6. Save the script
  7. Turn on Run on startup. If this is not done, the script will not restart when it restarts or if there’s a power cut.
  8. Press the triangle play button to start the script.

Script

This example comes from the Home Assistant Community forum.

Replace [home assistant address] with your Home Assistant address. Port can also changed if you have a non standard setup.

It will check if Home Assistant is responding with the correct status code (or any if at all) every 30 seconds. This timing can be adjusted in the Timer.set() on the last line.

function testHA() { Shelly.call("HTTP.GET", {url: 'http://[home assistant address]:8123', body:{}, ssl_ca: '*'}, function(resp) {
let mode = (resp && resp.code === 200)? "detached" : "flip";
Shelly.call("Switch.GetConfig", {id: 0}, function(oldConfig) {
if (oldConfig.in_mode !== mode) {
Shelly.call("Switch.SetConfig", { id: 0, config: { in_mode: mode } });
if (mode === "detached") Shelly.call("Switch.Set", { id: 0, on: true });
}
});
})
}

Timer.set((30 * 1000), true, testHA);

The script will change the switch from detached to edge. You can change it to follow by replacing “flip” to “follow” so the new line would be:
function testHA() { Shelly.call("HTTP.GET", {url: 'http://[home assistant address]:8123', body:{}, ssl_ca: '*'}, function(resp) {
let mode = (resp && resp.code === 200)? "detached" : "follow";
Shelly.call("Switch.GetConfig", {id: 0}, function(oldConfig) {
if (oldConfig.in_mode !== mode) {
Shelly.call("Switch.SetConfig", { id: 0, config: { in_mode: mode } });
if (mode === "detached") Shelly.call("Switch.Set", { id: 0, on: true });
}
});
})
}

Timer.set((30 * 1000), true, testHA);

More details on what this means can be found here. In basic terms, edge will turn the light on or off when you press it. follow/toggle will turn on when the switch is on the on position only and vice versa.

Test

To see this in action, make a note of the current input mode:

  1. On the Home page, click on the Input (0)
  2. Under Input settings, go to Input/Output settings.

The setting should be on Detached if Home Assistant is up. To test this, block access or change the IP address in the script to one where no device exists and it should change it to Edge by checking this page.

Summary

This is a great way to fall back if something goes wrong and thanks to the people on the Home Assistant forum for the script.

Control smart lights with Shelly with automated detached mode

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Home Automation. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.