Detect when network down

Posted on
Thu Nov 03, 2016 11:16 am
DVDDave offline
Posts: 470
Joined: Feb 26, 2006
Location: San Jose, CA

Detect when network down

Is there a way in Indigo to detect when the internet connection goes down, maybe by periodically pinging a known site? I would use this to reset my modem and router in an attempt to correct the problem. If not, please consider this as a feature request. Thanks.

Posted on
Thu Nov 03, 2016 11:43 am
jalves offline
Posts: 745
Joined: Jun 16, 2013

Re: Detect when network down

I like this idea. I know that when the reflector goes down I see a message in the log. I wonder if its possible to use that as a trigger?

Running Indigo 2023.2 on a 24" iMac M1), OS X 14.4
Jeff

Posted on
Thu Nov 03, 2016 2:02 pm
Different Computers offline
User avatar
Posts: 2544
Joined: Jan 02, 2016
Location: East Coast

Re: Detect when network down

I have a script that I used to check for power cycling the router if the internet went down, because I have pretty crappy internet service.

I posted this once before, but since then I've come to the realization that the script doesn't actually work! It's close though, so here it is again.
This scripts assumes/requires an Indigo variable called "Internet"

REPEAT. This script does NOT WORK currently in my live Indigo environment.
Code: Select all
repeat with i from 1 to 2
   try
      do shell script "ping -o -t 2 www.apple.com"
      exit repeat
      --set value of variable "Internet" to "true"
      tell application "IndigoServer"
         set value of variable "Internet" to "true"
   on error
      --set Internet to "false"
      delay 2
      tell application "IndigoServer"
         set value of variable "Internet" to "false"
      end tell
   end try
end repeat


It has been a couple of months since I messed with this script, but I recall getting it to work in some manual testing. Frankly, my internet got SO bad for a while that I turned off this scheduled check, and then my internet got reliable, and I haven't messed with it again.

Edit: messing with it now, and it seems like the problem is after the do shell script line succeeds, it stops without executing anything else.

In my setup, the script above ran as a schedule. I had an Indigo trigger watching goodConnect for "false", and that would act to turn my modem off and on via an X10 receptacle.

Ok, this is untested in a live environment, but I think the logic works now
Code: Select all
repeat with i from 1 to 2
   try
      do shell script "ping -o -t 2 www.apple.com"
        --set value of variable "Internet" to "true"
      tell application "IndigoServer"
         set value of variable "Internet" to "true"
exit repeat
       on error
      --set Internet to "false"
      delay 2
      tell application "IndigoServer"
         set value of variable "Internet" to "false"
      end tell
   end try
end repeat

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Fri Nov 04, 2016 7:30 am
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Detect when network down

Morning,

I hope you don't mind I was playing around with your code and wasn't able to get it to work. It appears that when I pulled my cable out to drop my connection it wouldn't change from true to false. I searched around the internet because I know very little about AppleScript or Python for a basic AppleScript to check for internet connection. and found something I then modified that code and combined it with your on whats seemed to make sense to me and came up with this. It appears to be working for me. Just thought I would share.


Code: Select all
repeat with i from 1 to 2
   try
      do shell script "ping -o -t 2 www.apple.com"
      exit repeat
   on error
      delay 2
      tell application "IndigoServer"
         set value of variable "Internet" to "false"
         if i = 2 then error number -128
      end tell
   end try
end repeat
tell application "IndigoServer"
   set value of variable "Internet" to "true"
end tell

Posted on
Fri Nov 04, 2016 10:50 am
Different Computers offline
User avatar
Posts: 2544
Joined: Jan 02, 2016
Location: East Coast

Re: Detect when network down

Awesome if it works for you!

I had to test it on a non-Indigo machine yesterday, and couldn't get some of it to work in some of the ways that are working for you.

Hope it does good work for you.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Fri Nov 04, 2016 11:06 am
kw123 offline
User avatar
Posts: 8365
Joined: May 12, 2013
Location: Dallas, TX

Re: Detect when network down

I strong recommend NOT to use AppleScript for these kinds of things.
Eg if your internet is down:the AppleScript will wait until indigo times it out ~ 10 secs (here it is set to 2 secs internally but anyway)
During these seconds plugins can NOT get any info from indigo or update and indigo state (eg does device xx exist or set state xx to ..) the queries and updates of states etc will bomb.
Then the plugin might assume that a device does not exist and tries to recreate the drive..

PLEASE PLEASE do not use AppleScript for these things..

python script do not behave that way!!

Karl

Posted on
Fri Nov 04, 2016 11:17 am
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Detect when network down

I began trying to figure out how to do this in Python and even found some code to do so on the net but the problem was I couldn't figure out how to get python to update the variable. Again I am new to any type of coding and only pay around with what I can find and attempt to make sense out of it.

Posted on
Fri Nov 04, 2016 11:48 am
kw123 offline
User avatar
Posts: 8365
Joined: May 12, 2013
Location: Dallas, TX

Re: Detect when network down

the attached will update 2 variables:

1." publicIpnumber" set to your public ipnuber of your router
2. "Internet" to true or false if it can connect / not connect to either ip.appspot.com or checkip.dyndns.org

Karl


UPDATED code to print error message to log file when connection errors
UPDATED to create variable if they do not exist
Code: Select all
import subprocess, sys
try:
   try: indigo.variable.create("Internet")
   except: pass
   try: indigo.variable.create("my_IP_address")
   except: pass
   ip= subprocess.Popen("/usr/bin/curl --max-time 4 ip.appspot.com",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
   ipitems= ip.split(".")   
   if "This application is temporarily over its serving quota" not in ip and len(ip) > 10 and len(ipitems) ==4:
         indigo.variable.updateValue("my_IP_address", ip )
         indigo.server.log("publicIpnumber:"+ ip)
         indigo.variable.updateValue("Internet", "True" )
         return
   else:
      msg= subprocess.Popen("/usr/bin/curl --max-time 4 http://checkip.dyndns.org",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
      ip = msg.split("Current IP Address: ")
      if len(ip) > 1:
         ip = ip[1].split("</body>")[0]
         ipitems= ip.split(".")
         if len(ip) > 10 and len(ipitems) ==4:
            indigo.variable.updateValue("my_IP_address", ip )
            indigo.server.log("publicIpnumber:"+ ip)
            indigo.variable.updateValue("Internet", "True" )
            return
   indigo.variable.updateValue("Internet", "False" )
   indigo.server.log("trying to get publicIpnumber, return: "+ msg)
except  Exception, e:
   indigo.server.log(u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
indigo.variable.updateValue("Internet", "False" )
Last edited by kw123 on Fri Nov 04, 2016 3:40 pm, edited 3 times in total.

Posted on
Fri Nov 04, 2016 12:35 pm
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Detect when network down

Thanks for this I do have a question though when you refer to public number I am assuming you mean the public static IP given from my ISP and not the 192.168.X.X. I ask because I have both variables set but I am getting an error when I execute


Script Error embedded script: Python argument types in
VariableCmds.updateValue(VariableCmds, str, bool)
did not match C++ signature:
updateValue(_VariableCmds {lvalue}, boost::python::api::object elem, CCString value)
Script Error Exception Traceback (most recent call shown last):

embedded script, line 21, at top level
ArgumentError: Python argument types in
VariableCmds.updateValue(VariableCmds, str, bool)
did not match C++ signature:
updateValue(_VariableCmds {lvalue}, boost::python::api::object elem, CCString value)



When I put the Public IP in the variable I get this error

cript Error embedded script: ElementNotFoundError -- could not find variable "my_IP_address"
Script Error Exception Traceback (most recent call shown last):

embedded script, line 17, at top level
ValueError: ElementNotFoundError -- could not find variable "my_IP_address"

Posted on
Fri Nov 04, 2016 1:11 pm
kw123 offline
User avatar
Posts: 8365
Joined: May 12, 2013
Location: Dallas, TX

Re: Detect when network down

try out the new version (UPDATED CODE).. yes its the public IP number

you need to create the variable first

Posted on
Fri Nov 04, 2016 1:14 pm
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Detect when network down

tried the updated code when I ran it it did not put anything in to the variable for Public IP but if I entered the IP address into the variable I got this error

Script in Line '20' has error='ElementNotFoundError -- could not find variable "my_IP_address"'

Posted on
Fri Nov 04, 2016 3:41 pm
kw123 offline
User avatar
Posts: 8365
Joined: May 12, 2013
Location: Dallas, TX

Re: Detect when network down

the newly posted version will create 2 variables:

Internet
my_IP_address

Internet: True / False
my_IP_address: the public ip number assigned by your inertness company assigned to your router .

Karl

Posted on
Fri Nov 04, 2016 4:03 pm
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Detect when network down

works perfect thank you

Posted on
Sun Dec 04, 2016 11:50 am
tiff offline
Posts: 7
Joined: Nov 29, 2016

Re: Detect when network down

This works great, thank you. My network is distant wi-fi, fast but not too reliable.
Adjusting curl to be: "/usr/bin/curl --connect-timeout 10 --max-time 10
Seemed to fix false alarms.

I added a schedule to run it every 3 minutes.

I set 2 triggers:
a trigger for the variable 'Internet' to become false, and it fills in a timestamp 'Internet _Went_Down_Time'
and a trigger for the variable 'Internet' to become true, so it sends me an email with the timestamp using AppleScript:

property emailAddress : "MY EMAIL ADDRESS"
property emailSubject : "Indigo: Internet has been down"

tell application "IndigoServer"
set downTime to value of variable "Internet _Went_Down_Time"
set emailBody to "Internet went down: " & Internet _Went_Down_Time
send email to emailAddress with subject emailSubject with body emailBody
end tell

Is there a way to get this timestamp variable displayed in the Indigo's Notification Actions>Send Email dialog instead of using AppleScript?

Posted on
Fri Nov 22, 2019 9:38 am
t-star offline
Posts: 115
Joined: Oct 26, 2007

Re: Detect when network down

Is there a way to create a trigger based on the reflector connection status? That could be an indicator of network connection (for those that use it).

Who is online

Users browsing this forum: No registered users and 7 guests