Present or not on LAN using DNS queries

Hi,
trying to detect if the family is home or not. (using wifi)
The ping intergration or openwrt intergration does not work well, it seems like the iphones goes to sleep when not used.

Maybe I could use DNS queries to detect presence? Using pihole and have access to the log.
Any thought on this, anyone? Trying to avoid coding this from cratch. :slight_smile:
Thanks!

For privacy reasons, modern versions of iPhone and Android rotate a synthetic Mac address on wifi by default so it makes your phone show up and multiple devices over time to your router. (and the net effect is network tracking based presence is unpredictable) I’m not an iPhone person but I know that in the settings for your WiFi network on iPhone there’s a setting to disable this functionality for your specific wifi network.
The Mac would then stay stable and your network gear based presence should stabilize if that’s the issue.

Thanks for info!
I’ve set static ip (openwrt) on 4 iphones, they do not change mac-adress by default, and i have not configured them at all. I think its two iphone 7 and two 12 mini. Also they’re updated IOS.
I’ll check the settings in the phones later.

You best way of detecting the presence of a mobile phone is the companion app.
Next I would guess arp lookup is the best way, where pings maybe can be used to force update the arp tables.

Ping only tell you something about the moment the command is run.
Arp tables are maintained by the network devices in order to quickly route packets to the right place.
Whenever a device sends a packet out on the network the devices that encounters this packet will write its mac address and port to the arp table.
The arp table is therefore updated whenever any time of packets is sent and the arp tables contain a view of the devices seen over a longer interval.
The interval is typically only set to a few minutes, so it is also not too long to give too many false positives.

Not everyone want to have the companion app installed to get tracket.

I can’t find much info on arp table. I guess the openwrt intergration is using it? I though the timeout of arp table in opewrt was 4 hours, but i’ll check again. If I can set the timeout to 5 min, it could be used to detect presence. But that may lead to too much arp-request flooding the LAN?

It looks more like 5 mins, but then purge can take double that, so 10 mins might be more accurate when dealing with it as detection.

Here is a discussion about it.

I did a “arp -a” on my openwrt router. It still shows devices which left the house 3 hours ago.
Anyway, i tested my dns-idea.

  1. A bash script starts every 5 min. (crontab). The script asks the pihole-database if a certain IP has used pihole last 5 min. The script puts the answer in a file, which HA can access.

# now-epoc
enow=$(date +%s)
#epoc 5 min agon
ebefore=$(($enow-300))
dnsnum=$(sqlite3 /myPath/pihole-FTL.db “SELECT count(*) FROM queries WHERE timestamp BETWEEN $ebefore AND $enow AND (client=‘10.0.1.1’)”)
echo $dnsnum > /anotherPath/dns_magne_telefon.txt

  1. Make a file-sensor in HA which reads the file:

- platform: file
file_path: “/morePath/dns_magne_telefon.txt”
name: dns_magne_telefon
scan_interval: 30

  1. Make a binary sensor. If the file-sensor (the content of the file) is 0, it means no queries last 5 min. So set the sensor to false.

dns_magne_telefon_is_home:
device_class: “presence”
value_template: >-
{% if int(states(‘sensor.dns_magne_telefon’)) == 0 %}
False
{% else %}
True
{% endif %}

look at the status of the arp entry.
It might still be there, but it is probably listed as stale.