Mastering Network Presence: Using Ping and Nmap for Home Assistant Presence Detection

0
0
  • #automation
6m read

Accurate presence detection is a cornerstone of any truly smart home. Knowing whether someone is home, arriving, or leaving allows your automations to react intelligently – turning on lights, adjusting thermostats, arming alarms, and more. While GPS-based methods are common, leveraging your home network offers a robust, local, and often more granular way to track devices.

In this guide, we'll dive into two powerful, built-in Home Assistant integrations for network scanning: !$0$! and !$1$!. We'll cover how to set them up, integrate your devices, and implement best practices to build a reliable network-based presence detection system.

Why Network Presence?

Network presence detection works by periodically checking if a specific device (identified by its MAC address or IP address) is connected to your local network. This is particularly useful for:

  • Tracking phones, tablets, and laptops that are connected to WiFi.
  • Monitoring static devices like game consoles, smart TVs, or even other smart home hubs that are always connected.
  • Creating a local, privacy-respecting presence solution that doesn't rely on external services or GPS data.

While it has limitations (like devices disconnecting to save power), combining it with other methods (like companion app location, Bluetooth, or door sensors) can create a highly accurate picture of presence.

Method 1: The Ping Integration

The !$2$! integration is one of the simplest ways to check if a device is reachable on your network. It sends ICMP packets to the target IP address or hostname and reports the round-trip time and success rate. For presence detection, Home Assistant simply uses this to determine if the device responded (is 'home') or not (is 'away').

Setup Steps (Configuration.yaml)

The !$3$! integration is typically configured directly in your !$4$! file.

!$5$!

Replace !$6$! and !$7$! with the actual IP addresses or hostnames of the devices you want to track.

Setup Steps (UI - Limited)

While some basic device tracking can be added via the UI, the !$8$! platform offers more flexibility via YAML, especially for setting !$9$! and !$10$!.

Pros of Ping:

  • Simple and lightweight.
  • Fast responses.
  • Works purely based on network reachability.

Cons of Ping:

  • Requires a fixed IP address or reliable hostname for the device.
  • Doesn't use MAC addresses directly (though your router maps IP to MAC).
  • Some devices might be configured to not respond to ping requests.

Method 2: The Nmap Integration

!$11$! is a powerful open-source tool for network discovery and security auditing. The Home Assistant !$12$! integration leverages this tool to scan your network and identify connected devices, primarily using MAC addresses.

Prerequisites

If you are running Home Assistant OS or Supervised, !$13$! is usually available. If running in Docker or Core on a standard OS, you might need to install !$14$! manually on the host system where Home Assistant is running.

Setup Steps (Configuration.yaml)

Configure !$15$! in your !$16$! file.

!$17$!

You can scan a specific range (like the example subnet !$18$!), a comma-separated list of IPs, or even hostnames. Using a subnet scan is powerful but can be slower depending on the size and congestion of your network.

Pros of Nmap:

  • Uses MAC addresses, which are permanent identifiers (unlike dynamic IPs).
  • More comprehensive scan can find more devices.
  • Can track new devices automatically.

Cons of Nmap:

  • Can be resource-intensive and slow, especially when scanning large subnets.
  • Relies on devices responding to network probes, which some might not.
  • Requires !$19$! to be installed and accessible.

Device Integration Tips

Once configured, Home Assistant will create !$20$! entities for the devices it finds. If you used !$21$! with !$22$!, check !$23$! (or the UI under Settings -> Devices & Services -> Entities) for new entities.

Finding MAC Addresses:

  • On the device: Check network settings (WiFi details).
  • On your router: Look at the list of connected clients (DHCP table or similar).
  • Using nmap directly: Run !$24$! (replace with your subnet) on a computer on your network. This performs a simple ping scan and lists IPs and MACs.

Dealing with Dynamic IPs: If using !$25$! and your router assigns dynamic IPs, your device's IP might change. It's highly recommended to set up DHCP reservations in your router so your tracked devices always get the same IP address. Alternatively, use !$26$! which tracks by MAC address.

Handling Mobile Devices: Modern mobile OS (Android, iOS) aggressively disconnect from WiFi and enter deep sleep to save battery. This makes them unreliable for network presence detection alone. They may not respond to pings or network scans when sleeping. Using the !$27$! parameter helps by keeping the device marked 'home' for a period after it was last seen, mitigating brief disconnections.

Best Practices for Reliability

  • Combine Methods: For critical presence detection (like determining if *anybody* is home), don't rely solely on one device or one method. Use a combination:

    • Network presence for consistency when devices are active.
    • Home Assistant Companion App location tracking (requires GPS and external access).
    • Bluetooth tracking (e.g., iBeacons or ESPresense).
    • Door/motion sensors (for immediate triggers upon arrival/departure).
  • Use Helpers: Create a Group helper (type: Binary Sensor) for each person. Add multiple device tracker entities (e.g., their phone's network tracker, their phone's companion app tracker) to this group. Set the 'Device class' to 'presence'. This group entity will be !$28$! (home) if *any* of the member trackers are !$29$!, and !$30$! (away) only if *all* members are !$31$!. This significantly improves accuracy.

    !$32$!

    Then create a Person entity and associate the group helper with it.

  • Adjust Scan Intervals: Shorter intervals mean faster detection but increase network traffic and potentially device battery drain (as devices wake up to respond). Longer intervals save resources but delay detection. Find a balance (e.g., 60-120 seconds for active devices, longer for others).

  • Leverage !$33$!: This is crucial for devices that frequently disconnect. It prevents rapid toggling between 'home' and 'away'. Set it to a value (in seconds) that accounts for brief network drops or sleep cycles.

  • Static IPs/DHCP Reservations: If using the !$34$! integration, assign static IPs or use DHCP reservations in your router for tracked devices. This ensures their IP doesn't change unexpectedly.

Automation Examples

Presence detection entities (!$35$! or the person/group helpers like !$36$!) can be used as triggers or conditions in your automations.

!$37$!
!$38$!

Troubleshooting

  • Device not detected: Double-check the IP/MAC address. Ensure the device is actually connected to the network and awake. Check your router's connected devices list.
  • Device flapping (frequently changing state): Adjust the !$39$! parameter to a higher value. This is very common with mobile phones.
  • Nmap errors: Ensure !$40$! is installed and accessible to the Home Assistant user/container. Check Home Assistant logs for specific error messages related to the !$41$! platform.
  • Slow detection: For !$42$!, narrow down the scanned !$43$! range or increase the !$44$!. For !$45$!, ensure the device responds quickly.

Conclusion

Implementing network-based presence detection with the !$46$! and !$47$! integrations offers a powerful way to add local, reliable presence data to your Home Assistant setup. While mobile device power saving presents challenges, using best practices like combining methods, leveraging helpers, and tuning parameters can significantly improve accuracy and enable truly responsive, presence-aware automations.

Experiment with both !$48$! and !$49$! to see which works best for your network and devices. Often, a combination of devices tracked by different methods provides the most robust solution.

Written by:

NGC 224

Author bio:

There are no comments yet
loading...