Spring naar de hoofdinhoud

Check external IP address and send a message throug Telegram when it has changed

Screenshot_20250528_110311.png

 

What This Flow Does

This Node-RED flow periodically checks your external IP address and sends a Telegram message if it changes.

Every 30 minutes:

  1. It checks your current external IP address.

  2. If the IP is different from the last recorded one, it:

    • Saves the new IP.

    • Sends a Telegram message to notify you.

This is useful if your internet provider gives you a dynamic IP and you want to track when it changes—ideal for home servers or remote access setups. Here's how it works, step by step:


1. Inject Node: "Check every 30 minutes"

Function: Triggers the flow every 30 minutes.

  • Also runs once at startup after a 5-second delay.

  • Sends the current timestamp as msg.payload.

Screenshot_20250528_110622.png


2. HTTP Request: "Get external IP"

Function: Makes a GET request to https://api.ipify.org.

  • This service returns your current public (external) IP address in plain text.

  • The response becomes the new msg.payload.

Screenshot_20250528_110555.png


3. Function Node: "Check IP address change"

Function: Compares the new IP address to the previously stored one.

  • Retrieves the last known IP from Node-RED's flow memory (flow.get('previousIp')).

  • If the new IP is different, it:

    • Stores the new IP (flow.set('previousIp', currentIp)).

    • Sets a message like Nieuw extern IP-adres gedetecteerd: <new IP>.

    • Passes this message along.

  • If the IP hasn't changed, it stops (returns null).

Screenshot_20250528_110651.png

// Haal het huidige IP-adres op uit de payload
const currentIp = msg.payload;


// Haal het vorige IP-adres op uit de flow context
const previousIp = flow.get('previousIp') || '';


// Vergelijk het huidige IP met het vorige IP
if (currentIp !== previousIp) {
// Bewaar het nieuwe IP in de flow context
flow.set('previousIp', currentIp);
// Stuur een bericht met het nieuwe IP
msg.payload = 'Nieuw extern IP-adres gedetecteerd: ' + currentIp;
return msg;
} else {
// IP is niet veranderd, stop de flow
return null;
}


4. Telegram Sender

Function: Sends the notification via your configured Telegram bot if there's a new IP address.

Screenshot_20250528_110722.png


5. Telegram Bot Configuration
  • Connected to a bot named myhassbot1mybot.

  • Sends messages to a specified user or chat ID.