Like many others, I have had some trouble with getting nmap to work correctly. It is prone to report false away states caused by the device falling out of the network because it regularly goes into sleep mode, at least on iOS.
Could this be improved by setting up a function in the HA app that tracks motion of the iPhone by using “Raise to Wake” or other sensors? If you leave the house, taking your iPhone with you, this should trigger the Raise to Wake status if the iPhone was in a sleep mode prior to the action?
This could be used as follows in a MK2 of the nmap:
An iPhone is connected to the home network, and nmapMK2 sets its status to ‘home’.
At the same time, HA app starts to monitor motion of the iPhone, using Raise to Wake, and reports the status to hass at regular intervals.
When the iPhone falls into sleep mode, this would normally trigger a ‘not_home’ status in the old nmap, but nmapMK2 first checks with hass whether the iPhone has been moving within the last x seconds (x to be set in configuration of nmapMK2 in configuration.yaml).
3a. If it has NOT been reported as moving within the x seconds prior to loosing network connection, nmapMK2 can safely assume that this is a false away and nothing happens.
3b. If it HAS been or IS moving, nmapMK2 sets the status of the device to ‘not_home’ and HA app stops to monitor motion of the iPhone until the next time it is accessing the home network.
This of course assumes that the “Raise to Wake” function is accessible for app developers, which I don’t know. It would also only work on iPhones from 6s and up. But IF it works we could have a much more reliable device tracker for newer iPhones on HA!
Nice idea. Most of the problem is around Apple devices as far as I know. Apple does releases frequently so some type of Raise to Wake functionality may be there now, I’m not sure. I have a couple of questions.
what constitutes motion?
Lets say my phone went to sleep. MK2 realizes I was at home and figures I’m probably still at home. 5 minutes later I leave the house and drive to the store some distance away. How does MK2 know I’m not still just asleep at at home??
I think the desire is to not monitor the phone heavily especially if you are in a known location like home. Those are see as wasted checks chewing up battery life. (Apple does a good enough job killing our batteries on it’s own ).
Keep thinking though. There has got to be an answer somewhere will all the smart people we have out here.
Here is where the Raise to Wake comes into play. The whole logic in this is built around the assumption that a phone only falls asleep and go off the network after being passive for a certain amount of time. While it is passive, any fallout from the network is a false away. If it is active when it falls out of the network, then the phone has left the building and should trigger a correct away status. I assume that when you grab the phone and leave with it in your pocket, this would trigger Raise to Wake or another motion detected by the iPhone sensor. I also assume that this will provoke a new connection to the network, and HA app will report to hass that the phone is in motion.
HA app would only need to report to hass when it is awake and connected to the network. Once it falls asleep, HA app should sense this and not make any attempts to access the network. I am not sure (don’t know enough about the tech here) whether this will lead to a drastic drainage of the battery?
Interesting presentation. It does bring up some new possibilities. I wouldn’t use Nmap, I think it’s approaching it from the wrong direction. But an app would be an interesting thought here.
So if I’m moving, ping the network every so often to verify I can still access my home network. I don’t think HA would be able to detect that you aren’t there anymore. You would still need to send some type of transaction to HA telling it you no longer have access to the network. A simple MQTT packet should be fine or a dedicated socket message or something. Then when the network is again reachable, another MQTT message would be sent to HA telling it that you are now home.
Now if wifi gets turned off on your phone, HA is going be told that you are away since the network can’t be seen anymore.
There will need to be some way of throttling the network checks only checking maybe once every 5 - 15 minutes and only sending a message when the network access status changes and once an hour while the status stays the same. Just to give a method of saving battery if it turns out to be more of a battery issue than we think.
There are other options as well, where other network access, could be used like zones to identify when the person is in another known location.
If only checking every 5-15 minutes, this would be rather useless for automations supposed to run at the moment you come home to your house?
For me nmap works fine when it comes to changes in state caused by entering and leaving the house. It reports me as home just before I enter the door and reports me away just after leaving the house. And it does this without spending any extra battery on my phone. The only annoyance are the numerous false away status changes caused by my iPhone dropping out of the network when falling asleep, followed by just as many false “coming home” status changes. This, of course, messes with any automation designed to run when entering and leaving the house. If the approach I describe above works the way I hope, this would remove the annoying false status changes and leave nmap as a working device tracking system.
I can’t see instantly how your approach will work and I think this would consume just as much battery, but perhaps we’ll have two working device trackers after this instead of one?
could be. To be honest, I don’t think anything short of key ring beacon, with some number of house powered readers spread around the house at least at each entrance is going to give us acceptable presence detection. Anything else requires the phone to be on and transmitting which eats up battery power. A key ring beacon never sleeps and the house wired sensors wouldn’t sleep either. That’s what I think we are looking at.
I use the standard ios tracking for more general tracking… mall, walking, near the house etc…
I use ping for immediate sensing when I enter the house. I use a shell script, MQTT, and a Ubuntu VM to accomplish this.
It will check for state changes every 1sec to ( as long a pause as you want ) seconds, but will not send the MQTT payload until the state changes. This way you keep a very quick response time.
Script:
#!/bin/bash
i=15
j=1
state=0
hflag=0
aflag=0
nflags=0
rsval=2
zero=0
echo $i
echo " "
while :
do
let "j += 1"
now=$(date +"%T")
state=$(ping -q -c 1 -W 2 192.168.10.150 > /dev/null 2>&1 && echo 'Home' || echo 'Away')
if [ "$state" == "Away" ]; then
if [ "$aflag" == "$zero" ]; then
mosquitto_pub -h 192.168.100.4 -t "/home/device/iphone" -m "$state - $now"
hflag=0
fi
aflag=1
echo $state $now $j
elif [ "$state" == "Home" ]; then
if [ "$hflag" == "$zero" ]; then
mosquitto_pub -h 192.168.10.4 -t "/home/device/iphone" -m "$state - $now"
aflag=0
fi
hflag=1
echo $state $now $j
fi
sleep $i
Output:
Away - 17:31:12
Home - 17:31:29
Away - 18:14:38
Home - 18:14:55
Away - 18:15:10
maybe with the later releases of ios the battery life has gotten better, but the standard ios reporting was making me recharge my battery 2 or 3 times a day. How do you ensure that you have the correct ip address to ping? Do you have a way of assigning your phone a static IP address? DHCP reservations have an issue where occasionally Apple will change your MAC address (to prevent cyber stalking). This screws up the DHCP reservation and makes you appear to not be home.
I don’t have any major battery problems, but it could be my use case.
I use pfsence with Static Mapping on DHCP… But, you can do that with most out of the box routers.
" Apple will change your MAC address" - Never had this problem. I do a auto setup on iPhone6.
(to prevent cyber stalking) - I have my HA network isolated form my IoT network and Secure network.
The Secure net can talk to HA but HA is prevented from reaching the Secure net.
The HA net can talk to IoT but IoT is prevented from reaching the HA or Secure net. [ Lights ]
It works just like a stateful router. Simplified Example: If Secure establishes a stateful link to HA then HA can only respond to those specific requests from Secure. It acts just like your home router connected to the internet.
HA has no issues [ ie. MQTT into HA from Secure to allow any info from Secure to be shared with HA, BUT no MQTT from HA > Secure. This is how I run my python3 scrips run on Secure.
Nothing gets to the Internet unless I allow it… Firewall Rules!
I do use Lets Encrypt and DuckDNS…
In short, No issues with local device tracking with ping.
Did this make any sense to you? If not let me know.
It makes perfect sense. Very similar to my configuration. What I found on an iPhone 7 was that when we were away from home, the iPhone spent the day sending messages back to HA telling it where it was. The GPS usage drained the battery very fast.
It’s a documented issue with Apple that they will change your MAC on occasion. The idea is that if someone outside of your house got your mac address, they could use it to track you and stalk you. It’s above my skill level, but that’s their excuse. I’d gladly be stalked by someone if it gave me a stable mac address :). I can handle stalkers on my own.
I was thinking to implement a system that only checks NMAP when front door opens (magnetic sensor). Like:
Door opens
Check NMAP immediately
Wait a minute
Check NMAP again
The second one would determine the actual status. With an exception: old status is “home” and both nmaps return “away”. That would indicate no change of status.
Scenarios:
I leave home. Old status: home, 1st nmap: home, 2nd nmap: away --> away.
I’m coming home. Old status: away, 1st nmap: home, 2nd nmap: home --> home.
I’m home sleeping (phone sleeping), my wife leaves home. Wife: Old status: home, 1st nmap: home, 2nd nmap: away --> away. Me: Old status: home, 1st nmap: away, 2nd nmap: away --> home.
It would not be bulletproof. Leaving home, staying 1 minute outside the door connected to wifi would keep me home forever. Or leaving the front door open on a nice summer day would never change status.
Might be good enough for automations for e.g. lights or TV. Because you could still create automations also based on the first nmap, like turning on lights: away–>home turns on lights. If the second nmap is away you had lights on for a minute. Maybe.