Enhancing Home Automation with Presence Detection Using Nmap and OPNsense

by Pete
7 minutes read

Presence detection is a fundamental aspect of smart home automation. Knowing who is home and when they arrive or leave allows you to create personalized and efficient experiences. In this guide, we’ll explore how to achieve robust presence detection using Nmap, OPNsense, and Home Assistant. This approach provides accuracy and flexibility for a wide range of automation scenarios.

Prerequisites

Before you embark on setting up presence detection using Nmap and OPNsense with Home Assistant, make sure you have the following:

  1. Home Assistant Installation: Ensure you have Home Assistant up and running in your smart home ecosystem. You can install it on various platforms, including Raspberry Pi, Docker, or a virtual machine.
  2. Basic Familiarity with Home Assistant: Having a foundational understanding of Home Assistant’s configuration and interface will be beneficial as you navigate through the setup process.
  3. OPNsense Router: Access to an OPNsense router or firewall is essential, as this platform will play a key role in enhancing your presence detection accuracy. [Optional]
  4. Devices to Track: Identify the devices you want to track for presence detection. These could be smartphones, tablets, laptops, or other devices that connect to your home network.
  5. Nmap Installed: Install Nmap on your Home Assistant system. Nmap is a powerful network scanning tool that you’ll use to scan your network and detect the presence of devices.
  6. Home Assistant Configuration Access: You should be familiar with accessing and editing your Home Assistant configuration files, typically found in configuration.yaml.

1
Install and Configure Nmap

To begin, you need to install Nmap on your Home Assistant system. Nmap is a powerful open-source tool for network discovery and security auditing. It can also be used for presence detection. You can install Nmap using your system’s package manager or by downloading it from the Nmap website.

Once Nmap is installed, you’ll need to configure it to work with Home Assistant. Open your Home Assistant configuration file (usually configuration.yaml) and add the following lines:

device_tracker:
  - platform: nmap_tracker
    hosts: 192.168.1.1-254
    home_interval: 10
    interval_seconds: 60

This configuration tells Home Assistant to use Nmap as a device tracker. It scans the IP addresses in the specified range every 60 seconds, marking devices as “home” if they respond to the scan. The home_interval parameter determines how long a device must be seen as “home” before it’s considered as such.

2
Utilize OPNsense

OPNsense is an open-source firewall and routing platform. You can use it to enhance your presence detection system. Log in to your OPNsense router’s web interface and navigate to the DHCP Server settings. Here, you can assign static IP addresses to the devices you want to track. This ensures that the IP addresses of your devices remain consistent, making them easier to detect.

3
Set Up Device Tracking

Back in Home Assistant, navigate to Configuration > Integrations and add the Nmap integration. Home Assistant will now start tracking devices using the Nmap scans. Devices detected through these scans will appear as device trackers in Home Assistant, showing their current state as either “home” or “not_home.”

4
Automation and Personalization

One of the most compelling features of presence detection is automation. With knowledge of who is home, you can create automations tailored to specific situations. For example, you can create an automation that activates lights when someone arrives home:

alias: Turn on Lights Upon Arrival
trigger:
  platform: state
  entity_id: device_tracker.your_device
  from: 'not_home'
  to: 'home'
action:
  service: light.turn_on
  entity_id: light.living_room

This automation triggers when the state of a specified device tracker changes from “not_home” to “home.” In response, it turns on the living room lights. This simple example demonstrates how presence detection can make your home more responsive to your needs.

5
Enhance with Zones

To further enhance your presence detection, define specific areas in your home as zones. Zones are geographic areas that can be associated with devices or automations. For example, you can define a zone for the living room:

zone:
  - name: living_room
    latitude: 40.7128
    longitude: -74.0060
    radius: 20

This code defines a zone called “living_room” with specified latitude, longitude, and radius values. You can then associate devices and automations with this zone to create more precise and context-aware automations.

6
Notifications and Alerts

Presence detection isn’t limited to automation; it can also be used for notifications and alerts. For instance, you can create an automation that sends you a notification when someone arrives home:

automation:
  alias: Notify Upon Arrival
  trigger:
    platform: state
    entity_id: device_tracker.your_device
    to: 'home'
  action:
    service: notify.mobile_app_your_device
    data:
      message: 'Someone has arrived home.'

This automation triggers when the specified device tracker changes its state to “home.” It then sends a notification to your mobile device with a message indicating that someone has arrived home.

Advantages of the System

Implementing Nmap and OPNsense presence detection with Home Assistant offers several advantages:

  • Precise Automation: Lights activate instantly upon entering a room, enhancing convenience and energy efficiency.
  • Enhanced Security: Receive alerts if an unfamiliar device joins your network, helping you monitor network security.
  • Tailored Experiences: Adjust lights, heating, or cooling based on individual presence, optimizing comfort.
  • Energy Conservation: Turn off lights and devices when no one is home, reducing energy consumption.

Conclusion

Combining the power of Nmap and OPNsense for presence detection with Home Assistant enables you to establish an intelligent, responsive, and personalized smart home environment. Whether it’s seamless automation, heightened security, or energy-conscious living, this presence detection system can revolutionize how your home interacts with your daily routines and preferences. By implementing these steps, you’ll be on your way to creating a smart home that adapts to your presence seamlessly.