Solution for changing wifi connection (for HA in docker)

I have home assistant in docker, in raspberry Pi. Whenever I change the wifi, I can’t connect through Nabu Casa cloud, and I get this in HA logs:

ERROR (MainThread) [snitun.multiplexer.core] Timeout error while pinging peer.

and I can’t connect. Even if I stay on the same wifi and the internet drops, I can’t connect anymore unless I restart.

I need to have a second backup for wifi so it changes when I lose connection.

It’s a server application, connect the Raspberry PI to your router with an Ethernet cable.

1 Like

Can you explain why you change the WiFi?

I have to agree. If you want more reliable connectivity, connect via a physical Ethernet cable. Wireless has a lot to it in order for it to be reliable. Most home networks are not up to par.

the problem is not in the wifi connection, it is in the ISP. It goes down sometimes. HA, as a server application, should recover

HA is a server, and does not like WIFI at all. Switching IP addresses is a even bigger problem.
You have to use the same IP address for all the networks. That means any routers have to have the same addresses for DHCP.

unfortunately not possible in my setup, I was looking for a solution to circunvent that. Like, what is the real issue? Because sometimes it works, sometimes not.

Also there is an issue where if my modem goes down (but not the router), then when it gets back I can’t ever access HA until I restart the container. Looks like the same issue.

I never let the ISP’s equipment touch my network. I have another firewall/router between. The WAN goes to the ISP’s device, the LAN goes to my equipment.
I always have the same network IP addresses as a result. This has worked for me with 4 different ISP’s.

1 Like

There are integrations that can show you the public IP of your HA server and you can then probably make an automation to restart the NabuCasa connection.

When the internet is down, then NabuCasa will not be available, of course, but the local address (typically home assistant.local) should be.
There can be a little delay though, due to the need for services to announce themself to the .local network.

Try and describe or draw out your network layout. Ideally you would have your ISP connected to a modem which is the connected to a router. The router is configured to router between WAN and LAN, so that if the ISP side (or modem) go down there is no affect on Home Assistant except that it cannot reach the Internet and hence any Integrations that rely on Cloud Polling and Nabu Casa will stop working.

If you are finding your ISP unreliable, then you should look at a Router which supports Multiple WAN connections and have that handle a backup connection. This means having 2 connections to the Internet. The key here is that router handles the IP changes on the WAN side and your LAN IP’s do not change.

How Home Assistant is configured will also play a part, although you mention Nabu Casa which does not open any connections to your Home Assistant server (your Home Assistant server connects to Nabu Casa). So, if your Raspberry PI changes IP address, your Home Assistant server has to open a connection to Nabu Casa. As WallyR mentions, you can use an automation which checks your public IP address and restarts the Nabu Casa Integration when the IP changes.

thanks, this gives a possible solution, I didn’t know I could restart just the nabu casa integration. I am now writing the current wifi to a file and then watching it through HA, and when it changes I restart HA Cloud.

It looks like it is working for 2 wifis. It restarts and I can connect after some seconds. But for a third wifi connection I tried it keeps giving these:

ERROR (MainThread) [hass nabucasa.remote] Connection problem to snitun server (Can't us-east-1-19.u1.nabu.casa:443 w1th: [Errno -3] Try again)

Also I can’t even ping from outside the container on the host raspberry pi:

lz@raspberrypi:~ S ping us-east-1-19.ui.nabu.casa 
PING us-east-1-19.ui.nabu.casa (64:ff9b::81d4:c513) 56 data bytes

it hangs. Is there some kind of whitelisting?

update: ping does not work on any wifi, I think this server simply does not answer pings. Still the problem that I can’t connect to us-east-1-19.ui.nabu.casa:443 remains only on this third wifi.

ok, it seems like restarting the nabu casa integration works, but sometimes it fails and I need to wait a bit and then restart again. I was trying to find a way to accurately monitor if the HA server is reachable from outside, but I couldn t.

The Remote UI entity always gets connected right away, no matter what, even if the server is not reachable from outside.

Also pinging does not solve the issue, because the container can ping, it just gets stuck somehow on the old connection.

I couldn’t find any entity that monitors this state, or something related that can be used. Any ideas?

If you have router access then you might be able to get the default gateway of the WAN interface and then ping that.
It will typically be the first hop inside your ISPs network.

the problem is not the gateway. I mean, it is one of the problems. But just switching from one network to another, even if both networks are working, can lead to it permanently lose connection. I waited for 10 hours and it won’t connected, then I restarted the HA cloud integration and it connected. It keeps locked to some old connection. Problem is that sometimes the cloud integration restart does not work and I need to do it again but only after some 1 or 2 minutes passed, not right away after a new connection.

If there were a way to monitor the cloud connection from other devices I’d be able to know if the server is truly reachable or not

I’m still trying to understand why you are changing WiFi networks. If it that your router/modem’ connect via WiFi and that is changing from one connection to another? or is it your Home Assistant server switching from one WiFi access point to another?

As I said, try and describe you network layout or just draw a picture and upload. We will be able to help more if we can understand the layout and potentially the issue you are trying to over-come.

The idea was to get the wan side’s default gateway and then ping that.

I think a 2 minutes delay before restarting the NabuCasa Cloud client would be the easiest.

I have a wifi router with gateway at 192.168.1.1, connected to a starlink. The other one is a 4G router with gateway at 192.168.0.1. All standard home networking and subnet /24.

The part that switches the wifi connections is the raspberry pi host, and the HA server is on docker compose, network non-host mode.

My current solution is to have a webhook and call it from HA itself, and the webhook updates a timer. If the timer reaches the end, it restarts the cloud integration.

- alias: GET webhook nabu casa monitor
  description: ""

  triggers:
    - trigger: time_pattern
      seconds: /30
  conditions: []
  actions:
    - action: rest_command.webhook_monitor_nabu_casa
      metadata: {}
      data: {}

  mode: single

- alias: restart when nabu casa inactive for x minutes
  description: ""

  triggers:
    - trigger: numeric_state
      entity_id:
        - timer.inactive_webhook_self_connection

      attribute: duration
      above: 5
      enabled: false

    - trigger: state
      entity_id:
        - timer.inactive_webhook_self_connection

      to:
        - idle

      from:
        - active

  conditions: []

  actions:

    - action: timer.start
      metadata: {}
      target:
        entity_id: timer.inactive_webhook_self_connection

      data:
        duration:
          hours: 0
          minutes: 0
          seconds: 0

      enabled: false

    - action: cloud.remote_disconnect
      metadata: {}
      data: {}

    - delay:
        hours: 0
        minutes: 0
        seconds: 15
        milliseconds: 0

    - action: cloud.remote_connect
      metadata: {}
      data: {}

    - action: persistent_notification.create

      metadata: {}
      data:
        message: restarted home assistant cloud connection!

    - action: timer.start
      data: {}

  mode: single

- alias: webhook monitor nabu cloud
  description: ""

  triggers:

    - trigger: webhook
      allowed_methods:
        - POST
        - PUT
        - GET

      local_only: false
      webhook_id: "-..."

  conditions: []
  actions:
    - action: timer.start
      target:
        entity_id: timer.inactive_webhook_self_connection
      data:
        duration:
          hours: 0
          minutes: 2
          seconds: 0

  mode: single

I would actually suggest you look at using a device capable of Multi-WAN (maybe OpenWRT with MWAN3 on the Raspberry Pi).

I know this will introduce Double NAT, but I personally have never seen this be a problem. The benefit is that you put your internal network on it’s own range eg. 192.168.100.1/24 and the Multi-WAN router takes care of which Internet connection is being used. You could even have both on at the same time although that won’t mean you get Starlink speed + 4G speed as it won’t aggregate, however, if you use torrent which opens multiple connections you will get the file faster as bits will download over both links.

I would add here, that this may not directly resolve your issue of losing a connection to Nabu Casa, since it would depend on how the Multi-WAN router handles connection states. However, you should be able to get the router to tell Home Assistant when a link isn’t available and that in turn triggers Home Assistant to restart the Nabu Casa connection.

I would say the web hook workaround is reasonably elegant given your current situation.

thanks! I don’t want to use multi wan gateway because just some minutes on the 4G router would spend all the data limits, and other reasons.

I also believe the Nabu Casa Integration would still hang due to sudden connection drops/route changes. I believe Id still need the restart solution.

At least I learned that I can restart just this integration, and my solution seems to work. HHoweve I think this restart should be automatically handled in the integration code.