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

0
0
  • #automation
Represent Mastering Network Presence: Using Ping and Nmap for Home Assistant Presence Detection article
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: ping and nmap. 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 ping 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 ping integration is typically configured directly in your configuration.yaml file.

# configuration.yaml
device_tracker:
  - platform: ping
    hosts:
      my_phone: 192.168.1.101 # Or hostname like 'my-phone.local'
      wife_phone: 192.168.1.102
    scan_interval: 60 # Check every 60 seconds
    consider_home: 180 # Consider device home if seen in last 180 seconds

Replace 192.168.1.101 and 192.168.1.102 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 ping platform offers more flexibility via YAML, especially for setting scan_interval and consider_home.

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

nmap is a powerful open-source tool for network discovery and security auditing. The Home Assistant nmap 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, nmap is usually available. If running in Docker or Core on a standard OS, you might need to install nmap manually on the host system where Home Assistant is running.

Setup Steps (Configuration.yaml)

Configure nmap in your configuration.yaml file.

# configuration.yaml
device_tracker:
  - platform: nmap_tracker
    hosts: 192.168.1.0/24 # Scan the entire subnet, or specific IPs/hostnames
    home_interval: 5 # Time in minutes before scanning for devices marked as away
    track_new_devices: true # Automatically add new devices found
    exclude:
      - 192.168.1.1 # Exclude your router
    scan_options: '-F --host-timeout 5s' # Fast scan, host timeout
    consider_home: 180 # Consider device home if seen in last 180 seconds

You can scan a specific range (like the example subnet 192.168.1.0/24), 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 nmap to be installed and accessible.

Device Integration Tips

Once configured, Home Assistant will create device_tracker entities for the devices it finds. If you used track_new_devices: true with nmap, check known_devices.yaml (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 nmap -sn 192.168.1.0/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 ping 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 nmap 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 consider_home 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 on (home) if *any* of the member trackers are home, and off (away) only if *all* members are away. This significantly improves accuracy.

    # configuration.yaml (example group helper)
    group:
      living_room_lights:
        name: Living Room Lights
        entities:
          - light.light_1
          - light.light_2
    
    # UI Helper equivalent (Recommended for Binary Sensor Groups)
    # Go to Settings -> Devices & Services -> Helpers -> + Create Helper -> Group -> Binary sensor group

    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 consider_home: 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 ping 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 (device_tracker.device_name or the person/group helpers like binary_sensor.person_name) can be used as triggers or conditions in your automations.

# Example Automation: Turn on lights when first person arrives
automation:
  - alias: 'Welcome Home Lights'
    trigger:
      - platform: state
        entity_id: binary_sensor.group_all_persons # Or binary_sensor.person_name
        to: 'on'
    condition:
      # Optional: Check if sun is down
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
      - service: light.turn_on
        target:
          entity_id: light.entrance_light
# Example Automation: Arm alarm when last person leaves
automation:
  - alias: 'Arm Alarm When Away'
    trigger:
      - platform: state
        entity_id: binary_sensor.group_all_persons # Or binary_sensor.person_name
        to: 'off'
        # Optional: Add a delay to ensure it's not a brief drop
        for:
          minutes: 5
    action:
      - service: alarm_control_panel.alarm_arm_away
        target:
          entity_id: alarm_control_panel.home_alarm
        data:
          code: !secret alarm_code # Use secrets for sensitive data

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 consider_home parameter to a higher value. This is very common with mobile phones.
  • Nmap errors: Ensure nmap is installed and accessible to the Home Assistant user/container. Check Home Assistant logs for specific error messages related to the nmap_tracker platform.
  • Slow detection: For nmap, narrow down the scanned hosts range or increase the scan_interval. For ping, ensure the device responds quickly.

Conclusion

Implementing network-based presence detection with the ping and nmap 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 ping and nmap to see which works best for your network and devices. Often, a combination of devices tracked by different methods provides the most robust solution.

Avatar picture of NGC 224
Written by:

NGC 224

Author bio:

There are no comments yet
loading...