Nmap issues, detect "home" and "not_home"

I am using Nmap to track my phone to detect “home” and “not_home”, works fine.

But I have issues when there is power outage and I have left the home, when hassio comes back it still shows my phone as “Home”.

I am sure this not an isolated issue and some of you have experienced the same, suggestions and workarounds are welcome.

At startup device_tracker platforms are given a chance to “see” their configured devices, which can provide up-to-date information about the device. For any devices listed in known_devices.yaml (i.e., were seen at some time in the past) that are not “seen” at startup, their previous state will be restored from the database.

For Nmap devices, this means if they are not home at startup (i.e., they’re not “seen”), their last known state will be retrieved from the database. So, if the device’s state was “home” when the power went out, they will be set to “home” when HA starts, even if they aren’t really home anymore. It will stay this way until the next time their state is updated, which is the next time they are “seen”, which, of course, is when they are actually home.

Now you might think that the consider_home parameter would come into play here and eventually change them to ‘not_home’. Unfortunately, I believe there is a bug in the code that prevents this “stale detection” from working in this scenario.

Thanks, is there some thing we can do on the HA startup to check the current state and set it?

There are two things you could possibly do. First, you could call the device_tracker.see service to “kick start” the consider_home stale detection process (to work around the “bug”.) Or you could use a python_script to change the state of the device_tracker entity. Let me give this some more thought. I’ll get back to you soon.

Unless you have some other way to determine (another device_tracker???) where your device really is (i.e., home or not) when HA starts, I think the best you can do is to add an automation something like this:

automation:
  - alias: Kick start consider_home process
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: device_tracker.ME
      state: 'home'
    action:
      service: device_tracker.see
      data:
        dev_id: ME

I really can’t test this because I don’t use an Nmap device_tracker, but based on reading the code I think this should work. I.e., at startup, if the device_tracker’s state is home (either because Nmap found it, or because that is the state restored from the database), calling this service should cause the consider_home stale detection process to start. (I.e., once the consider_home period expires after this service is called, the device should change to not_home, assuming of course, Nmap doesn’t find it during that time.)

One side effect is the source_type of the device_tracker will probably change from ‘router’ to ‘gps’, but that shouldn’t be a problem. Once Nmap sees it the source_type should change back to ‘router’. If you’d rather not have that happen, you can add the following after dev_id in the service call above:

        source_type: router

That should work even though it’s not “documented”.

I think I’ll open an issue against the device_tracker component. If it loads the state from the database, and the state is home, then it really should enable the consider_home stale detection to work in this scenario.

EDIT: Actually you should open the issue / bug report (see Home Assistant Issues and click on New Issue.) I really can’t because, again, I’m not using this platform, and I don’t have all the details from your install.

1 Like

Thanks a lot, I have tested the code and its works :). I will also report the bug, thanks again.