Ping Action

I am having a bit of a problem - my cable modem goes down and becomes unresponsive, and a reboot will fix it. Forget about replacing the modem or getting the isp involved - not happening. Anyway - I was wondering, could I setup a ping on HASS to say ping google dns servers every x minutes - if no response - cycle this zwave appliance switch? I’ve got the idea, but not sure how to handle it…not asking someone to just hand me an answer as I’m still learning HASS, but I need some solid direction.

Thanks for any ideas…

Thanks!!

Well, you can use nmap (https://home-assistant.io/components/device_tracker.nmap_scanner/)

this will create an item in HA fro each element (IP) in your network. When that item status move from ‘Not home’ to home I’d say is the moment to need to recycle the Zwave appliance.

would this work for you??

The command_line binary sensor contains a ping example. Then add a simple automation rule with a trigger and an action that changes the state of your Z-Wave switch.

1 Like

Thanks for the input guys - I think that command line sensor is what I wa slooking for, didn’t see that when I posted (like I said learning HASS, don’t know all the sensors/components that are available in terms of remembering them all even after browsing over them…) - let me see if I can get that going. Appreciate the help!!!

Do/Can I put this ping sensor on some kind of timer?

@vexter0944

I couldn’t get any of the examples to work. I did create a work around and it seemed to be polling every 5min.
I added the scan_interval to the component and it seems to have brought it down to every 3 minutes. I have not messed with it much since.

Anyway, it is really easy to turn on a light if the sensor’s state goes to off.

@ptp - thanks for all the testing/help! It shows you deleted the post - do you have the final code to show how you polled the internet? I’d be curious to see how you put it all together. Appreciate all of the help!

@vexter0944

The post I deleted was a wash. I tried using the ping condition from the speedtest.net component. I posted the setup without testing. I quickly found out that the component holds its last state if connection is lost. I would be happy to show you what I did to make it work.

#Create a Shell Script

In the .homeassistant directory I made a file call ping.sh. The file looks like this.

#! /bin/bash

ping -w 5 8.8.8.8 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "Success"
else
    echo "Fail"
fi

The script pings google five times and reports after its done. After you have that done you have to make it executable:

chmod +x ping.sh

Finally, modify your path to add the directory where your script is located:

export PATH=$PATH:/home/user/.homeassistant

To test it you can run:

sh ping.sh

it should return “Success” or “Fail”

Create a Sensor

binary_sensor:
  platform: command_line
  scan_interval: 30
  name: WAN
  command: "/home/user/.homeassistant/ping.sh"
  payload_on: "Success"
  payload_off: "Fail"

I used the scan_interval in my setup but I am not sure if it is doing anything.
#Create an Automation
automation:
- alias: turn on light when internet goes down
trigger:
platform: state
entity_id: sensor.your_sensor_name
to: ‘off’
action:
service: light.turn_on
entity_id: light.your_light_name

You will need a second, similar automation to turn it off. Hope this works out for you!!

Regards,

6 Likes

@PtP - sorry so long to reply - been travelling. I will most certainly take a look at this when I get home - I appreciate the help very much! Looks like something I can put together and try out and tweak as needed. MUCH appreciated!

No problem!

@PtP - Thanks for putting me on an EXCELLENT path! Got it all up and running,

Just a couple of notes -

  1. I put the ping.sh script in /usr/bin - had some kind of issue with the PATH situation above and noted from some forum reading that /usr/bin should work (and it did…!)
    2, The sensor was dead on - worked perfectly.
  2. I had the automation for the sensor state calling a script - looks like this -
    "cable_modem_reset:
    sequence:
    • service: switch.turn_off
      entity_id: switch.leviton_dzpa11lw_plugin_appliance_module_switch_5
    • delay: 00:00:30
    • service: switch.turn_on
      entity_id: switch.leviton_dzpa11lw_plugin_appliance_module_switch_5"

So far working like a champ in testing!

MUCH MUCH appreciate all the help!

1 Like

@vexter0944

I am happy to hear it is working for you!! HA is very powerful and there is usually a work around for niche needs.

Regards,

1 Like

Mike, thanks for posting this. Please forgive my lack of knowledge. How does executing ping -w 5 8.8.8.8 checks Google? Why are you using 8.8.8.8 as the IP address to ping? Thanks again for sharing all this, it’s very helpful

1 Like

8.8.8.8 is the dns server for Google on the internet - so by pinging it, you’re effectively checking to see if the internet/cable modem is working by getting a response from it. Any address on the internet outside of your network is an ip you can use to ping and check connectivity, which is the point of all this setup in this thread. Hope this helps!

2 Likes

Great to know that! Thanks for the response!

You’re very welcome!