How to test an NWS Weather Alert?

I installed the NWS integration and can see weather for my location. I created the automation below from a website slightly modified. Is there any way to test an NWS alert automation?

-Thanks

alias: Test NWS Notification
description: ""
triggers:
  - trigger: state
    entity_id:
      - weather.ustn0365
    from: "off"
    to: "on"
conditions: []
actions:
  - action: notify.mobile_app_pixel_8
    metadata: {}
    data:
      title: New NWS Alert
      message: |-
        {% set alerts = state_attr('sensor.nws_alerts_alerts','Alerts') %}
        {{ alerts[0].Headline }}
mode: single
  1. Use the run command in the UI editor to test it without the trigger.
  2. Add a 2nd trigger temporarily and force that to test the automation.
  3. Go to developer-states and change the trigger state manually.
    Open your Home Assistant instance and show your state developer tools.

I did part of what you suggested already. I added a virtual switch as a trigger that I can flip. But when I did I didn’t receive a notification. Then I tried running the notification itself and got the error below.

Also, in Developer Tools → States it already indicates the NWS state is “on”. Should I change this then?

-Thanks

You’re assuming that alerts is a list with at least one element. The error message suggests that the value of that variable is none.

Thanks. That’s part of the reason I was asking how to test this. Coincidentally, I received a severe weather notification from my NOAA rule on my Hubitat. High winds around here! But nothing from the NWS. Bummer.

I’ll research this further…

You can test a lot of Jinja code in Developer Tools → Template.

It gets a little tricker when you’re debugging code in automations that uses context-specific stuff, like the trigger variable. But you can type in things like, for example,

{% set alerts = state_attr('sensor.nws_alerts_alerts','Alerts') %}
{{ alerts }}

And you instantly see if alerts is a scalar, a list, none, etc.

Your automation only fires when weather.ustn0365 goes from off to on. Are you sure that’s the exact transition that will happen when you have severe weather that you want to be notified about? That seems dubious… take a look at this page listing the states that a weather entity can take.

Thanks for the developer tools info. To answer your question, I was not sure of the off to on parameters. I just changed it to “Any state”. That seems like it should cover it, at least for testing purposes!

-Thanks

Update: I had no idea there were so many weather integrations! I need to look into this further.

Assuming that there will be an Alerts attribute on the weather entity ever, it seems like you can’t assume that any one of those states actually does or does not come with an alert.

Possibly a better approach is to trigger your automation on changes to the entity’s attribute, with conditions to make sure it has something to report, like this:

alias: Test NWS Notification
description: ""
triggers:
  - trigger: state
    entity_id:
      - weather.ustn0365
    attribute: Alerts
conditions:
  {{
      state_attr('weather.ustn0365') is list
        and
      state_attr('weather.ustn0365')|length
  }}
actions:
  - action: notify.mobile_app_pixel_8
    metadata: {}
    data:
      title: New NWS Alert
      message: |-
        {% set alerts = state_attr('sensor.nws_alerts_alerts','Alerts') %}
        {{ alerts[0].Headline }}
mode: single

If that is closer to the approach you want, some refinement might be needed depending on how the integration updates the attribute.

If you only care about alerts in certain weather conditions, you can add state conditions on the state of the weather entity.

Thank you!

I copied and pasted your YAML into a new automation. But when I try to save it I get a 500 error.

Ha. I don’t use the GUI Automation editor, but presently I am getting that same error no matter what I try to do with the GUI editor, including if I try to create a simple automation based on the state of a light using the dropdown boxes.

Thank you, I also found the updated guide below. But I’m having problems with the binary sensor indicating it is “Unavailable”. I think it’s a syntax error with the sensor template.

How to Set Up NWS Alerts in Home Assistant (v6+ Guide) - This Smart House