Tasker - turn off wifi IF there's no internet connection
You can create a task to ping Google DNS as follows:

Task → Code  → Run Shell

Command: ping -c 2 8.8.8.8 [Store Output In: %stdout] [Continue Task After Error: checked]


  If %stdout ~ 100% packet loss
  
  google servers unreachable
  
  Else
  
  connectivity established
  
  End If




Add this to your main task with either keeps wi-fi conection or switches it off.

Credits

Run shell 'ping' with tasker