Presence detection

Python and python framework

Moderator: leecollings

Post Reply
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Presence detection

Post by remko2000 »

I am trying to implement the following scripts:
https://www.domoticz.com/wiki/Presence_detection

In the script I only change these 3 rules (into my own data):
domoticzserver="192.168.2.1:8080"
domoticzusername = "admin"
domoticzpassword = "admin"
but I'm stuck after following al the steps. If I start my script manually in my terminal with:
python check_device_online.py MYIP xxxx 10 120
I get first
script started
and than some errrors:
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 431, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
the status in domoticz of my switch does not change.

If I open the log
check_device_online.py_myip.pid
it's empty. What can be the problem?
pipiche
Posts: 2005
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: France
Contact:

Re: Presence detection

Post by pipiche »

Python Plugin required at least Phython3.4 and above !

https://www.domoticz.com/wiki/Using_Python_plugins

So first you need to cross-check that Python3 is used by domoticz and not 2.7 as I see in the log
Zigbee for Domoticz plugin / RPI3B+ / Electrolama ZZH-P / 45 devices

If the plugin provides you value, you can support me with a donation Paypal.

Wiki is available here.

Zigbee for Domoticz FAQ
unclekoen
Posts: 21
Joined: Saturday 27 October 2018 11:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by unclekoen »

Hee i am also into this.
I started the script manualy, no error, but also no change in the switch. Where can i find some kind of a log file??

When i do “python -V” i get version 2.7.13.

The wiki is also writing:
Check if the script works
Now we need to check if the script works when we activate it by hand. Go to the scripts directory (cd /home/pi/domoticz/scripts/) and then activate the script with the correct parameters:
python check_device_online.py IP_PHONE IDX INTERVAL TIMEOUT. For testing you can use a interval of 10 seconds and a timeout of 30 seconds, so you don't have to wait very long to see the switch turn on/off in Domoticz.
If everything works good, you should see some output like: Script already running, exiting or Needs more parameters. If you get other errors (unclear errors, that doesn't seem to be related to the script), maybe you don't have 'python' (not 'python3' !!) correctly (sudo apt-get install python), or you made a mistake when you copied the script.
unclekoen
Posts: 21
Joined: Saturday 27 October 2018 11:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection (python version, syntax error)

Post by unclekoen »

remko2000 wrote: Saturday 19 January 2019 16:13 and than some errrors:
When waiting a minute i also get the same errors.

Off course, when installing Raspbian full version, Python 2.7 and 3.5 is installed.
To change the default version i used this website: https://linuxconfig.org/how-to-change-f ... bian-linux

Python -V is nicely showing: Python 3.5.3 :D

But next problem has appeared:
File "check_device_online.py", line 56
print datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists"
^
SyntaxError: invalid syntax
(with the ^ on the second e in line)
unclekoen
Posts: 21
Joined: Saturday 27 October 2018 11:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection (forget this script)

Post by unclekoen »

Forget this script.
There is a built in Ping in Domiticz. See here: https://www.domoticz.com/wiki/System_Al ... %28Ping%29

Setup and working under 5 minutes.
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by gschmidt »

I tried system alive checker....problem is that 2 of my android phones (and they have both a fixed ip adrress on my router) constantly are kicked out....with the python presence detection script on domoticz wiki they don't, but this script is a 2.7 version and gives errors in domoticz log because domoticz is running python 3.5.3 which uses a different library for urllib2.

Still looking for a good presence detection plug-in or working py script....
EscApe
Posts: 535
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Presence detection

Post by EscApe »

Does the idetect plugin work for you?
https://www.domoticz.com/forum/viewtopi ... 65&t=20467
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by gschmidt »

Nope...I have a router running pfsense firewall software...this blocks ssh access port 22 by default...besides that ssh to your main router is dangerous when the pi/domoticz is hacked
Last edited by gschmidt on Friday 15 February 2019 19:40, edited 1 time in total.
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by gschmidt »

I am not an experienced programmer...but I find it strange that the system alive checker plugin which uses the same arping library as the python script has a problem with keeping mobiles alive in the network?
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection (python version, syntax error)

Post by gschmidt »

unclekoen wrote: Saturday 09 February 2019 8:26
remko2000 wrote: Saturday 19 January 2019 16:13 and than some errrors:
When waiting a minute i also get the same errors.

Off course, when installing Raspbian full version, Python 2.7 and 3.5 is installed.
To change the default version i used this website: https://linuxconfig.org/how-to-change-f ... bian-linux

Python -V is nicely showing: Python 3.5.3 :D

But next problem has appeared:
File "check_device_online.py", line 56
print datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists"
^
SyntaxError: invalid syntax
(with the ^ on the second e in line)
This is because the script is written in python 2 at a time that Domoticz python version was probably the same...
urllib2 is not used in python 3...in 3 the print command should have parenthesis:

print (datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists")

there is a conversion possible...haven't tried it yet
https://docs.python.org/2/library/2to3.html
unclekoen
Posts: 21
Joined: Saturday 27 October 2018 11:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by unclekoen »

gschmidt wrote: Friday 15 February 2019 18:30 I tried system alive checker....problem is that 2 of my android phones (and they have both a fixed ip adrress on my router) constantly are kicked out....with the python presence detection script on domoticz wiki they don't, but this script is a 2.7 version and gives errors in domoticz log because domoticz is running python 3.5.3 which uses a different library for urllib2.

Still looking for a good presence detection plug-in or working py script....
The script is 4 years old. chopperrob doen't wants to communicate anymore (www.chopperrob.nl), so i think this is a dead end. Unless someone else wants to rewrite the script.

I have 2 phones connected. Mine is very relax and reliable (maybe a very small time out time, i help this wit a time out time of 5 minutes (300 sec)). My girlfriends phone is not relax, continuously losing it's wifi, for some ours (when inactive). Maybe it's a setting in her phone. But at daytime, her phone my be a bit less active :lol: so it's working good enough for me.
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by gschmidt »

Chopperrob's script is rocksolid when it comes to presence detection, the virtual switches are working properly eventhough it is a 2.7 script.
The only thing what annoys me is the log of domoticz which generates tonnes of errors.

I spent some time in trying to convert the script to 3.5, but i get stucked with the import of the python 3 module urllib.request
The problem is when I run the script I get an error that urllib.request is not found.
But when I enter "import urllib.request" manually in the python 3 console of the Rpi (which runs Domoticz),
it returns that the "<class 'urllib.request.Request'>" is available.....
I can't figure out why the script not recognizes the module...must be an environment issue somehow
I created a thread a few days ago at viewtopic.php?f=65&t=27003, but nobody is responding :cry:
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection

Post by gschmidt »

unclekoen wrote: Monday 18 February 2019 6:50
gschmidt wrote: Friday 15 February 2019 18:30 I tried system alive checker....problem is that 2 of my android phones (and they have both a fixed ip adrress on my router) constantly are kicked out....with the python presence detection script on domoticz wiki they don't, but this script is a 2.7 version and gives errors in domoticz log because domoticz is running python 3.5.3 which uses a different library for urllib2.

Still looking for a good presence detection plug-in or working py script....
The script is 4 years old. chopperrob doen't wants to communicate anymore (www.chopperrob.nl), so i think this is a dead end. Unless someone else wants to rewrite the script.

I have 2 phones connected. Mine is very relax and reliable (maybe a very small time out time, i help this wit a time out time of 5 minutes (300 sec)). My girlfriends phone is not relax, continuously losing it's wifi, for some ours (when inactive). Maybe it's a setting in her phone. But at daytime, her phone my be a bit less active :lol: so it's working good enough for me.
Jippie.....I think I found the solution for the chopperrob Presence Detection script at https://www.domoticz.com/wiki/Presence_detection working properly under Python 3

I have the modified script at viewtopic.php?f=65&t=27003
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest