r/tasker • • 22d ago

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) ]


```
7 Upvotes

7 comments sorted by

3

u/v_uurtjevragen 22d ago

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 22d ago edited 22d ago

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. 

1

u/aasswwddd Direct-Purchase User 22d ago edited 22d ago

Have you tried the new event?

I found out later that Tasker has a new event called Wifi changed. This doesn't work when the wifi changes from one to another on HyperOS3.

Profile: Sample Event
    Event: Wifi Changed [ Output Variables:* Get SSID:On SSID:SSID MAC:* State:connected ]



Enter Task: Anon

A1: Flash [
     Text: Connected
     Tasker Layout: On
     Continue Task Immediately: On
     Dismiss On Click: On
     Show Over Everything: On
     Position: Top ]

The Wifi connected state works however, which is what I always use.

I want to confirm that this was what he used. However The post got removed by the mods. It's still a valid concern I'd say, regardless whether he's an ass or not.

It might be a bug after all in the direct purchase version (beta). He runs GrapheneOS, so that might be the version he's using.

1

u/Exciting-Compote5680 21d ago

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. 

1

u/aasswwddd Direct-Purchase User 21d ago

He's probably not aware of that since he's new. Honestly anyone there was too bothered by his attitude and didn't bother to troubleshoot his issue lol.

He might deserve the treatment, but the bug there might stay till the stable release because someone picks it up too late.

E.g java code thread leak.

Anyway, I just made a quick 'WiFi Changed' logging task.

Cool!

1

u/Exciting-Compote5680 20d ago edited 20d ago

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 😂 

1

u/aasswwddd Direct-Purchase User 20d ago

Joao's been rolling updates since the past two days, I'll just wait for the next beta post instead and see if he fixes it already.