Skip to main content

QR Codes for Smart Home Device Management with Tailscale

·394 words·2 mins

I have a lot of smart home devices, and not all of them get used all the time. Smart outlets are a good example. I often set them up for a specific short or medium-term task, then unplug them and throw them in a drawer.

When I get that outlet out later, I have to remember which of the unused outlets in my Home Assistant instance is the one I want.

No longer, for I have discovered that my label maker can print QR codes.

H10 Hygrometer

Great, I can print and scan a QR code … but where should it point? I could link directly to the item but there’s two problems with that:

  1. It won’t work if I’m not on the local network
  2. I’d have to reprint QR codes if I change the name of the device in HASS

So, I want to point it to a URL that will work no matter where I am, and I want it to go to something more durable than the name of the device in HASS. I want it to open in the Home Assistant app on my phone, but also work if I put the URL in my laptop computer.

A little Tailscale and Caddy magic to the rescue.

I use simple numbered names for items on their labels. Like H1, H2 for hygrometers, LED1, LED2 for lights, O1, O2 for outlets.

Enter Tailscale’s golink - an internal URL shortener that works across your entire tailnet. With the tailscale iOS app’s “VPN on Demand” feature, it ‘just works’ on my phone, no matter where I am.

I generate a golink URL for each of my outlets, like https://go.tailnet.ts.net/hass-H1. That URL redirects to the Caddy server (which uses a real domain) that I normally use to access Home Assistant, and has the unique identifier for the device in Home Assistant: something like https://hass.hq.1kp.io/config/devices/device/0606b8ec691a3d8566c90d897771fe1a.

Since I’m the only one accessing these QR codes, I can just use Caddy to do a blanket iOS redirect to homeassistant://navigate to open in the Home Assistant app.

...
  @hass host hass.hq.1kp.io
  handle @hass {
          tracing {
                  span hass
          }
          @ios-redirect-to-hass-app `header_regexp('User-Agent', 'iPhone')`
          redir @ios-redirect-to-hass-app homeassistant://navigate{uri} temporary

          reverse_proxy http://192.168.1.252:8123
  }
...

And that’s it! Now I never have to go through my Home Assistant instance to find the device I want. I just scan the QR code, and I’m good to go.