script for presence detection of a phone

All kinds of 'OS' scripts
krizzz
Posts: 171
Joined: Wednesday 20 November 2013 21:36
Target OS: Linux
Domoticz version:
Location: The Netherlands
Contact:

Re: script for presence detection of a phone

Post by krizzz » Saturday 10 June 2017 22:14

thijsdebets wrote:I created a script to detect all devices on the network.
Installation instructions are here: https://github.com/thijsdebets/networkdetectz/wiki

It uses a combination of arpscan, ping and even supports bluetooth detection.
I use it very succesfull to detect who is at home and to set the lights to TV-Mode when the tv switches on, etc.

It requires one arp-scan every minute via cron making it low on resources.
I tried and follow the wiki and I get stuck already at

Code: Select all

register the IDX in nwd-config
When doing the git install, is the nod-onfig file already present somewhere? I think not, so do I have to create the file as new? What is included? If I look at

Code: Select all

HardwareIDX=999
Create a Hardware switch for NewDevicedFound and add the IDX in nwd-config
NewDevicesFoundIDX=999
Create a Hardware switch for Running indicator, to see if the script is up. Set this indicator on 'Off-delay' 10 minutes
NWDScriptRunning=999

Set other configuration items in nwd-config:
DomoPIN='1234'
RetryAttempts=1 # 3 = retry 4 times (5 minutes), 1 = retry 2 = 3 minutes
I do not know what should be in or out... Could you help me out?

User avatar
EdwinK
Posts: 1394
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: script for presence detection of a phone

Post by EdwinK » Sunday 11 June 2017 10:52

thijsdebets wrote:I created a script to detect all devices on the network.
Installation instructions are here: https://github.com/thijsdebets/networkdetectz/wiki

It uses a combination of arpscan, ping and even supports bluetooth detection.
I use it very succesfull to detect who is at home and to set the lights to TV-Mode when the tv switches on, etc.

It requires one arp-scan every minute via cron making it low on resources.

Looks neat. But since it requires Domoticz to be running on a Pi, I'm not able to run it on a Synology NAS.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

glsf91
Posts: 38
Joined: Tuesday 14 November 2017 22:56
Target OS: Linux
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by glsf91 » Tuesday 14 November 2017 23:10

Instead of a ip-address you can also use the dns/dhcp name of the device.
At my router my phone is registered with always the same name.

You can get the name with doing nslookup <ip-address>. It tells you the name. This can be used as argument for the script instead of the ip-address.

marmachine
Posts: 79
Joined: Saturday 26 March 2016 11:19
Target OS: Raspberry Pi
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: script for presence detection of a phone

Post by marmachine » Thursday 09 August 2018 13:08

See https://dougvitale.wordpress.com/2011/1 ... lacements/

ARP scan is deprecated, IP neighbor should be used now....

zicht
Posts: 223
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 3.8xxx
Location: NL
Contact:

Re: script for presence detection of a phone

Post by zicht » Sunday 26 August 2018 10:58

Hi All

Thanks for this great Idea ! I had no clue about the possibilities of this.
On win10 x64 platform (full updated) i implemented arp lookup in LUA
Now i am doing some test and it looks promising.

When doing plain Ping test for presence with android (diff versions : jelly-been to oreo) in dose/sleep state does not get a respond.
Somehow the ARP table keeps the entry also in this state, and the regular wake up of android for checks is enough to keep the entry in the ARP table: normaly based on ping, over night you will see a sequence of ON / OFF in the dummy switch log.

With ARP it stays On the complete night !
But when i turn wifi off the enty disapears in ARP after 5 minutes ...
- A clear benefit for presence detection.
- Another benefit is you can lookup the ARP table against the MAC adress what makes it IP undependent (DHCP can be used vs Static assignment)
- It is very fast ! not depending on external factors, just internal table lookup (no Phone interaction needed)

I Love the idea and will keep on testing for next couple of days.

Below the code replace xx with your mac lookup, It runs the batch file below.

Code: Select all


	commando='findmac.bat xx-xx-xx-xx-xx-xx'   
	content=os.capture(commando)
	if string.len(content)>10 then 
			findm=true
			if otherdevices['Arp M']=="Off" then 
				commandArray['Arp M']="On" 
				NMA("Mherkend") 
			end			
	else
			if otherdevices['Arp M']=="On" then commandArray['Arp M']="Off" end
	end
Lua Helper function i use for more OS captures on my system to read results

Code: Select all

function os.capture(cmd, raw)				-- os.command uitvoeren en resultaat daarvan lezen in string
  local f=assert(io.popen(cmd, 'r'))
  local s=assert(f:read('*a'))
  f:close()
  if raw then return s end
  s=string.gsub(s, '^%s+', '')
  s=string.gsub(s, '%s+$', '')
  s=string.gsub(s, '[\n\r]+', ' ')
  return s
end
Windows batch file in domoticz dir. It is important where you place this file to make it reachable for Lua
It returns an empty string when the ARP entry is not found (checked in LUA on length of the string)
It returns IP and Mac when the entry is found in the ARP table.

Code: Select all

@ECHO OFF
SET macd="%1"
ARP -a |find %macd% 
I keep you posted with the results ( for my purpose this seems much better usable vs the ping aproach...)

EDIT :
I need the pinger to make some kind of connection , if not it takes long before the arp catches the MAC.
But with pinger enabled to the phone it works perfectly and more reliable.
Due to the pinger i slill need to assign a DHCP fixed IP binding to the phone.
So i am trying now a network monitor to see if this is enough for getting the phone in the ARP table
First test looks promising, but need to test longer (goal to not ping any more at all)
Keep you posted
Rpi & Win x64. Using : cam's,Nest,RFXCom,Kaku,LaCrosse,RFY,IsAlive,Dummy,Wether Underground, standard Lua, Curl on windows,Domoticz Android App & Tasker,

"The only source of knowledge is experience (A.Einstein)"

snuiter
Posts: 72
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: script for presence detection of a phone

Post by snuiter » Tuesday 11 September 2018 22:12

Today I installed the script of thijsdebets, it was a bit figuring out how he created/made it as the install guide is not flawless but got it working and will now test it for a few a days. Will see how devices will be switched off etc..

Not tried the solution from zicht metioned above

garethiowc
Posts: 22
Joined: Monday 04 December 2017 0:02
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by garethiowc » Thursday 01 November 2018 0:26

hi all,

i'm getting the following -

Code: Select all

root@Domoticz:/home/gareth/domoticz/scripts# ./check_device_online.py  192.168.1.1 6 10 120
22:23:29- script started.
Traceback (most recent call last):
  File "./check_device_online.py", line 101, in <module>
    lastreported = domoticzstatus()
  File "./check_device_online.py", line 79, in domoticzstatus
    json_object = json.loads(domoticzrequest(domoticzurl))
  File "./check_device_online.py", line 96, in domoticzrequest
    response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 435, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
using the script from - https://www.domoticz.com/wiki/Presence_ ... ing_Python

User avatar
jvdz
Posts: 1461
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: script for presence detection of a phone

Post by jvdz » Thursday 01 November 2018 10:01

garethiowc wrote:
Thursday 01 November 2018 0:26
hi all,

i'm getting the following -

Code: Select all

-snip-
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
using the script from - https://www.domoticz.com/wiki/Presence_ ... ing_Python
Are you using Domoticz web access with UserId & Password as it sounds like you script isn't allowed to update the device.
When the script runs on the domoticz server itself you can use http://127.0.0.1..... and add that IP to the local Networks ip addresses to allow access without the need for a UserId/Password.

Jos

garethiowc
Posts: 22
Joined: Monday 04 December 2017 0:02
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by garethiowc » Sunday 04 November 2018 23:52

Hi Jos,

thank you very much, adding the local address got it working

garethiowc
Posts: 22
Joined: Monday 04 December 2017 0:02
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: script for presence detection of a phone

Post by garethiowc » Monday 05 November 2018 0:46

Can't get the script to work with iPhone or android

works on laptops etc

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests