Check device online stops working after Domoticz V4.9700 update

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!
Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post

Please mark your topic as Solved when the problem is solved.
Post Reply
Meppel8
Posts: 8
Joined: Tuesday 06 June 2017 21:20
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: NL
Contact:

Check device online stops working after Domoticz V4.9700 update

Post by Meppel8 » Sunday 24 June 2018 12:26

Dear all,

Today I have updated my Domoticz to version V4.9700 ona a PI3 with success. Thanks for all the hard word to the developers.
Everything is running stable only my presence detection script is giving errors, like:
- import urllib2 not avaliable
and this error:

Code: Select all

2018-06-24 12:16:25.858 Error: EventSystem: Failed to execute python event script "/home/pi/domoticz/scripts/python/check_device_online.py"
  2018-06-24 12:16:25.858 Error: EventSystem: File "/home/pi/domoticz/scripts/python/check_device_online.py", line 68
  2018-06-24 12:16:25.858 Error: EventSystem: print datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists"
  2018-06-24 12:16:25.858 Error: EventSystem: ^
  2018-06-24 12:16:25.858 Error: EventSystem: SyntaxError: invalid syntax 
This is the script I use:

Code: Select all

#   !/usr/bin/python
#   Title: check_device_online.py
#   Author: Chopper_Rob
#   Date: 25-02-2015
#   Info: Checks the presence of the given device on the network and reports back to domoticz
#   URL : https://www.chopperrob.nl/domoticz/5-report-devices-online-status-to-domoticz
#   Version : 1.6.2
 
import sys
import datetime
import time
import os
import subprocess
import urllib2
import json
import base64
 
# Settings for the domoticz server
domoticzserver ="192.168.178.112:8080"
domoticzusername = "pi"
domoticzpassword = "raspberry"
domoticzpasscode = "Niet aanzitten"
 
# If enabled. The script will log to the file _.log
# Logging to file only happens after the check for other instances, before that it only prints to screen.
log_to_file = False
 
# The script supports two types to check if another instance of the script is running.
# One will use the ps command, but this does not work on all machine (Synology has problems)
# The other option is to create a pid file named _.pid. The script will update the timestamp
# every interval. If a new instance of the script spawns it will check the age of the pid file.
# If the file doesn't exist or it is older then 3 * Interval it will keep running, otherwise is stops.
# Please chose the option you want to use "ps" or "pid", if this option is kept empty it will not check and just run.
check_for_instances = "pid"
 
 
 
# DO NOT CHANGE BEYOND THIS LINE
if len(sys.argv) != 5 :
  print ("Not enough parameters. Needs %Host %Switchid %Interval %Cooldownperiod.")
  sys.exit(0)
 
device=sys.argv[1]
switchid=sys.argv[2]
interval=sys.argv[3]
cooldownperiod=sys.argv[4]
previousstate=-1
lastsuccess=datetime.datetime.now()
lastreported=-1
base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=devices&filter=all&used=true&order=Name'
 
if check_for_instances.lower() == "pid":
  pidfile = sys.argv[0] + '_' + sys.argv[1] + '.pid'
  if os.path.isfile( pidfile ):
    print datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists"
    if (time.time() - os.path.getmtime(pidfile)) < (float(interval) * 3):
      print datetime.datetime.now().strftime("%H:%M:%S") + "- script seems to be still running, exiting"
      print datetime.datetime.now().strftime("%H:%M:%S") + "- If this is not correct, please delete file " + pidfile
      sys.exit(0)
    else:
      print datetime.datetime.now().strftime("%H:%M:%S") + "- Seems to be an old file, ignoring."
  else:
    open(pidfile, 'w').close() 
 
if check_for_instances.lower() == "ps":
  if int(subprocess.check_output('ps x | grep \'' + sys.argv[0] + ' ' + sys.argv[1] + '\' | grep -cv grep', shell=True)) > 2 :
    print (datetime.datetime.now().strftime("%H:%M:%S") + "- script already running. exiting.")
    sys.exit(0)
 
def log(message):
  print message
  if log_to_file == True:
    logfile = open(sys.argv[0] + '_' + sys.argv[1] + '.log', "a")
    logfile.write(message + "\n")
    logfile.close()
 
def domoticzstatus ():
  json_object = json.loads(domoticzrequest(domoticzurl))
  status = 0
  switchfound = False
  if json_object["status"] == "OK":
    for i, v in enumerate(json_object["result"]):
      if json_object["result"][i]["idx"] == switchid:
        switchfound = True
        print (datetime.datetime.now().strftime("%H:%M:%S") + "- Switch ID found. Switch ID = " + switchid) 
        if json_object["result"][i]["Status"] == "On": 
          status = 1
        if json_object["result"][i]["Status"] == "Off": 
          status = 0
  if switchfound == False: print (datetime.datetime.now().strftime("%H:%M:%S") + "- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.")
  return status
 
def domoticzrequest (url):
  request = urllib2.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  response = urllib2.urlopen(request)
  return response.read()
 
log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
 
lastreported = domoticzstatus()
if lastreported == 1 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is online")
if lastreported == 0 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is offline")
 
while 1==1:
  # currentstate = subprocess.call('ping -q -c1 -W 1 '+ device + ' > /dev/null', shell=True)
  # currentstate = subprocess.call('sudo arping -q -c1 -W 1 '+ device + ' > /dev/null', shell=True)
  currentstate = subprocess.call('sudo ping -q -c1 -W 1 '+ device + ' > /dev/null', shell=True)
 
  if currentstate == 0 : lastsuccess=datetime.datetime.now()
  if currentstate == 0 and currentstate != previousstate and lastreported == 1 : 
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, no need to tell domoticz")
  if currentstate == 0 and currentstate != previousstate and lastreported != 1 :
    if domoticzstatus() == 0 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, tell domoticz it's back")
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- Send to Domoticz -> ON -> " + "http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=On&level=0")    
    # domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=On&level=0"  + "&passcode=" + domoticzpasscode)  --> er gaat nog wat mis met de bescherm code van en switch
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=On&level=0")
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, but domoticz already knew")
    lastreported=1
 
  if currentstate == 1 and currentstate != previousstate :
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, waiting for it to come back")
 
  if currentstate == 1 and (datetime.datetime.now()-lastsuccess).total_seconds() > float(cooldownperiod) and lastreported != 0 :
    if domoticzstatus() == 1 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, tell domoticz it's gone")
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- Send to Domoticz -> OFF -> "+ "http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=Off&level=0")
    # domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=Off&level=0" + "&passcode=" + domoticzpasscode)  --> er gaat nog wat mis met de bescherm code van en switch
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=Off&level=0")
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, but domoticz already knew")
    lastreported=0
 
  time.sleep (float(interval))
 
  previousstate=currentstate
  if check_for_instances.lower() == "pid": open(pidfile, 'w').close()
Is Domoticz V4.9700 using an newer version of Python?

Someone knows a solution?

Thanks for your help.

Greeting Rene

SweetPants
Posts: 2255
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.10007
Location: The Netherlands
Contact:

Re: Check device online stops working after Domoticz V4.9700 update

Post by SweetPants » Sunday 24 June 2018 13:34

Did you only update domoticz? Have you tried a re-boot? Looks like the script is already running
Ubuntu 16.04.3 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

Meppel8
Posts: 8
Joined: Tuesday 06 June 2017 21:20
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: NL
Contact:

Re: Check device online stops working after Domoticz V4.9700 update

Post by Meppel8 » Monday 25 June 2018 7:08

Yes, I only updated Domoticz to the new stable version V4.9700 and reboot several times. Also started python update manual, but got the message: already the latest version.

And yes the script in running, but not correctly. Some line give a error . When I correct the print lines with ( and ) then there comes the Urllib2 error. I see a difference in this library between Python en Python version 3.

Could that be the problem? The Urllib2 is used for URL to access the log/status file for each GSM.

Thanks for helping.


Verzonden vanaf mijn iPhone met Tapatalk

mrohner
Posts: 7
Joined: Saturday 29 April 2017 9:16
Target OS: Linux
Domoticz version: 4.9700
Location: Switzerland
Contact:

Re: Check device online stops working after Domoticz V4.9700 update

Post by mrohner » Sunday 08 July 2018 14:18

Same error here. When I upgraded to V4.9700 I first got message:
EventSystem - Python: Failed dynamic library load, install the latest libpython3.x library that is available for your platform.

As I did have python 3.5. installed already, I followed some hints given in this forum and installed
libpython3.5-dev.

That made the above message disappear but the script, which perfectly ran before, shows:

Code: Select all

2018-07-08 13:54:11.097 Error: EventSystem: Failed to execute python event script "/home/domoticz/domoticz/scripts/python/check_device_online.py"
2018-07-08 13:54:11.097 Error: EventSystem: File "/home/domoticz/domoticz/scripts/python/check_device_online.py", line 56
2018-07-08 13:54:11.097 Error: EventSystem: print datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists"
2018-07-08 13:54:11.097 Error: EventSystem: ^
2018-07-08 13:54:11.097 Error: EventSystem: SyntaxError: invalid syntax

SjoerdNLD
Posts: 6
Joined: Friday 12 May 2017 20:58
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Check device online stops working after Domoticz V4.9700 update

Post by SjoerdNLD » Tuesday 10 July 2018 23:13

Got the same, it is due version 3 of python;
Modified to work as before:

Code: Select all

#!/usr/bin/python
#   Title: check_device_online.py
#   Author: Chopper_Rob
#   Date: 25-02-2015
#   Info: Checks the presence of the given device on the network and reports back to domoticz
#   URL : https://www.chopperrob.nl/domoticz/5-report-devices-online-status-to-domoticz
#   Version : 1.6.2.1 modified by SjoerdNLD for python 3
 
import sys
import datetime
import time
import os
import subprocess
import urllib.request
import json
import base64
 
# Settings for the domoticz server
domoticzserver="127.0.0.1:8080/domoticz"
domoticzpasscode = "Black"
 
# If enabled. The script will log to the file _.log
# Logging to file only happens after the check for other instances, before that it only prints to screen.
log_to_file = False
 
# The script supports two types to check if another instance of the script is running.
# One will use the ps command, but this does not work on all machine (Synology has problems)
# The other option is to create a pid file named _.pid. The script will update the timestamp
# every interval. If a new instance of the script spawns it will check the age of the pid file.
# If the file doesn't exist or it is older then 3 * Interval it will keep running, otherwise is stops.
# Please chose the option you want to use "ps" or "pid", if this option is kept empty it will not check and just run.
check_for_instances = "pid"
 
 
 
# DO NOT CHANGE BEYOND THIS LINE
if len(sys.argv) != 5 :
  print ("Not enough parameters. Needs %Host %Switchid %Interval %Cooldownperiod.")
  sys.exit(0)
 
device=sys.argv[1]
switchid=sys.argv[2]
interval=sys.argv[3]
cooldownperiod=sys.argv[4]
previousstate=-1
lastsuccess=datetime.datetime.now()
lastreported=-1
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=devices&filter=all&used=true&order=Name'
 
if check_for_instances.lower() == "pid":
  pidfile = sys.argv[0] + '_' + sys.argv[1] + '.pid'
  if os.path.isfile( pidfile ):
    print (datetime.datetime.now().strftime("%H:%M:%S") + "- pid file exists")
    if (time.time() - os.path.getmtime(pidfile)) < (float(interval) * 3):
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- script seems to be still running, exiting")
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- If this is not correct, please delete file " + pidfile)
      sys.exit(0)
    else:
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- Seems to be an old file, ignoring.")
  else:
    open(pidfile, 'w').close() 
 
if check_for_instances.lower() == "ps":
  if int(subprocess.check_output('ps x | grep \'' + sys.argv[0] + ' ' + sys.argv[1] + '\' | grep -cv grep', shell=True)) > 2 :
    print (datetime.datetime.now().strftime("%H:%M:%S") + "- script already running. exiting.")
    sys.exit(0)
 
def log(message):
  print (message)
  if log_to_file == True:
    logfile = open(sys.argv[0] + '_' + sys.argv[1] + '.log', "a")
    logfile.write(message + "\n")
    logfile.close()
 
def domoticzstatus ():
  json_object = json.loads(domoticzrequest(domoticzurl))
  status = 0
  switchfound = False
  if json_object["status"] == "OK":
    for i, v in enumerate(json_object["result"]):
      if json_object["result"][i]["idx"] == switchid:
        switchfound = True
        if json_object["result"][i]["Status"] == "On": 
          status = 1
        if json_object["result"][i]["Status"] == "Off": 
          status = 0
  if switchfound == False: print (datetime.datetime.now().strftime("%H:%M:%S") + "- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.")
  return status
 
def domoticzrequest (url):
  request = urllib.request.Request(url)
  response = urllib.request.urlopen(request)
  return response.read()
 
log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
 
lastreported = domoticzstatus()
if lastreported == 1 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is online")
if lastreported == 0 :
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- according to domoticz, " + device + " is offline")
 
while 1==1:
  # currentstate = subprocess.call('ping -q -c1 -W 1 '+ device + ' > /dev/null', shell=True)
  currentstate = subprocess.call('sudo arping -q -c10 -w500000 '+ device + ' > /dev/null', shell=True)
 
  if currentstate == 0 : lastsuccess=datetime.datetime.now()
  if currentstate == 0 and currentstate != previousstate and lastreported == 1 : 
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, no need to tell domoticz")
  if currentstate == 0 and currentstate != previousstate and lastreported != 1 :
    if domoticzstatus() == 0 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, tell domoticz it's back")
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=On&level=0" + "&passcode=" + domoticzpasscode)
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " online, but domoticz already knew")
    lastreported=1
 
  if currentstate == 1 and currentstate != previousstate :
    log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, waiting for it to come back")
 
  if currentstate == 1 and (datetime.datetime.now()-lastsuccess).total_seconds() > float(cooldownperiod) and lastreported != 0 :
    if domoticzstatus() == 1 :
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, tell domoticz it's gone")
      domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + switchid + "&switchcmd=Off&level=0" + "&passcode=" + domoticzpasscode)
    else:
      log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + " offline, but domoticz already knew")
    lastreported=0
 
  time.sleep (float(interval))
 
  previousstate=currentstate
  if check_for_instances.lower() == "pid": open(pidfile, 'w').close()





Meppel8
Posts: 8
Joined: Tuesday 06 June 2017 21:20
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: NL
Contact:

Re: Check device online stops working after Domoticz V4.9700 update

Post by Meppel8 » Thursday 12 July 2018 20:33

Hello Sjoerd,

Thanks for the modified script, my first impression was right that it had something to do with the Python library. I have installed it on domotics and I have the following error:

Line 27: import urllib.request
ImportError: No module named request

Did't you have that error? Is there something wrong with my python library after the update to V4.9700.

Thanks for your help I have spend many hours already.

Best regard Rene

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest