With the addition of PING in RM 5.1 (under 2.2.9.140), I successfully built a set of rules which checks my internet connection on a periodic basis, and if down, cycles an AEOTEC smart switch 7 OFF. I mention the switch, only because that particular switch has an Auto-On setting which you can set a timer (1 minute is the shortest) to automatically cycle the switch ON when turned off. This makes it a very effective reset option.
I broke the rule into 4 separate rules, primarily for ease of development and testing. I also have several virtual switches and strings (with connectors) which allow you to both test the rule actions, and to manually trigger portions of the rule separately. I've detailed everything in the section below for anyone interested.
DETAILS
Hub Variables
CronPingRate - a String to set a variable Period trigger (minutes) using Cron. Added in .140, still not fully implemented and requires a little extra code to pick up when the string is changed. Sample shows a Period of 1 minute, I start with 10 or 15 when the network is up, and go to 1 when it is down.
DownRate - A number with connector to allow you to change the Network Test rate (Cron) when the network is down. I use 1 (1 minute) by default.
UpRate - A number with connector to change the Network Test rate (Cron) when the network is up. I use 15 minutes by default.
InternetTest; ModemTest - strings with connectors to set IPs for Ping test locations. This just allows you to easily change the Ping IP from a dashboard vice going into the RM code itself. The routines ping both an external internet IP, and an internal Modem IP (explained in the Network Test code).
PingLoss - a number to capture the %value% result of the Ping.
PingRate - a number to set the current testing frequency - the starting point to change the CronPingRate.
PingText - a text to capture the %text% result of the Ping.
Virtual Switches. The following virtual switches are used:
Auto Reset - Used as a predicate to suspend the rules when auto-action is not desired.
Cable Modem Reset - Used as a manual trigger to reset the modem (cycles the power plug).
CronReset - Virtual switch with 2 sec auto-on to reset the Cron routine when PingRate and CronPingRate (Cron Period) are changed. Used as a work-around, explained further in Cron Update / Cron Trigger routines.
Internet Down / Modem Down - Two virtual switches to indicate the respective Pings was unsuccessful and trigger the reset routine.
Internet Ping - Virtual switch with 1 sec auto-off to trigger the Network Test routine. Used both manually, and by the Cron Trigger routine.
Network Down Simulator - simulates a loss of Internet as an added conditional in the Network Test routine, also disables the Cable Modem Reset routine through a predicate condition to avoid power cycling the modem. Used for testing.
ROUTINES
Cron Update
Captures a change in rate (UpRate, DownRate, or PingRate). Applies the change to PingRate if applicable, updates the Cron String CronPingRate, then triggers the CronReset switch to restart Cron Trigger with the new rate (period).
Cron Trigger
The primary timer routine using a Cron period timer. This triggers Network Test through the virtual switch Internet Ping. The Test Trigger is just another virtual switch I used in testing (500ms auto off) and just haven't removed.
The CronReset Predicate resets the Cron timer when the Cron String is updated through a virtual switch with 2 second auto-off. This is a current work-around as updates to the Cron String otherwise are not captured by the timer.
Network Test
Primary Network Test rule:
PREDICATE: Auto Reset is a virtual switch which controls all the Network Test / Reset apps.
TRIGGER: Triggered by a virtual switch INTERNET PING with a 1 sec auto-off. This allows the routine to easily be triggered manually or by other apps.
ACTIONS:
Two separate tests are included - an Internet Test (currently using DNS lookup 8.8.8.8 IP address), and an internet network MODEM test using the Modem IP. While a single Internet test would be sufficient, I've had issues where my particular modem HTML admin script was not responding, even though the internet connection was working fine. Therefore this test routine checks both.
Both checks are similar:
PING the address using a string variable, the variable just allows me to set a different IP address without changing the app itself.
Capture the %text% and %value% responses in Hub Variables and log them. I noticed that the responses weren't logged otherwise and I wasn't sure if the Ping was initially working. You can skip the hub variable and just log %text% %value% directly, added when I was building the routines and didn't end up using them anywhere else.
I've been getting PingLoss responses (%value% of 0 or 100). 3 pings, either all go through (0) or none (100).
If 100% loss, then turn ON INTERNET DOWN virtual switch which triggers the network reset routines. Also set PingRate to DownRate which is increased testing (set to every minute currently) when the network is down until it comes back up. Also sends a notification to my phone which understandably will not get through with the network down.
ALSO a second conditional virtual switch 'Network Down Simulator' also triggers the routine and allows me to test the routines remotely without taking down the network.
ELSE - if InternetDown was ON, meaning the network just came back up, then notify my phone the network was restored, which will (should) happen. Reset Virtual Switch InternetDown to OFF.
The ModemTest Ping actions work the same, except I only added the simulator switch on the InternetTest routine.
Finally, If both Internet Down and Modem Down virtual switches are off, reset the PingRate to the UpRate (less frequent polling when network is up).
This is placed at the end separate from the two test routines to otherwise avoid possible looping. If you opt for only a single test routine, then you can place it with the ELSE actions.
Cable Modem Reset
Main reset routine.
Cable Modem is a z-wave switch which has an 'auto-on' setting after 1 minute (a built-in switch setting). After you turn it off, it will automatically turn back on after 1 minute, which removes a possible error of lost z-wave transmission, hub is also down/etc. This is the plug that power-cycles the modem.
Alternatively you can build a separate rule that is triggered on Plug Off and turns it back on after a suitable delay.
PREDICATE CONDITIONS: Auto Reset is a virtual switch that allows me to turn all associated Network Test / Reset rules on or off. Network Down Simulator is another virtual switch that simulates a downed network that I used to test the Cron routines. This disables this routine, which actually power cycles the plug, to keep the network up during testing. It is also used in the Network Test routine as a separate conditional. This was especially important since these routines were built for a remote location.
TRIGGERS: Main trigger is virtual switch CABLE MODEM RESET. Also triggered by either INTERNET DOWN (ID) or MODEM DOWN (MD) virtual switches. The only difference between ID and MD is that ID pings Googles DNS lookup 8.8.8.8, MD pings the local cable modem IP. Both are described in more detail in NETWORK TEST.
The routine turns off the modem plug, then waits for switches 'Modem Down' and 'Internet Down' to turn off, which indicates the network is back up, and finally resets the Cable Modem Reset switch.
Dashboard.
This shows the various switches/connectors in a dashboard which brings most together. A couple other switches are included for my VPN (FireWalla) and a presence detector which I used previously but wasn't working as reliably.
EDIT:
I found that during a reset with no IP address, PING returns transmitted 0, received 0 with a loss of 0 which equated to the network being up in my routines. Additionally, checking both external and internal networks created a partial loop which resulted in 3 separate resets before the network came back up. I made some adjustments to correct both these conditions.
I also modified my NETWORK DOWN SIMULATOR switch so that it DOES reset the modem (I removed the predicate from the Cable Modem Reset routine), but it automatically turns off after the first reset to also simulate the network coming back up. This allows me to actually test the routine by taking down the network and going through the modem reboot routine.
Details here.
NETWORK TEST Changes in Bold Italics with additional remarks in [ ].
Ping %InternetTest%
Set PingLoss to %value% as integer
Set PingText to '%text%'
Log: 'Internet %PingLoss% %PingText%'
IF (Variable PingLoss = 100 OR
Variable PingText contains 'Transmitted: 0' OR
Network Down Simulator is on) THEN
IF (Network Down Simulator is on) Off: Network Down Simulator
IF (Internet Down is off) Notify Dave’s iPhone: '%date% %time% Everett Dr. Internet is down.'
[moved the notification above the switch and added the conditional to prevent repeat notifications each time through the test.]
On: Internet Down.
Set PingRate to DownRate
ELSE
IF (Internet Down is on) Notify Dave’s iPhone: '%date% %time% Everett Dr. Internet was reset.'
Off: Internet Down
END-IF
Ping %ModemTest%
Set PingLoss to %value% as integer
Set PingText to '%text%'
Log: 'Modem %PingLoss% %PingText%'
IF (( Variable PingLoss = 100 OR
Variable PingText contains 'Transmitted: 0'
) AND
Modem Down, Internet Down all off) THEN
[added conditional to only take action on internal network if external network is up.]
On: Modem Down
Set PingRate to DownRate
Notify Dave’s iPhone: '%date% %time% Everett Dr. Cable Modem is unreachable.'
ELSE
IF (Modem Down is on) Notify Dave’s iPhone: '%date% %time% Everett Dr. Modem was reset.'
Off: Modem Down
END-IF
IF (Modem Down, Internet Down all off) Set PingRate to UpRate[/details]
CHRON TRIGGER
Removed the trigger to run on a change in ChronPingRate which was generating an unnecessary additional test cycle each time the network went up or down.
Cable Modem Reset
I removed the Network Down Simulator switch OFF predicate which prevented the reset routine from running during a test cycle.