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?