Getting the daily temperatures and precipitation from NWS forecasts in Home Assistant

This is a snippet that you can use to create sensors for the day’s high and low temperature from the NWS integration: high temperature, low temperature, daytime precipitation chance, and nighttime precipitation chance.

These sensors can be used in automations for your climate control with {{ states('sensors.wx_temp_high') }} or to be displayed on a dashboard.

It uses the Twice Daily forecast action, so should only be ran once per day. From my experience, the forecast is generated before 6AM central time so I have this running a 6AM.

To handle situations where the NWS forecast is incorrectly formatted or unavailable, this sets a temperature of -200°F (since the coldest ever recorded is -126°F, this should be a safe number) and precipitation chances to -200%. These should be handled in whatever automations you use the temperature sensors in. I’ve included YAML of an automation to alert you of this error below.

Since you can’t create template sensors directly in the Home Assistant UI, this snippet needs to be added to your configurations.yml file. You can edit the file directly on your server or by using the File Editor add-on or Studio Code Server add-on.

You’ll need to change weather.ktcl_daynight to the correct entity ID for your NWS integration.

template:
  - trigger:
      # Run each day at 6AM
      - trigger: time_pattern
        hours: 6
        minutes: 0
    
    action:
      - action: weather.get_forecasts
        target:
          # Change this to your entity ID
          entity_id: weather.ktcl_daynight
        data:
          type: twice_daily
        response_variable: wx_data
      - variables:
          # Change this to your entity ID
          daytime: >-
            {% for wx in wx_data["weather.ktcl_daynight"]["forecast"] if as_datetime(wx["datetime"]).date() == now().date() and wx["is_daytime"] %}
              {{ wx }}
            {% else %}
              {"temperature": -200, "precipitation_probability": -200}
            {% endfor %}
          # Change this to your entity ID
          nighttime: >-
            {% for wx in wx_data["weather.ktcl_daynight"]["forecast"] if as_datetime(wx["datetime"]).date() == now().date() and not wx["is_daytime"] %}
              {{ wx }}
            {% else %}
              {"temperature": -200, "precipitation_probability": -200}
            {% endfor %}
    
    sensor:
      ###
      # Create high temperature sensor
      ###
      - name: High temperature
        unique_id: wx_temp_high
        state: "{{ daytime.get('temperature') }}"
        unit_of_measurement: °F
        
      ###
      # Create low temperature sensor
      ###
      - name: Low temperature
        unique_id: wx_temp_low
        state: "{{ nighttime.get('temperature') }}"
        unit_of_measurement: °F
        
      ###
      # Create daytime precipitation sensor
      ###
      - name: Daytime chance of precipitation
        unique_id: wx_precipitation_daytime
        state: "{{ daytime.get('precipitation_probability') }}"
        unit_of_measurement: "%"
        
      ###
      # Create nighttime precipitation sensor
      ###
      - name: Nighttime chance of precipitation
        unique_id: wx_precipitation_nighttime
        state: "{{ nighttime.get('precipitation_probability') }}"
        unit_of_measurement: "%"

Outdoor Temperature Sensor

Using the daytime sensor, you can create a template sensor that returns the high (daytime) or low (nighttime) temperature.

{% if states('binary_sensor.daytime') == 'on' %}
  {{ states('sensor.wx_temp_high') }}
{% else %}
  {{ states('sensor.wx_temp_low') }}
{% endif %}

Error Notification Automation

This is an automation for creating a persistent notification when the weather sensors are in an error state.

alias: Notification on weather sensor error state
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.wx_temp_high
    below: -199
  - trigger: numeric_state
    entity_id:
      - sensor.wx_temp_low
    below: -199
  - trigger: numeric_state
    entity_id:
      - sensor.wx_precipitation_daytime
    below: -199
  - trigger: numeric_state
    entity_id:
      - sensor.wx_precipitation_nighttime
    below: -199
conditions: []
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      message: >-
        One or more of the weather sensors from the NWS forecast are in an error
        state as of {{ now().strftime('%m/%d/%Y') }}.
      notification_id: wx_sensors_in_error_state
mode: single

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *