I have been experimenting with the feasibility of using Shelly Gen4 relays as Zigbee light switches. I want to replace my current system which uses repurposed circuit boards from Ikea smart sockets and is getting unreliable and suffers from the usual problem that local switching is not integrated with HA. It has been a struggle (headline is: great hardware, pity about the software and firmware!), but I think I finally have a workable solution.
The first problem I encountered with the Shelly 1 Mini Gen 4 is that the web interfaces currently do not work with Safari browsers or any browser on iPhone or iPad. I am using Chrome on my Mac. Note that the Shelly App works fine on iPhone, just not the web interface. Shelly support have acknowledged this and are working on a solution.
Second problem is that the printed leaflet that comes with the device seems to be highly inaccurate in its description of the software operation. In particular the device ships with firmware that supports Matter but not Zigbee. The instructions say to press the reset button five times rapidly to switch to Zigbee firmware. There is no feedback (just a single red LED) so this would be very hit and miss, but I never made it work at all. The way that worked for me was to first get the device on the WiFi network using the iPhone App and Bluetooth method and then connect to the devices web interface using Chrome on my Mac. This gets you to the next roadblock! There SHOULD be an item on the Settings menu ‘Switch to Zigbee’ which will download and install the Zigbee firmware. But some of the time I found this was missing and instead there was an item ‘Switch to alternative firmware’ (nice British English there!) which leads to a page saying ‘No alternative firmware available’. If this happens the only way I found forward was to do a factory reset (hold the reset button for 10 seconds and then release it) and then go through the WiFi initialisation on the iPhone again.
Once on the Zigbee firmware, the WiFi interfaces all still work, except for Matter. Note that a factory reset will not return to the Matter firmware, it just resets the Zigbee firmware to the condition immediately after installation.
I was then able to pair the device with Zigbee 2 MQTT (Z2M). You are supposed to be able to put it in pairing mode by pressing the reset button three times in rapid succession. Again, I was unable to get this to work. What worked was a factory reset which leaves the device in pairing mode for a few minutes, so be ready to pair it as soon as it comes back up. You can do this before reentering the WiFi credentials and returning to the web interface.
Like a mountaineer, you crest one false summit only to be faced with the next! The whole point of the Zigbee exercise was to get the device off my over-congested WiFi. In the Web interface I can simply disable WiFi in settings and reboot. But then there is no way back in except for yet another factory reset and I am getting damned fed up typing that WiFi password! This sent me down the rabbit-hole of scripting, which is very comprehensive, but poorly documented and based on the second worst programming language of all time, JavaScript!
I want to use the ‘retractive’ (or as real electronics guys call them ‘momentary action’) switch approach as this enables hardware control of the lights properly integrated with HA control. Having connected the device with a retractive switch connecting Live to the SW input, the next step is to click on the Home, Input (0), Input/Output path. Then change the Input Mode from ‘Switch’ to ‘Button’, the Output Type to ‘Detached’ and the Action on power on to ‘Turn OFF’. Press ‘Save settings’.
Next go to the ‘Scripts’ section and create a script ‘ZigbeeLightSwitch’ with the following content:
var DELAY = 4000; // Reboot delay in ms - empirically determined
function OnState(j) {
if (j["sta"]["enable"]) {return;}
Shelly.call("Wifi.SetConfig", {"config": {"sta": {"enable": true}}});
Shelly.call("Shelly.Reboot", {'delay_ms': DELAY});
}
Shelly.addEventHandler(function(e) {
if (e.component === "input:0") {
if (e.info.event === "single_push") {
Shelly.call("Switch.toggle", {'id': 0});
}
if (e.info.event === "long_push") {
Shelly.call("WiFi.GetConfig", {}, OnState);
}
if (e.info.event === "double_push") {
Shelly.call("Switch.set", {'id': 0, 'on': false});
}
}
});
Save this script, set it to Run on startup and start it immediately. At this point, a single-push of the switch should toggle the relay (on if it is off, off if it is on). I have also programmed a double-push to switch the relay off unconditionally in case you cannot see the lights from the switch position (and because I can!).
Now create a second script ‘Zigbee Reboot’ (the names do not actually matter) and enter the following content:
var TIMEOUT = 10; // Timeout in minutes
var DELAY = 4000; // Reboot delay in ms - empirically determined
function OnReboot() {
Shelly.call("Wifi.SetConfig", {"config": {"sta": {"enable": false}}});
Shelly.call("Shelly.Reboot", {'delay_ms': DELAY});
}
function OnState(j) {
if (j["sta"]["enable"]) {
Timer.set(TIMEOUT * 60000, false, OnReboot);
}
}
Shelly.call("WiFi.GetConfig", {}, OnState);
Save this and also set it to ‘Run on startup’, but DO NOT start it until you are finished configuring the device.
The idea is that the ZigbeeReboot script will set WiFi off in the configuration and reboot the device which will then not be on the network. This occurs ten minutes after a reset, or after the script is started manually. If you need more that ten minutes in configuration, or to leave the WiFi permanently on, just stop the ZigbeeReboot script.
When off the network, a long-push of the retractive switch will restore network access using the previously stored WiFi SSID and password, but this only lasts ten minutes in case the long-push is accidental or malicious (unless you go into the Web interface and stop the ZigbeeReboot script).
In HA, there is an entity for the relay and the state of this entity follows the relay even when it is switched using the retractive switch and the retractive switch also continues to work even if the Zigbee network or HA is down. The relay can also be switched by automations in HA.
This technique of putting the switch in ‘detached’ mode and then re-attaching local relay control using a script is even more useful when the control is over WiFi to the Shelly Connector in HA. Putting the switch in detached mode causes switch actions to be reported as a HA entity. We can attach some of the switch actions locally, so they work even if HA is down, whilst using other actions to control other stuff through HA.
Hopefully this is useful to someone, that it will encourage others to suggest improvements and report their findings, and that it may even encourage Shelly to up their game on software and documentation to the same high level as their hardware designs!