Bullet proofing weather entity data

With the recent outage of the NWS weather API (in the US), someone asked how to go about supplementing certain weather data with another service.

I use the Template Weather Provider integration to create my own weather.backyard entity.

Here is an example of what that could look like based on what I’m using (NWS and Accuweather):

weather:
  - platform: template
    name: "Backyard"
    unique_id: "backyard_weather_32"
    condition_template: >-
      {% if states('weather.kgrr_daynight') not in ['unavailable','unknown'] %}
        {{ states('weather.kgrr_daynight') }}
      {% elif states('weather.accuweather') not in ['unavailable','unknown'] %}
        {{ states('weather.accuweather') }}
      {% endif %}
    temperature_template: >-
      {% if states('weather.kgrr_daynight') not in ['unavailable','unknown'] %}
        {{ state_attr('weather.kgrr_daynight','temperature') |float(0) }}
      {% elif states('weather.accuweather') not in ['unavailable','unknown'] %}
        {{ state_attr('weather.accuweather','temperature') |float(0) }}
      {% endif %}

You would need to do this for each attribute that you plan to use (e.g. temperature, humidity, winds, etc.). You can also use other local temperature and humidity sensors as well. See link above for full listing and further examples about how to use the integration.

1 Like

A slightly cleaner way to do this would be to use has_value :

weather:
  - platform: template
    name: "Backyard"
    unique_id: "backyard_weather_32"
    condition_template: >-
      {% if has_value('weather.kgrr_daynight') %}
        {{ states('weather.kgrr_daynight') }}
      {% elif has_value('weather.accuweather') %}
        {{ states('weather.accuweather') }}
      {% endif %}
    temperature_template: >-
      {% if has_value('weather.kgrr_daynight') %}
        {{ state_attr('weather.kgrr_daynight','temperature') |float(0) }}
      {% elif has_value('weather.accuweather') %}
        {{ state_attr('weather.accuweather','temperature') |float(0) }}
      {% endif %}

You can use a for-loop to iterate through multiple weather providers. It reports the first provider that has a valid value.

weather:
  - platform: template
    name: "Backyard"
    unique_id: "backyard_weather_32"
    condition_template: >-
      {% for x in ['weather.kgrr_daynight','weather.accuweather'] if has_value(x) %}
      {{ states(x) }}{% break %}
      {% endfor %}
    temperature_template: >-
      {% for x in ['weather.kgrr_daynight','weather.accuweather'] if has_value(x) %}
      {{ state_attr(x, 'temperature') | float(0) }}{% break %}
      {% endfor %}
3 Likes

Nice. Thanks!

1 Like

@cbhii for once, I don’t need to change the template as I am kgrr as well!

1 Like

Same here!

Nice. Hey neighbors!

Great idea!

1 Like