Xiaomi Mi Smart Home Bridge - never goes online by itself


I have a problem with Xiaomi Mi Smart Home Bridge. If for any reason bridge will lose connectivity to WiFi (router restart/lack of power) the bridge stays offline.

If I will disable/enable it everything starts to work normally. Do I need to write a rule to periodically check if it’s offline and try to turn it online or it’s a bug in the bridge and it should try to heal it self after some time?

Did you manage to fix this…

I don’t think it’s an issue in the bridge, all the device in the mi app still works. Interesting enough is that renaming the bridge will bring all the things back online.

Maybe a rule that checks the status of the bridge and when it’s online it renames the bridge with an added increasing number?

didn’t had time to work on that one but it happens every time when I will reboot my router :confused:

I hope that I finally figured out a way to sort this out. The only way that comes to my mind is to restart the whole binding every time that it will go offline.

I used this tutorial to create SSH keys Tutorial: Restart Binding from rule

Here is the rule that checks the status every 10 min

rule "restart mihome binding when thing is offline"
	Time cron "0 0/10 * * * ?"   // every 10 minute
	var Status = getThingStatusInfo("mihome:gateway:7c49ebb19279").getStatus().toString()
	if (Status == "OFFLINE" || Status == "UNINITIALIZED") {
	    logInfo("mihome:gatewayOFFLINE", "mihome:gateway is offline! Restarting bridge!")
		sendNotification("XXXXi@gmail.com", "mihome:gateway is offline! Restarting bridge!" )
		var String result = executeCommandLine("sudo /home/openhabian/restart_mihome.sh", 6000)
		logInfo("result", result)
		logInfo("mihome:binding", "mihome:binding restarted")

Apart from that I have rule that tries to execute the restart immediately after bridge goes offline

rule "XiaomiBridge offline"
    Thing 'mihome:bridge:7c49ebb19279' changed from ONLINE to OFFLINE or
	Thing 'mihome:bridge:7c49ebb19279' changed from ONLINE to UNINITIALIZED
    logInfo("XiaomiBridgeOffline", "XiaomiBridge is offline!")
    sendNotification("XXXXX@gmail.com", "XiaomiBridge is offline!!")
	var String result = executeCommandLine("sudo /home/openhabian/restart_mihome.sh", 6000)
	logInfo("result", result)
	logInfo("mihome:binding", "mihome:binding restarted test")

Now the content of the file for execution is:

sudo -u openhabian ssh -p 8101 -i /home/openhabian/karaf_keys/openhabian.id_rsa openhabian@localhost bundle:restart org.openhab.binding.mihome

I wanted to execute the command directly in the rule but couldn’t figure it out instead
I configured the script to be allowed to be executed without password prompt like in topic below:


sudo visudo -f /etc/sudoers.d/010_pi-nopasswd


openhab ALL=(ALL) NOPASSWD: /path/to/file/to/execute/

in my case

openhab ALL=(ALL) NOPASSWD: /home/openhabian/restart_mihome.sh

What didn’t work

I wanted to execute it as a command directly like here

var String result = executeCommandLine("sudo -u openhabian ssh -p 8101 -i /home/openhabian/karaf_keys/openhabian.id_rsa -t openhabian@localhost bundle:restart org.openhab.binding.mihome", 6000)

But I got error like that one

“Sorry, user openhab is not allowed to execute ‘/usr/bin/ssh -p 8101 -i /home/openhabian/karaf_keys/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.mihome’ as openhab on openHABianPi.”

I tied to configure ssh as command but it didn’t worked.

Second approach was something like this one

var String result = executeCommandLine("sudo openhab-cli console bundle:restart org.openhab.binding.mihome", 6000)


We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.

#2) Think before you type.

#3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

It would be far easier to figure this out so files wouldn’t be needed.

Your second attempt looks like you did not execute

sudo -u openhab sudo -l

In the command line after setting the rights or did you?

Thanks for the rule. It’s currently in use and works really well. I updated the scripts that is uses the REST API this way it doesn’t require SSH.

This is how my restart_mihome.sh looks:

curl -X PUT --header “Content-Type: application/json” --header “Accept: application/json” -d “{ “label”: “Xiaomi Zolder Bridge - $RANDOM”}” “http://openhabianpi:8080/rest/things/mihome%3Abridge%3Ac65a30ee
curl -X PUT --header “Content-Type: application/json” --header “Accept: application/json” -d “{ “label”: “Xiaomi Beneden Bridge - $RANDOM”}” “http://openhabianpi:8080/rest/things/mihome%3Abridge%3Ad85d3961

As you can see I have two bridges, the script does a webrequest to the bridges and adds an Random number to the name forcing the restart.

Make sure you replace the ID of your bridge in the URL :slight_smile:

1 Like