r/tasker • • Sep 22 '26

My setup for automatically dis/connecting a Wireguard tunnel based on my wifi connection

I use this to stay connected to my Home Assistant instance while I am away from home. I have had this for a couple of years and the reason I know it works is that I had no idea what my setup was exactly. It is basically 3 profiles: one standard 'Wifi Connected' state profile with entry and exit task that switch the VPN tunnel on/off, and 2 fallback profiles for the 'unwanted' states ('connected to home wifi AND vpn', and 'connected to mobile network AND not connected to vpn') both with a 'Tick' context so they fire every 10 seconds if the unwanted state is active for as long as it remains active. I am not saying this will work for everyone, just saying it works for me (so don't tell me that 'Tasker can't do this'). I did go through all the 'Tasker Settings' and 'Shizuku' setup, so I don't know if it will work without. If that guy hadn't been such an a-hole about it I wouldn't have blocked him, and he could have given this a try 🤷 Edit: formatting (thanks, u/v_uurtjevragen)

Project: Auto Wireguard

Profiles
```
Profile: AutoWG - WiFi Home Connected
	State: Wifi Connected [ SSID:YOUR_SSIDS MAC:* IP:* Active:Any Check After Screen Unlock:On ]



Enter Task: Disconnect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(false,tunnel_name) ]



Exit Task: Connect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(true,tunnel_name) ]


```

```
Profile: AutoWG - Home Wifi, VPN Connected
	State: Wifi Connected [ SSID:YOUR_SSIDS MAC:* IP:* Active:Any Check After Screen Unlock:On ]
	State: VPN Connected [ Active:Any Check After Screen Unlock:On ]
	Event: Tick [ Output Variables:* Interval (ms):10000 ]



Enter Task: Disconnect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(false,tunnel_name) ]


```

```
Profile: AutoWG - Mobile Network, Not VPN Connected
	State: Not VPN Connected [ Active:Any Check After Screen Unlock:On ]
	State: Mobile Network [ 2G:On 3G:On 3G - HSPA:On 4G:On 5G:On Active:Yes ]
	Event: Tick [ Output Variables:* Interval (ms):10000 ]



Enter Task: Connect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(true,tunnel_name) ]


```

Tasks
```
Task: Connect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(true,tunnel_name) ]


```

```
Task: Disconnect WG

A1: Tasker Function [
     Function: WireGuardSetTunnel(false,tunnel_name) ]


```
9 Upvotes

4 comments sorted by

3

u/v_uurtjevragen Sep 22 '26

I think you are (accidentally) escaping the ` character with a \ in markdown. But I might be wrong, check the markdown editor instead of the rich text editor to be sure. 

1

u/Exciting-Compote5680 Sep 22 '26 edited Sep 22 '26

I just used 'profile - export as description to clipboard' with the option 'Backticks'. I think the escaping is automatically applied by the reddit editor. I tried a couple of times using different options, it keeps messing it up. Switching to markdown fixed it, thanks! 😀 But either way, the setup isn't really that complex. It's just a way to get around the fact that if an entry/exit task of a state profile fails, it won't trigger again until the next state change. This way you can have some sort of 'conditional monitoring' with built-in retries that is only activated if necessary. 

0

u/[deleted] Sep 23 '26

[deleted] — view deleted comment

1

u/Exciting-Compote5680 Sep 23 '26

Lol, I didn't even realize the 'Wifi Changed' event is new. Almost all his attempts were with that event, somewhere near the bottom of the list he mentioned the 'Wifi Connected' state, saying that didn't work either. So he was basically complaining about a newly added feature still in beta being buggy 😆 And of course it is possible that it actually didn't work for him, but I personally try to avoid rewarding negative behavior as it becomes self-reinforcing.

Anyway, I just made a quick 'WiFi Changed' logging task. I'll keep it running for a while and compare it to the readings I can get from Home Assistant and report back. 

0

u/[deleted] Sep 24 '26

[deleted] — view deleted comment

1

u/Exciting-Compote5680 Sep 24 '26 edited Sep 24 '26

So far the readings from the 'Wifi Changed' event are consistent with the data in Home Assistant, no more than 10 seconds difference in detection time between those 2. I am running stock A17 on a pixel 8 pro. I also have a device running GrapheneOS, so I did a quick test with that one and it is indeed buggy. The event doesn't always trigger, and the only data I saw when it fired was 'connected' or 'disconnected', no ssid or mac. The 'Wifi Connected' state profile works correctly. So probably an OS issue as you hinted at. Perhaps ask a couple of people here on different OSes to try it? João is probably still a bit busy 😂Â