Tech Expert & Vibe Coder

With 15+ years of experience, I specialize in self-hosting, AI automation, and Vibe Coding – building applications using AI-powered tools like Google Antigravity, Dyad, and Cline. From homelabs to enterprise solutions.

setting up geofence-based home automation with owntracks and n8n: triggering server wake-on-lan and service scaling

Setting Up Geofence-Based Home Automation with OwnTracks and n8n

I’ve been running a self-hosted home automation setup for years, and one of the most useful features I’ve implemented is geofence-based automation. This allows me to trigger actions like waking up my server or scaling services based on my location. Here’s how I set it up using OwnTracks for location tracking and n8n for workflow automation.

Why I Needed This

I run a Proxmox server with several services that I only need when I’m home. Previously, I had to manually wake up the server and start services when I arrived, which was inconvenient. I wanted a system that would automatically:

  • Wake up my server via Wake-on-LAN when I’m within 500 meters of home
  • Start scaling up my services when I’m within 100 meters
  • Scale down services when I leave the defined zone

My Setup

Here’s what I’m using:

  • OwnTracks – For location tracking (iOS app)
  • MQTT Broker (Mosquitto) – Running as a Docker container on my server
  • n8n – For workflow automation (also running in Docker)
  • Proxmox – My home server running VMs and containers
  • Home Assistant – For device tracking (optional, but useful for visualizing)

What Worked

1. Setting Up OwnTracks

I installed the OwnTracks app on my iPhone and configured it to connect to my local MQTT broker:

  • Server: mqtt://192.168.1.100:1883 (my server’s IP)
  • Username/Password: Credentials for my MQTT user
  • Device ID: vipin_iphone
  • Tracker ID: vipin
  • Topic: owntracks/vipin/vipin_iphone

I set the monitoring mode to “Significant Changes” to balance accuracy and battery life, with a minimum displacement of 50 meters and interval of 60 seconds.

2. Configuring n8n Workflow

Here’s my n8n workflow that listens for MQTT messages and triggers actions:

  1. MQTT Trigger Node: Subscribes to owntracks/# topic
  2. Function Node: Parses the incoming message to extract latitude, longitude, and accuracy
  3. HTTP Request Node: Queries my GeoJSON file to check if the location is within my home zone
  4. Condition Node:
    • If within 500m: Send Wake-on-LAN packet
    • If within 100m: Start scaling up services
    • If outside: Scale down services
  5. Terminal Node: Logs the action taken

Here’s a simplified version of the Function Node code:

// Calculate distance from home (34.052235, -118.243683)
const homeLat = 34.052235;
const homeLon = -118.243683;
const earthRadius = 6371000; // meters

function getDistanceFromLatLonInMtr(lat1, lon1, lat2, lon2) {
  const dLat = (lat2 - lat1) * Math.PI / 180;
  const dLon = (lon2 - lon1) * Math.PI / 180;
  const a =
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  return earthRadius * c;
}

const distance = getDistanceFromLatLonInMtr(
  $input.data.latitude,
  $input.data.longitude,
  homeLat,
  homeLon
);

// Output the distance
return { distance: distance };

3. Wake-on-LAN and Service Scaling

For Wake-on-LAN, I used the wakeonlan command in a Docker container:

docker run --rm --net=host jamesclemson/wake-on-lan wake 18:1E:24:00:11:22

For service scaling, I used the Proxmox API to adjust HAproxy load balancer settings, increasing backend servers when I’m close to home.

What Didn’t Work

1. Battery Drain

Initially, I had the OwnTracks app set to “Move” monitoring with a 30-second interval, which killed my battery. Switching to “Significant Changes” with a 60-second interval helped, but I still saw about 5-10% extra daily battery usage.

2. False Positives

Sometimes my GPS would give inaccurate readings when indoors, causing the automation to trigger incorrectly. I mitigated this by:

  • Adding an accuracy threshold (ignoring updates with accuracy > 50m)
  • Adding a delay of 2 minutes before acting on a zone change

3. n8n Workflow Complexity

My initial workflow was too complex, trying to do everything in one flow. I ended up breaking it into smaller workflows:

  • One for location updates
  • One for Wake-on-LAN
  • One for service scaling

Key Takeaways

  • Start simple – Build basic functionality first, then add complexity
  • Balance accuracy and battery – “Significant Changes” mode is a good compromise
  • Add safeguards – Accuracy thresholds and delays help prevent false triggers
  • Break down workflows – Smaller, focused workflows are easier to maintain
  • Monitor your system – Set up logging to track when actions are triggered

This setup has been running reliably for about 6 months now. The most valuable part is not having to manually wake up my server when I arrive home – it just works. The service scaling is nice too, as it saves resources when I’m not using them.

One thing I’m still working on is adding support for multiple users (family members). That will require some changes to the workflow to handle multiple trackers and different zones.

Leave a Comment

Your email address will not be published. Required fields are marked *