Automate Tailscale connection with home assistant

Not a blueprint but though I’d share my automations on how you can control your tailscale VPN connection on your phone.
There is a current limitation that the Tailscale app has to be open in the background for the VPN to turn on. Fixed by @fantangelo.
Current limitations:

  1. There is a bug that once that connection is up it sometimes can only be diabled by the automation/device restart, not manually stopping the connection: If android intent is triggered more than once, a connecting loop is triggered and you can't disconnect · Issue #8013 · tailscale/tailscale · GitHub
  2. It seems to inly run when the device first ‘wakes’
  3. When it runs it opens the tailscale app
alias: "[phone] Tailscale when away from home wifi"
description: >-
  This automation enables tailscale when away from home wifi and disables it
  when at home.
trigger:
  - platform: state
    entity_id:
      - device_tracker.pixel_3a
    to: not_home
    id: away
  - platform: state
    entity_id:
      - device_tracker.pixel_3a
    from: not_home
    to: home
    id: home
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: away
        sequence:
          - service: notify.mobile_app_pixel_3a
            data:
              message: command_launch_app
              data:
                package_name: com.tailscale.ipn
            alias: Ensure App running
          - service: notify.mobile_app_pixel_3a
            data:
              message: command_broadcast_intent
              data:
                intent_package_name: com.tailscale.ipn
                intent_action: com.tailscale.ipn.CONNECT_VPN
            alias: Enable VPN
      - conditions:
          - condition: trigger
            id: home
        sequence:
          - service: notify.mobile_app_pixel_3a
            data:
              message: command_broadcast_intent
              data:
                intent_package_name: com.tailscale.ipn
                intent_action: com.tailscale.ipn.DISCONNECT_VPN
            alias: Disconnect tailscale
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: notify.mobile_app_pixel_3a
            data:
              message: command_broadcast_intent
              data:
                intent_package_name: com.tailscale.ipn
                intent_action: com.tailscale.ipn.DISCONNECT_VPN
            alias: Ensure tailscale disconnected
mode: single

Hi does this also work for an iPhone?

Thanks for sharing this. I added this to ensure that the tailscale is open first.

service: notify.mobile_app_pixel_3a
data:
  message: command_launch_app
  data:
    package_name: com.tailscale.ipn
1 Like

I could be completely wrong here but I thought the Tailscale app simply allowed local traffic to stay local if you were on your home network.

If I am right then what is the benefit of turning it on and off?

If I am wrong, please correct me!

Not BP related at all, as you stated. Perhaps somewhere in the configuration area?
If the point is others would be interested, you should add it to all like all
20 threads…

works a treat, updated to include your suggestion and consolidated to a single automation with 2 different triggers.
Would be good if there was an intent command to open the app in the background

1 Like

That may well be that case, but I tend to use my home as the exit node so when I am within my home network I get weird hair-pining issues