Internet Status Monitoring with Home Assistant and Node-RED

If you’ve read previous posts on my blog, you’ll undoubtedly know that I’ve had some issues with my internet connection with Comcast. In the last few years the service has been fairly decent, especially in the cooler months. But for some reason when it gets hot outside (90F+ on average) the connection drops throughout the day. My suspicion, and I have no actual proof to back this up outside of the temperature, is that there’s a piece of hardware in an equipment shed somewhere that’s overheating, or becoming unstable due to inadequate cooling. I’ve tried customer service, but, as expected got nowhere with them. They either apologized and said there’s really nothing they can do, or they’d try to “push a fix to my modem”. Even after explaining that there have been multiple techs out to the house only to find that there’s nothing wrong either in the house or to the pole on the street. The only thing I seem to be able to do is complain and get nothing resolved.

Initially, this project was just to help automate the rebooting of my modem. But after I was seeing 20+ disconnects a day, I decided to also incorporate tweets into the solution.

Prerequisites

    • Home Assistant with the following
    • A controllable switch which is connected to your modem
      • I use a GE Z Wave Switch, but anything that’s integrated with Home Assistant should work.   

Home Assistant – Switches and Sensors

In Home Assistant, you’ll need to do the following:

    1. Ensure that you can control your switch. Depending on the kind of switch you’re using the configuration and setup will be different, so I’m doing to skip that in the post.
      1. The home assistant docs, and the home assistant and home automation reddit pages are quite helpful.
    2. Create a binary sensor which evaluates if the internet is up or down.
      1. In your configuration.yaml file, add a binary_sensor: entry if you don’t have one, otherwise append to the existing one.
        1. binary_sensor:
            - platform: ping  
              host: google.com  
              count: 5  
              scan_interval: 30  
              name: "Internet Status"
          
           
        2. You can choose whatever domain you like you ping. I just chose google as they’re probably up more than Comcast is /s

Node-RED – What do do when the connection drops

The flow isn’t all that complicated. I’ll give a quick overview before we dive into specifics.

    • When the Internet Status goes from 1 to 0
    • Start a 2 minute timer 
      • Sometimes there are hiccups.
    • Check the internet status again
    • If the status is still 0 (down), then turn off the power to the modem
    • Start a 30 second timer
    • Turn the power to the modem back on
    • Check the Internet Status every 10 seconds until it’s back up
    • Compose the message for the tweet
    • Replace the datetime token with the current datetime
    • Send the Tweet

Step 1 – Triggering on Status change

Use a State Trigger to detect that the Internet has gone down.

Only trigger if the state has gone from “on” to “off”.

Step 2 – Wait to make sure it’s actually down

Sometimes the connection will hiccup and show as down. To combat this, we’ll just wait a bit before we go through the whole process of “Power Down, Power Up”, which could take up to 5 minutes to complete (for me anyway). So we’ll add a stoptimer and set it to 2 minutes. This can be changed to your preferences, but I think it’s a good amount for my needs.

Step 3 – Check the Internet Status

We’ll use the Current State function node to check the Internet Status. Once you’ve set the properties, the node should have two outputs. One if “state is on” is false, and the other if “state is on” is true. We’ll link Step 3 to Step 4 using the “If State is False” output.

        

Step 4 – Turn off the Modem

To turn off the modem we’re going to use the Service Call function.

And we’re going to tell Home Assistant to turn off the switch associated with the modem. I happen to have renamed mine to switch.ge_media_room_modem_switch.

Step 5 – Wait a bit for you know, reasons.

We’ll use another stop timer to wait before we turn the modem back on. 30 seconds sounds good, right? It’ll be the same as step 2, add another Stop Timer except this time we’re only running for 30 seconds.

Step 6 – Delete the payload message

In order to turn things off and on in the same flow, we’ll need to delete the current payload message, otherwise Node-RED will just use the same payload value as before (turn_off). To achieve this, we’ll use the Change function node.

Step 7 – Turn the modem back on

Now that we have a fresh empty payload again, we’ll use another Call Service node and tell Home Assistant to turn the power back on.

Step 8 – Wait a bit before checking the Internet Status

In order to send out tweet, we’ll need to have an internet connection that’s working. So now we wait. For about 10 seconds. Add another Stop Timer and set it for 10 seconds

Step 9 – Check the Internet Status

We’ll use the Current State function node again to check the Internet Status. This time, we’ll link the “false” output back to the input of the timer in step 8, the true will continue on to the input of the node in step 10.

        

Step 10 – Set the Tweet message

We’ll use another Change function node here, but instead of just deleting the payload message, we’ll be setting it to a specific value; our tweet. You can tweet whoever you want, I tweet the Comcast support account.

Here’s my message:

Hi @ComcastCares, I just wanted to let you know that the internet went down again and i had to reboot my modem at %timestamp%. #downdetection #automation #terribleservice #homeassistant

Step 11 – Replace the timestamp token in the message (Optional)

Since my message contains a timestamp token (“%timestamp%”) I want to send along with the tweet, I need to be able to get the current datetime. I do this with a function node and run a tiny bit of javascript.

var date = new Date();
msg.payload = msg.payload.replace("%timestamp%", date);
return msg;

Step 12 – Send the tweet!

To send the tweet we’ll use the Twitter OUTPUT function node. Now we can send the tweet! Well, once you’ve configured your credentials. 

If your properties window looks like this, then you’ll need to set your credentials first. Click the pencil to get to the next screen.

From here you’ll set your twitter account you want to use, and then after following the steps in step 1, enter in your api and access token information.

 

Once you’ve done that, your user should show in the list and you can start sending tweets!

 

Published by Dan

I'm a technophile at heart. I love tech and how it can make our lives better. Let's take a little trip into my mind :P

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.