The Tallest Dwarf

RSSEmailMastodonWheelchairBlueskyNewsletterTwitterGitHubDribbbleLinkedInFacebookInstagramYouTubePinterestReddit icon

Restart modem automatically when Internet is down

Posted at — Dec 13, 2024 by Abishek Muthian

If I have to restart my modem often like a caveman, I'll at least automate it.

Years ago I would have pitied the soul which had to restart their Internet modem often, Now I'm in the situation where the ISP seems to have not heard about network reliability and so my modem hangs at least once per day.

My modem is completely managed by the ISP and so there are no useful options for me to fix the issue. Even the scheduled reboot doesn't work if the modem hangs before the scheduled time. I use a separate OpenWRT router for internal networking and security.

It became clear that I have to automate the power cycling of the modem and for that I used Node RED in Home Assistant, smart plug which allows me to control locally and Gotify to inform me when this all happens.

I found a fellow high-tech neanderthal on reddit doing the same, taking some inspiration from them I created my own work of art -

Node RED flow to restart modem in Home Assitant
Node RED flow to restart modem in Home Assitant

Sorry about the spaghetti code, I'm a time-constrained person and so I promise to improve it incrementally.

It works like this,

  1. Ping google, Increment counters for ping failure and high latency respectively.
  2. If the modem_reset_timestamp is null/empty or more than an a hour old then forward the fail counter value else do nothing (to avoid endless modem restart during ISP downtime).
  3. If the ping has failed more than 5 times then switch off the smart plug and switch on after 5 seconds. Send Gotify messages for the same.
  4. Reset the fail counter, set modem_reset_timestamp.
  5. When the high latency counter is >1000, send a Gotify message about the same.

Ping Evaluation

// Ping Evaluation

var MsgFail = {Payload: "PING FAIL"};
var MsgHighPing = {Payload: "HIGH PING"};
var MsgNormalPing = {reset: "true"};

// if ping fails, message goes out first input
// if ping is slow, message goes out second
// if ping is normal, message goes out third

if (msg.payload == false){
return [MsgFail, null];
} else if (msg.payload > 150) {
return [null, MsgHighPing];
} else if (msg.payload < 150) {
return [MsgNormalPing, MsgNormalPing];
}

Reset Timestamp Evaluation

// reset_timestamp_eval

const modem_reset_timestamp = flow.get('modem_reset_timestamp');

function isMoreThanAnHourOld(modemResetTimestamp) {
const ONE*HOUR = 60 * 60 \_ 1000; // 1 hour in milliseconds
const currentTimeInMilliseconds = new Date().getTime(); // Current time in milliseconds

// Calculate the difference between the modem reset timestamp and the current time
const diff = currentTimeInMilliseconds - modemResetTimestamp;

// Check if the difference is greater than one hour
return diff > ONE_HOUR;
}

if (modem_reset_timestamp == undefined || modem_reset_timestamp == null){
node.warn('The timestamp is null or undefined.');
return [{ count: msg.count }, null];
}
else if (isMoreThanAnHourOld(modem_reset_timestamp)) {
node.warn('The timestamp is more than an hour old.');
return [{ count: msg.count }, null];
} else {
node.warn('The timestamp is not more than an hour old.');
return [null, null];
}

And this works spectacularly,

Modem restart messages on Gotify
Modem restart messages on Gotify, Yeah I should probably add timestamp to the messages

So well that it doesn't hurt as much as it used to during the countless hours setting up this automation instead of coordinating with other customers to force the ISP to get its act together.

Newsletter

I strive to write low frequency, High quality content on Health, Product Development, Programming, Software Engineering, DIY, Security, Philosophy and other interests. If you would like to receive them in your email inbox then please consider subscribing to my Newsletter.