Community. Driven. Weather. Data.Always Ad-FreeDeveloper FriendlyChecking statusDiscord
Become a supporter
Sign in

Subscribe to live alerts over MQTT

Make an MQTT credential, connect to the broker from Python and print each NWS alert for your saved places as it is published.


You will write a short Python program that connects to the WxAlerts MQTT broker with a credential of your own and prints each alert for your saved places as it arrives. You need Python 3 and a WxAlerts account. Install the MQTT library first, version 2.0 or newer:

pip install paho-mqtt

1. Save a place and make a credential

  1. Sign in, open Places and save a place, such as your home. Alerts for the places you save are what your credential receives.
  2. Open Credentials, choose New credential, pick MQTT credential and Create credential.
  3. Copy the username, the password and your alert topic. The password is shown once. The topic looks like V2/12345/NWS/#, where the number is your user id.

See Make an MQTT credential or API key for the page in detail.

2. Write the client

Save this as alerts.py. It reads the username, password and topic from the environment so they never sit in a file you might share.

import json
import os
import paho.mqtt.client as mqtt

TOPIC = os.environ["WXA_TOPIC"]   # for example V2/12345/NWS/#

def on_connect(client, userdata, flags, reason_code, properties):
    if reason_code.is_failure:
        print("refused:", reason_code)
        return
    client.subscribe(TOPIC, qos=1)

def on_message(client, userdata, msg):
    alert = json.loads(msg.payload)
    print(msg.topic)
    print(alert["event"], "|", alert["status"], "|", ", ".join(alert["places"]), "|", alert["url"])
    if alert["description"]:
        print(alert["description"])

client = mqtt.Client(
    mqtt.CallbackAPIVersion.VERSION2,
    client_id="my-unique-client",
    transport="websockets",
)
client.username_pw_set(os.environ["WXA_USER"], os.environ["WXA_PASS"])
client.tls_set()
client.on_connect = on_connect
client.on_message = on_message
client.reconnect_delay_set(min_delay=30, max_delay=300)
client.connect("mqtt.wxalerts.org", 443)
client.loop_forever()

The client talks MQTT over a WebSocket on port 443 with TLS, which is the only way into the broker. Change my-unique-client to a name of your own: two clients that share a client ID take each other’s session over. The reconnect delay keeps a failing client under the 2 connection attempts a minute your account is allowed.

3. Run it

WXA_USER=ha-xxxxxxxxxxxx WXA_PASS=... WXA_TOPIC='V2/12345/NWS/#' python3 alerts.py

Nothing prints at first, and that is normal. Alerts are not retained, so you hear an alert from the moment it is issued or changed, not the ones already in effect. Leave the program running: when the NWS issues or updates an alert that covers one of your places, it prints:

V2/12345/NWS/KSEW/52HcPf5Z
Storm Warning | active | Home, Dock | https://wxalerts.org/alerts/52HcPf5Z/
Southwest winds 35 to 45 kt with gusts up to 55 kt. Seas 12 to 17 ft.

Each message carries the alert’s full text in description and instruction, either of which is null when the alert has none.

Stop it with Ctrl+C. To see the alerts already in effect, ask the alerts REST API once at start up; see No retained messages, and how to catch up.

You now have a client that is told about alerts instead of asking for them. To make it reliable enough to leave running, see Build a reliable MQTT client.

View this page as Markdown

May we use Microsoft Clarity to see how pages are used (clicks, scrolling and replays of a visit, with anything you type hidden), plus a random ID linking that to crash reports? Only to find bugs and confusing screens. Never advertising, never sold.