Cellular network backup on hassio

The primary network for my hassio system is a hardwired ethernet connection. I’ve added a second NIC to my platform that I’m looking to connect to a cellular interface device. Ideally I’d ping something over the primary interface. If the ping fails then I’d change the default route over to the cellular network. I’d continue to attempt to ping out the hardware ethernet and once the ping was successful I’d change the default route back. The normal version of ping has a -I option so you can specify the interface. hassio’s version of ping doesn’t support this. Commands like “ip route” or route don’t work on hassio to get the router table. If someone could provide me some information on how to work on the hassio base distribution I’d consider looking into this.

This can be done automatically by half decent routers (e.g. EdgeRouter X, $59 USD).

Most of these devices assume you put them at the edge of your home network. Which means if power is cut than any thing between the alarm and the edge router has to be on a UPS. I want one additional low power routing device that supports both ethernet connectivity to my home network and has integrated cellular. So the device would sit between the hassio box and my home network. The big problem I’ve seen with these devices is that they do not allow you to have independent filter rules for each path. As my home network is secured, I want full access from my LAN to the hassio box, so basically all income traffic would be allow from the LAN. If power is pulled on the house or the internet service is cut then I want the hassio box to be the only user of the cellular and I don’t want any port forwarding to it, as I’ll use the hassio cloud to gain access. Looking at the document provided for the router you identified it has this limitation. Port forwarding rules apply to all paths. It also appears I’d need a third box to get the cellular integration. If hassio supported and controlled the multipath I’d ensure the cellular network only had the traffic I desired. The added device would simply be a cellular interface. In a best case situation that backup path would be a USB device I could plug into the hassio box to limit the power requirement for the UPS. If you had the option as I suggested, for easy integration I could even use an old cell phone with tethering as the backup path.

Yeah you’re right, the port forwarding rules do apply to both WAN ports. An interface box isn’t required if you use something like the Netgear LB2120. It’s what I use. Not as cheap as an old cell phone or USB stick modem though.

You’re probably aware that most cellular networks use CGNAT. I think Nabu Casa can work around this but have not tried.

So what is required on hassio to make this mostly work would be a version of ping that includes the interface parameter to control the interface the ICMP packet goes out over, the ifmetric tool to switch the cost assigned to each interface, whatever magic is required to tie into the docker container and a simply script to ping some address that if it fails will result in a switch of the metric value. I’ve done a test using just linux and this method seems to work fine. I’m going to load home assistant supervised onto raspbian and see if I can make it work. Once it’s working it would be great to do it in the hassio release.

Spent some time loading home assistant supervised so I could check this out on a generic version of linux. I used ubuntu 18.04 for odriod xu4. The following python script does exactly what is required on a standard linux distribution. The default route switches and the home assistant container traffic switches paths. I only tested with pings from the container. At some point I’ll test with the home assistant cloud.

#!/usr/bin/python3
import os
import time

primeNIC = "eth0"
backupNIC = "wlan0"

testHost = "8.8.8.8" 
onbackup = False

lowMetric = "50"
highMetric = "200"


os.system("ifmetric " + primeNIC + " " + lowMetric)
os.system("ifmetric " + backupNIC + " " + highMetric)

while True:
    response = os.system("ping -I " + primeNIC + " -c 1 " + testHost)
    # and then check the response...
    if response == 0:
        pingstatus = "Network Active"
        print("Status "+ pingstatus)
        if onbackup == True:
            print("Switching back to prime network!!")
            os.system("ifmetric " + primeNIC + " " + lowMetric)
            os.system("ifmetric " + backupNIC + " " + highMetric)
            onbackup = False
    else:
        pingstatus = "Network Error"
        print("Status "+ pingstatus)
        if onbackup == False:
            print("Switching backup network!!")
            os.system("ifmetric " + primeNIC + " " + highMetric)
            os.system("ifmetric " + backupNIC + " " + lowMetric)
            onbackup = True

    time.sleep( 10 )