Real-Time Severe Weather Alerts in Home Assistant

Having access to weather data is a built-in feature in Home Assistant. However one thing that the built-in Home Assistant weather integration does not account for is severe weather alerts. Do you want an alert when a thunderstorm is approaching and you have windows open? Or maybe you want to notify everybody in your household that a tornado is headed their way? Having the NWS sever weather alerts as a sensor in your Home Assistant server allows for all of those ideas and more.

Prerequisites

The only prerequisite for adding Home Assistant weather alerts is having your Home Assistant server set up. Any automation that you would add after the alerts may require more, but for the sensors themselves, this is all you need.

NWS Weather Alerts

NOAA’s National Weather Service sends out severe weather alerts on a constant basis. In order to receive those alerts, they have created an API that you can connect to. This allows us easy access to adding weather alerts to Home Assistant. Weather alerts are grouped by zone so you will need to know what your alert zone is before we can add the sensor.

Finding your Weather Alert Zone

In order to find your zone, you will want to go to https://alerts.weather.gov/ . Scroll down towards the bottom of the page and there will be a list of states. Select your state and it will bring up a list of counties in your state. Find your county and then write down the zone code. You now have all of the information you need to get weather alerts in to your server.

Pulling in alerts for your zone

Now that we have the zone code, we can check the alerts to make sure it is working. The url for the weather alerts is “https://api.weather.gov/alerts/active?zone=[INSERT ZONE HERE]”. If you do not have an active weather alert in your area at the time you set this up, the response will not give the information we are looking for. Below is what a weather alert does look like, so we can break it down from there.

{
    "@context": [
        "https://geojson.org/geojson-ld/geojson-context.jsonld",
        {
            "@version": "1.1",
            "wx": "https://api.weather.gov/ontology#",
            "@vocab": "https://api.weather.gov/ontology#"
        }
    ],
    "type": "FeatureCollection",
    "features": [
        {
            "id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.76814495f7c2aed97c80b5a9061a4d423c578a62.001.1",
            "type": "Feature",
            "geometry": null,
            "properties": {
                "@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.76814495f7c2aed97c80b5a9061a4d423c578a62.001.1",
                "@type": "wx:Alert",
                "id": "urn:oid:2.49.0.1.840.0.76814495f7c2aed97c80b5a9061a4d423c578a62.001.1",
                "areaDesc": "Wise; Denton; Collin; Hunt; Parker; Tarrant; Dallas; Rockwall; Kaufman; Hood; Johnson; Ellis; Henderson",
                "geocode": {
                    "SAME": [
                        "048497",
                        "048121",
                        "048085",
                        "048231",
                        "048367",
                        "048439",
                        "048113",
                        "048397",
                        "048257",
                        "048221",
                        "048251",
                        "048139",
                        "048213"
                    ],
                    "UGC": [
                        "TXZ102",
                        "TXZ103",
                        "TXZ104",
                        "TXZ105",
                        "TXZ117",
                        "TXZ118",
                        "TXZ119",
                        "TXZ120",
                        "TXZ121",
                        "TXZ131",
                        "TXZ133",
                        "TXZ134",
                        "TXZ135"
                    ]
                },
                "affectedZones": [
                    "https://api.weather.gov/zones/forecast/TXZ102",
                    "https://api.weather.gov/zones/forecast/TXZ103",
                    "https://api.weather.gov/zones/forecast/TXZ104",
                    "https://api.weather.gov/zones/forecast/TXZ105",
                    "https://api.weather.gov/zones/forecast/TXZ117",
                    "https://api.weather.gov/zones/forecast/TXZ118",
                    "https://api.weather.gov/zones/forecast/TXZ119",
                    "https://api.weather.gov/zones/forecast/TXZ120",
                    "https://api.weather.gov/zones/forecast/TXZ121",
                    "https://api.weather.gov/zones/forecast/TXZ131",
                    "https://api.weather.gov/zones/forecast/TXZ133",
                    "https://api.weather.gov/zones/forecast/TXZ134",
                    "https://api.weather.gov/zones/forecast/TXZ135"
                ],
                "references": [],
                "sent": "2022-08-10T14:56:00-05:00",
                "effective": "2022-08-10T14:56:00-05:00",
                "onset": "2022-08-10T14:56:00-05:00",
                "expires": "2022-08-11T18:00:00-05:00",
                "ends": null,
                "status": "Actual",
                "messageType": "Alert",
                "category": "Met",
                "severity": "Unknown",
                "certainty": "Unknown",
                "urgency": "Unknown",
                "event": "Air Quality Alert",
                "sender": "w-nws.webmaster@noaa.gov",
                "senderName": "NWS Fort Worth TX",
                "headline": "Air Quality Alert issued August 10 at 2:56PM CDT by NWS Fort Worth TX",
                "description": "The Texas Commission on Environmental Quality (TCEQ) has issued\nan Ozone Action Day for the Dallas-Fort Worth area for Thursday,\nAugust 11, 2022.\n\nAtmospheric conditions are expected to be favorable for producing\nhigh levels of ozone air pollution in the Dallas-Fort Worth area\non Thursday. You can help prevent ozone pollution by sharing a\nride, walking, riding a bicycle, taking your lunch to work,\navoiding drive-through lanes, conserving energy, and keeping your\nvehicle properly tuned.\n\nFor more information on ozone:\n\nOzone: The Facts (www.tceq.texas.gov/goto/ozonefacts)\n\nAir North Texas: (www.airnorthtexas.org)\n\nEPA Air Now\n(www.airnow.gov/index.cfm?action.local_state&STATEID=45&TAB=0)\n\nTake care of Texas (www.takecareoftexas.org)\n\nNorth Central Texas Council of Governments Air Quality\n(www.nctcog.org/trans/air/index.asp)",
                "instruction": null,
                "response": "Monitor",
                "parameters": {
                    "AWIPSidentifier": [
                        "AQAFWD"
                    ],
                    "WMOidentifier": [
                        "AEUS74 KFWD 101956"
                    ],
                    "NWSheadline": [
                        "OZONE ACTION DAY"
                    ],
                    "BLOCKCHANNEL": [
                        "EAS",
                        "NWEM",
                        "CMAS"
                    ]
                }
            }
        }
    ],
    "title": "current watches, warnings, and advisories for Tarrant (TXZ118) TX",
    "updated": "2022-08-10T22:00:00+00:00"
}

This API response will look a little different than what we have seen in my post about RESTful sensors. That is because this response is not in JSON format. When we add the rest sensor for the weather alert, we will append a header that will give us the response in JSON.
We will not setup the sensor for the Home Assistant weather alerts. For this you will need to go into your configuration.yaml file.

sensor:
  - platform: rest
    resource: https://api.weather.gov/alerts/active?zone=[YOUR ZONE HERE]
    name: Sever Weather Alert Event
    value_template: >
      {% if value_json.features[0] is defined %}
        {{ value_json['features'][0]['properties'].description }}
      {% else %}
        None
      {% endif %}
    json_attributes:
      - features
    headers:
      User-Agent: Homeassistant
      Accept: application/geo+json
    scan_interval: 60

Breaking Down the Sensor

You will set up the sensor and since it will be a rest sensor, we will add the – platform: rest line.
Resource is the url that you would like the rest sensor to pull data from.
Name is where you will type in whatever name you choose to name this sensor.
Value template is where things get interesting. Since the api does not send a full response when there is no weather alert, we need to insert an if statement. Looking at the example alert above, you will see that the weather alert is grouped in an array called “features.” Without an alert, this “features” array will be returned empty, so we will start our if statement by checking if it has information inside of it. If it does, then we want to pull the information, if it is undefined, we will have the sensor return “None.”
The information that we want for this specific sensor is the full weather alert description. You can use just the name of the event, in which case you will change .description to .event but for this, we will pull the full description.
json_attributes is where we will define where we want the information pulled from. Since we want the information in the “features” dictionary, we will add -features.
Headers are sent along with the api request so add the user-agent of homeassistant and accept will be application/geo+json which will get us the response in json for us.
Scan interval is how often we will have this sensor pull data. Some APIs have rate-limiting, so it is good practice to add this to your rest sensors. This sensor will poll the data every 60 seconds.

Final Thoughts

Once you have this home assistant weather alert, you can add it to any automations you would like. When you set the trigger for the automation, look for a state change from None to not none, and you will have the automation run when the next alert pops up.

One response to “Real-Time Severe Weather Alerts in Home Assistant”

Leave a Reply

Your email address will not be published. Required fields are marked *