Skip to content

Instantly share code, notes, and snippets.

@auriiolym
Last active October 20, 2023 02:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auriiolym/0ba854553a1b1cee2527bf79feed953f to your computer and use it in GitHub Desktop.
Save auriiolym/0ba854553a1b1cee2527bf79feed953f to your computer and use it in GitHub Desktop.
Triggerable nmap sensor
# Triggerable nmap sensor v1.1
# Created by Auriiolym (auriiolym@gmail.com).
#
# This is a custom nmap presence sensor that can be triggered by running the command_line.reload service.
# The nmap tracker integration (https://www.home-assistant.io/integrations/nmap_tracker/) does not
# support a service that can manually trigger an nmap scan. Such a feature can be useful when you
# want a motion sensor to trigger a presence scan.
# These steps create a binary_sensor entity synced to a device_tracker entity that you can add to a person
# entity. This is necessary because binary_sensor entites can not be added to a person, but device_tracker
# entities can.
# The examples show the entity that tracks my phone. Sources used to create this solution can be found below.
#
# Changelog:
# 20220605 - v1.1 - Improved docs by mentioning that you actually CAN remove the created device_tracker
# entity, and remove the useless --exclude option from the nmap command.
# 20220528 - v1.0 - Birth.
#
# 1. Add the sensor below (or this whole text) to your configuration.yaml.
# 2. Add the following automation:
# alias: Sync custom device tracker entities
# description: >-
# This links the binary_sensor entity (created in configuration.yaml)
# to the corresponding device_tracker entity, which can then be used to track a
# person entity. This is necessary because binary_sensor entites can not be added
# to a person, but device_tracker entities can.
# trigger:
# - platform: state
# entity_id:
# - binary_sensor.triggerable_nmap_127_0_0_ii
# to: 'on'
# - platform: state
# entity_id:
# - binary_sensor.triggerable_nmap_127_0_0_ii
# to: 'off'
# condition: []
# action:
# - service: device_tracker.see
# data:
# dev_id: triggerable_nmap_127_0_0_ii
# location_name: >
# {%- if is_state('binary_sensor.triggerable_nmap_127_0_0_ii', 'on') -%}home{%-
# else -%}not_home{%- endif -%}
# mode: restart
# 3. Run the action once to create the device_tracker entity (or wait until your automation is run once).
# Note that in order to remove this entity again, you have to remove its entry from known_devices.yaml
# and restart HASS.
# 4. Add the newly created device_tracker entity to your person entity.
# 5. Restart hass.
# 6. Test correct functioning:
# - Set your binary_sensor state (in the Developer Tools) to its opposite value. Observe your
# device_tracker entity following the same change.
# - Call the command_line.reload service (in the Developer Tools). Observe your device_tracker
# entity changing back to its original state.
#
# Here is an the automation that I use to prevent my burglar "alarm" from going off:
# alias: Trigger presence scan, on motion detection, if not home
# description: ''
# mode: single
# trigger:
# - platform: state
# entity_id:
# - binary_sensor.cerberus_1
# to: 'on'
# condition:
# - condition: state
# entity_id: person.auriiolym
# state: not_home
# action:
# - service: command_line.reload
# data: {}
#
# sources:
# https://www.home-assistant.io/integrations/command_line/#check-if-a-system-service-is-running
# https://community.home-assistant.io/t/manually-trigger-nmap-home-scan/167111
# https://community.home-assistant.io/t/how-to-remove-device-tracker-from-database/88775
binary_sensor:
- platform: command_line
name: 'triggerable nmap 127.0.0.ii'
unique_id: triggerable_nmap_127_0_0_ii
device_class: presence
scan_interval: 300 # seconds
command: 'nmap -oG - 192.168.1.227 -F --host-timeout 5s | grep "Status: Up" > /dev/null 2>&1 && echo up || echo down'
payload_on: 'up'
payload_off: 'down'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment