Hi. This is for people with two internet connections that are load balanced and ensuring your load balancing works properly.
There are three components
- Something to actively check load balancing
- A visual to visually inspect load balancing
- An automation to tell you when load balancing is not working
It checks 11 different sites for “what is my IP”. The responses should be distributed.
This is just hacked together and forces IPv4. you could verify with IPv6 also - easy change. I may improve it in the future like having a docker image for it. But for now, I’m sharing the quick solution.
Part 1
Here’s a python script. set it up as a cron job somewhere and configure the details for an already integrated MQTT server
import requests
import paho.mqtt.client as mqtt
import logging
import json
import re
CONST_MQTT_HOST=""
CONST_MQTT_USERNAME=""
CONST_MQTT_PASSWORD=""
requests.packages.urllib3.util.connection.HAS_IPV6 = False
# List of websites to fetch payloads from
websites = [
'http://ifconfig.me',
'http://icanhazip.com',
'http://ipinfo.io/ip',
'http://ident.me',
'http://ipecho.net/plain',
'http://checkip.amazonaws.com',
'http://wtfismyip.com/text',
'http://ifconfig.io',
'https://api.ipify.org',
'http://whatismyip.akamai.com',
'http://2ip.io',
'https://ipecho.net/plain',
'http://ifconfig.me',
'http://ip.me'
]
# Function to get payload from HTTP website
def get_http_payload(url):
headers = {'User-Agent': 'curl/7.54'}
response = requests.get(url,headers=headers)
if response.status_code == 200:
return response.text
else:
print(f'Failed to get payload from {url}')
return None
def replace_periods(ip_address):
return re.sub(r'\W', '_', ip_address)
# Function to publish payload to MQTT topic
def publish_to_mqtt(payload,website):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
client.username_pw_set(CONST_MQTT_USERNAME,CONST_MQTT_PASSWORD)
client.connect( CONST_MQTT_HOST, 1883)
ipaddress=replace_periods(website)
mqtt_payload = {}
mqtt_payload["name"]= f"whatismyip_{ipaddress}"
mqtt_payload["state_topic"]=f"homeassistant/sensor/whatismyip_{ipaddress}/state"
mqtt_payload["device_class"]=None
mqtt_payload["unique_id"]=f"whatismyip_{ipaddress}"
device = {}
device["identifiers"] = mqtt_payload["unique_id"]
device["name"] = f"What Is My IP Response For {website}"
mqtt_payload["device"] = device
mqtt_payload = json.dumps(mqtt_payload)
client.publish(f"homeassistant/sensor/whatismyip_{ipaddress}/config", payload=mqtt_payload, qos=0, retain=False)
client.publish(f"homeassistant/sensor/whatismyip_{ipaddress}/state", payload=payload, qos=0, retain=False)
client.disconnect()
# Main process
if __name__ == '__main__':
for website in websites:
# Get payload from HTTP website
payload = get_http_payload(website)
payload_strip = payload.strip()
logger = logging.getLogger(__name__)
logger.info(f"Website {website} reports {payload_strip}")
print(f"Website {website} reports {payload_strip}")
# Publish payload to MQTT
publish_to_mqtt(payload_strip,website)
- A visual
Create a history card with all the entities. you should see a lot of color variation. This is what mine looks like.
- An automation config looks something like this. it will tell you when the state hasn’t changed - likely because the IP hasn’t changed.
alias: "WHATISMYIP | NO STATE CHANGE | TELEGRAM NOTIFY "
description: ""
trigger:
- platform: state
entity_id:
- >-
sensor.what_is_my_ip_response_for_http_checkip_amazonaws_com_whatismyip_http_checkip_amazonaws_com
- sensor.what_is_my_ip_response_for_http_2ip_io_whatismyip_http_2ip_io
- >-
sensor.what_is_my_ip_response_for_http_icanhazip_com_whatismyip_http_icanhazip_com
- sensor.http_ident_me
- >-
sensor.what_is_my_ip_response_for_http_ifconfig_io_whatismyip_http_ifconfig_io
- sensor.http_ifconfig_me
- sensor.what_is_my_ip_response_for_http_ip_me_whatismyip_http_ip_me
- >-
sensor.what_is_my_ip_response_for_http_ipecho_net_plain_whatismyip_http_ipecho_net_plain
- >-
sensor.what_is_my_ip_response_for_http_ipinfo_io_ip_whatismyip_http_ipinfo_io_ip
- >-
sensor.what_is_my_ip_response_for_http_whatismyip_akamai_com_whatismyip_http_whatismyip_akamai_com
- >-
sensor.what_is_my_ip_response_for_http_wtfismyip_com_text_whatismyip_http_wtfismyip_com_text
- >-
sensor.what_is_my_ip_response_for_http_checkip_amazonaws_com_whatismyip_http_checkip_amazonaws_com_2
- >-
sensor.what_is_my_ip_response_for_https_api_ipify_org_whatismyip_https_api_ipify_org
- >-
sensor.what_is_my_ip_response_for_https_ipecho_net_plain_whatismyip_https_ipecho_net_plain
for:
hours: 0
minutes: 30
seconds: 0
condition: []
action:
- service: telegram_bot.send_message
metadata: {}
data:
message: Internet load balancing is not working
mode: single