Need Guidance on Making Template Changes for 0.118+

I am using the rest sensors for NWS Alerts as described in https://community.home-assistant.io/t/severe-weather-alerts-from-the-us-national-weather-service/71853

I have my sensors configured in configuration.yaml as:

  - platform: rest
    resource: https://api.weather.gov/alerts/active?zone=[Zone IDs Redacted]
    name: NWS Alert Event Raw
    value_template: >
      {% if value_json.features[0] is defined %}
        {{ value_json['features'][0]['properties'].event }}
      {% else %}
        None
      {% endif %}
    json_attributes:
      - features
    headers:
      User-Agent: Homeassistant
      Accept: application/geo+json
    scan_interval: 60
  
  - platform: template
    sensors:
      nws_alert_event_filtered:
        friendly_name: NWS Alert Event
        value_template: >
          {% if is_state('sensor.nws_alert_event_raw', 'unavailable') or is_state('sensor.nws_alert_event_raw', 'unknown') %}
            {{ states.sensor.nws_alert_event_filtered.state }}
          {% else %}
            {{ states.sensor.nws_alert_event_raw.state }}
          {% endif %}

I then have conditional Lovelace Cards that display only when there is an alert with the following code:

type: conditional
conditions:
  - entity: sensor.nws_alert_event_filtered
    state_not: none
card:
  cards:
    - entity: sensor.nws_alert_event_filtered
      type: entity
    - content: >-
        {% if states.sensor.nws_alert_event_raw.attributes.features[0] is
        defined %}
         
         {{states.sensor.nws_alert_event_raw.attributes.features[0].properties.headline }}

         <b> Details </b>
           {{states.sensor.nws_alert_event_raw.attributes.features[0].properties.description }}

          <b>Instructions</b>

          {{states.sensor.nws_alert_event_raw.attributes.features[0].properties.instruction }}
        {% else %}
          none
        {% endif %}
      type: markdown
  type: vertical-stack

With the release of 0.118 my lovelace cards began displaying the card and text “Unknown” and “None” in a box directly below the unknown when no events were active instead of hiding the card.
When there are no events the endpoint for the raw sensor returns something like this:

{
    "@context": {
        "@version": "1.1"
    },
    "type": "FeatureCollection",
    "features": [],
    "title": "current watches, warnings, and advisories",
    "updated": "2020-12-10T16:00:00+00:00"
}

I found that there were breaking changes in Templates with 0.118 with the helpful advice to add

homeassistant:
  legacy_templates: true

as a transitional fix. This fixes the problem and my cards behave properly – displaying the alert if active or hiding the card if no alerts are active. However, I don’t know what changes I need to make to my sensors and/or lovelace card configuration in order to resolve the breaking template issue long-term. I am currently running 2020.12.7 on Hass.io HassOS 4.17

Could you please point me in the right direction?

I’ll have to dig in and see what’s changed.

I’ve only had a couple of alerts and but never serious enough to notice that things weren’t working.

1 Like

I couldn’t figure this out either but after asking for guidance in another thread the solution will be to change the “raw” sensor template to return a different result when there are no alerts.

try this:

- platform: rest
    resource: https://api.weather.gov/alerts/active?zone=[YOUR ZONE]
    name: NWS Test Alert Event Raw
    value_template: >
      {% if value_json.features[0] is defined %}
        {{ value_json['features'][0]['properties'].event }}
      {% else %}
        No Active Alerts
      {% endif %}
    json_attributes:
      - features
    headers:
      User-Agent: Homeassistant
      Accept: application/geo+json
    scan_interval: 60

But…

I have moved away from using those sensors a long time ago.

I recommend moving to my NWS Alerts custom integration:

You can install it manually or via HACS.

Thank you for your help on this – the configuration change resolved the issue with the template. I then changed the conditional in my Lovelace conditional card to
state_not: No Active Alerts
to match the configuration.yaml that fully resolved the issue.

I do need to move over to the custom integration. Originally, I just understood the REST sensor better. As an aside, thank you for all your time and effort for this sensor / integration it is much appreciated.

1 Like