Wireguard and Tasker Part 2: The 24h reconnection timeout problem

In my previous Tutorial I showed you, how I'm using Tasker to automatically connect to my WireGuard VPN server, when I'm leaving my home-network.

And this setup is working pretty good. But unfortunately there is one thing which always bothered me: As my WireGuard Server is on my Home-Network the 24h reconnection of my dial-up connection leads into a timeout.

If the dial-up connection at home gets a new IP address (in most cases usually at night) and I'm connected to WireGuard, the new IP-Address does not get automatically advertised to WireGuard, even tho the DynDNS is resolving to the new IP,  the connection only seems active for Android but in fact you are offline.

And that's kinda annoying, especially if you are not at home during night, for example if you are on vacation somewhere else and you want to use wireguard continuously.

What can I do?

Solution: A time-based state event

Well, my solution for this is kinda easy. My router controls the auto-disconnect at arround 5:00am every day. So I want to use a time state event to reconnect to my VPN if its necessary.

Here is the plan:

On a WIFI Not on a WIFI 5.30am reconnect VPN disconnected
if it's not Home's SSID -> VPN -> VPN disconnect VPN at 5:30 am Reconnect instantly to the VPN if SSID is not Home-Wifi

How can I achieve this with Tasker?

I decided to completely re-do my Tasker-Setup for WireGuard, as I think this new way is not that complicated to build.

1.) Set the SSID into a variable:

First I want that Tasker fills in the current connected SSID into a variable.

Add the Profile:

State -> Net -> Wifi Connected

As I want Tasker to do the following Task for every Wifi-Connection I leave all input fields blank.

Add the Task "StoreWifiInfo" to the Profile:

Net -> Test Net -> Type: Wifi SSID -> Store Result In: %CurrentSSID

I activate the profile and test if it works: I disconnect my Wifi-Connection and reconnect it. In Tasker I tap on the VARS tab and see if %CurrentSSID is filled with the SSID of the network I'm currently connected to. If it works I tap back to the PROFILES tab.

Now I want Tasker to set %CurrentSSID to "none" if my phone is not connected to a Wifi.

Add the Profile:

State -> Net -> Wifi Connected -> Invert: ✓

I want the State "Not Wifi Connected", therefore I hit the checkmark on the Invert section of the Wifi Connected State.

Add the Task "ResetWifiInfo":

Variables -> Variable Set -> Name: %CurrentSSID -> To: none

So, I can activate this Profile and test again. Once I disconnect my phone from my current Wifi the variable %CurrentSSID should get set to "none" under the VARS tab.

2.) Connect/disconnect to Wireguard automatically

I want, as in my first Tutorial, that Tasker automatically connects my phone to the WireGuard VPN if my connected Wifi's SSID is not my Home-Wifi's SSID.

Add the Profile:

State -> Variables -> Variable Value -> Conditions: %CurrentSSID doesn't match HOMESSID

HOMESSID is the name of your Wifi's SSID.

Add Task "ConnectVPN":

Tasker -> Tasker Function-> WireGuard Set Tunnel -> Tunnel Up: YES -> Tunnel Name: Home

Home should be the name of your WireGuard Profile.

What I just did: If %CurrentSSID is not my Home-Wifi's SSID, Tasker connects to my WireGuard VPN.

Now I need to add a profile for the disconnection of WireGuard once I reach my Home-Wifi.

Add the Profile:

State -> Variables -> Variable Value -> Conditions: %CurrentSSID match HOMESSID

and the Task "DisconnectVPN":

Tasker -> Tasker Function-> WireGuard Set Tunnel -> Tunnel Up: No -> Tunnel Name: Home

Now it's time to test the new Profiles. Turn Wifi on/off and see if Tasker connects/disconnects your WireGuard VPN.

3.) The time-based reconnection

My router handles the auto-disconnect by itself at arround 5:00am. So my idea is to disconnect the VPN at 5:30am so I am offline only to a maximum of 30minutes.

Add the Profile:

Time -> FROM: 05.30 -> (EVERY: blank) -> TO: 05:30

It looks like this:

the Time State

I now use the Task "DisconnectVPN" which I have already created at Step 2.

Okay so when it's 05:30am Tasker will disconnect my phone from my WireGuard VPN. Now I need the last Profile: The Reconnect

Add the Profile:

State -> Net -> VPN Connected -> Invert: ✓

So now I have the profile "Not VPN Connected"

Add the Task "ReconnectVPN":

Task -> Wait -> Seconds: 5
Task -> If -> Condition: %CurrentSSID doesn't match HOMESSID

I end the If task by just selecting "If" on the next dialogue. I don't want an else task or something. On the right side of this If state there is an arrow, I make sure it's expanded to add the next step into the IF section:

Tasker -> Tasker Function-> WireGuard Set Tunnel -> Tunnel Up: YES -> Tunnel Name: Home

It should look like this:

the ReconnectVPN Task

So if Tasker recognizes a disconnection of the VPN (which will be triggered by the 05:30 event) it will wait 5 seconds and after that it will check if the %CurrentSSID is not my Home-Wifi's SSID to reconnect to the WireGuard VPN. The other State of a VPN disconnection is if I reach my Home-Network so this Profile will let the VPN disconnected.


How I solved my 24h reconnection timeout problem with WireGuard:

I set a time-based Task which disconnects the WireGuard VPN right after my dial-up connection (also my WireGuard Server's connection) got reconnected with a new IP-Address. Tasker will now check if I'm at Home or not and if not, it reconnects to my WireGuard VPN. This Stack of Profiles lead me into a state of an (kinda) always-on VPN when I'm not at home for a longer time without any timeout problems.

There might be better options to solve this problem, but this is my way of and it works pretty good for me.

And maybe this helped or inspired one of you to solve this timout issue as well.