custom-components / weatheralerts

A sensor that gives you weather alerts from alerts.weather.gov.
MIT License
126 stars 15 forks source link

Zone and County Issue #46

Closed chasklar closed 3 years ago

chasklar commented 3 years ago

Version of the custom_component version 0.1.4

Describe the bug Getting warnings and errors when restarting and randomly.

Log Latest logs.


Add your logs here.
```This error originated from a custom integration.

Logger: custom_components.weatheralerts.sensor
Source: custom_components/weatheralerts/sensor.py:145
Integration: weatheralerts (documentation)
First occurred: 9:31:37 AM (8 occurrences)
Last logged: 10:11:34 AM

[TXZ205,TXC325] Possible API outage. Currently unable to download from weather.gov - HTTP status code 502
[TXZ203,TXC463] Possible API outage. Currently unable to download from weather.gov - HTTP status code 502

Error1

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:72
Integration: Template (documentation, issues)
First occurred: 9:31:35 AM (21 occurrences)
Last logged: 10:11:35 AM

TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[1] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'sensor.weatheralerts_3_alert_2'
TemplateError('UndefinedError: list object has no element 3') while processing template 'Template("{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[3] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[3].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'sensor.weatheralerts_3_alert_3'
TemplateError('UndefinedError: list object has no element 0') while processing template 'Template("{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[0] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[0].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'sensor.weatheralerts_2_alert_1'
TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[1] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'sensor.weatheralerts_2_alert_2'
TemplateError('UndefinedError: list object has no element 2') while processing template 'Template("{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[2] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[2].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'sensor.weatheralerts_2_alert_3'

Error 2

Logger: homeassistant.helpers.template
Source: helpers/template.py:1378
First occurred: 9:31:31 AM (139 occurrences)
Last logged: 10:14:35 AM

Template variable error: list object has no element 1 when rendering '{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[1] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}'
Template variable error: list object has no element 3 when rendering '{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[3] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[3].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}'
Template variable error: list object has no element 0 when rendering '{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[0] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[0].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}'
Template variable error: list object has no element 1 when rendering '{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[1] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}'
Template variable error: list object has no element 2 when rendering '{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[2] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[2].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}'

Warning 3

Logger: homeassistant.helpers.event
Source: helpers/template.py:391
First occurred: 9:31:35 AM (6 occurrences)
Last logged: 9:31:35 AM

Error while processing template: Template("{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[1] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")
Error while processing template: Template("{% if (states('sensor.weatheralerts_2') != 'unavailable') and (state_attr('sensor.weatheralerts_2', 'alerts')[2] != null) or ((states('sensor.weatheralerts_2') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_2', 'alerts')[2].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")
Error while processing template: Template("{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[0] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[0].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")
Error while processing template: Template("{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[1] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[1].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")
Error while processing template: Template("{% if (states('sensor.weatheralerts_3') != 'unavailable') and (state_attr('sensor.weatheralerts_3', 'alerts')[3] != null) or ((states('sensor.weatheralerts_3') == 'unavailable') and (as_timestamp(state_attr('sensor.weatheralerts_3', 'alerts')[3].endsExpires) - as_timestamp(now()) > 0)) %} on {% else %} off {% endif %}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 389, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1359, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1289, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 924, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 326, in getattr
    value = getattr(obj, attribute)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1383, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1375, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: None has no element 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 505, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 391, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: None has no element 0

Warning 4

This error originated from a custom integration.

Logger: custom_components.weatheralerts.sensor
Source: custom_components/weatheralerts/sensor.py:118
Integration: weatheralerts (documentation)
First occurred: 9:31:33 AM (2 occurrences)
Last logged: 9:31:34 AM

[ContentTypeError] 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://api.weather.gov/alerts/active?zone=TXZ205')
[ContentTypeError] 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://api.weather.gov/alerts/active?zone=TXZ203')
jlverhagen commented 3 years ago

This is normal and expected behavior for the WeatherAlerts integration when the weather.gov API is experiencing problems. The initial error indicates a HTTP status code error 502, which means the api.weather.gov API was having an issue at that time. HTTP status code error 502 = Bad Gateway error. Bad Gateway error = the WeatherAlerts request went to the public-facing API web server, the public-facing API web server forwarded it to a NWS internal server that serves the alerts back to the public-facing API web server, and the public-facing API web server is complaining it didn't receive a valid response from the NWS internal server. 100% a problem somewhere within the weather.gov API. The weather.gov weather alerts API is not 100% reliable, unfortunately. Any weatheralerts-related TimeoutError and HTTP status code errors you might see in the home-assistant.log file are just pointing out how unreliable the API can be. The weather.gov API is free, likely receives a very high volume of requests, and it's run by the U.S. government, so occasional or intermittent failures are likely.

The template errors will happen whenever there is an API error. The template errors can be ignored. Additional code could be added to the template to prevent the errors, but the added code complicates the template and makes it even more unmanageable than it is already. The template errors also show up in the log during Home Assistant startup if the weatheralerts templates load before the WeatherAlerts integration is loaded, and these can also be ignored. The template package will be replaced, at some point, with a much simpler and easier to manage template package.

One other note... Right now, if the integration runs into any of these API errors when Home Assistant is starting up, WeatherAlerts will fail to load if the errors persist longer than the time Home Assistant tries and retries to load the integration. I'll see if I can change this behavior and allow WeatherAlerts to continue to startup, and then store the startup failure details in an entity attribute until WeatherAlerts is able to successfully connect to the weather.gov API.