How To Get Weather Forecast Min/Max Temps

I’d like to setup an automation that will give me the day’s forecasted min and max temperatures, but I’m having trouble finding an entity to use.

I’ve currently got the Google Weather integration working and when I go into the integration and click on the Home sensor it will give me a very thorough forecast that has min/max daily, twice daily and hourly. This is exactly what I’m looking to use, but I can only find a singular temperature entity. Any idea how to reference these min/max values?

Thanks in advance for any help!

I think the standard is that the high and low forecast temp are in forecast[0]. So the forecast high is in forecast[0].temperature and the forecast low is in forecast[0].templow.

That may vary a bit depending on your weather provider though. Those are the names I have for my Apple Weatherkit integration.

If you go into the actions area of the developer tools, you can call the weather.get_forecasts action (make sure to add a target and pick which forecast type), you’ll get the raw forecast data. That should tell you the names of the things.

I ran the weather.get_forecasts action and it did give me information back, but the forecast doesn’t appear to be part of the data returned. I’m using Google’s weather service, I was trying to avoid paying Apple $100 per year just to get the weather.

Am I missing something perhaps?

It seems to use the temperature and templow on my openweathermap integration.



But you have to run daily check, now hourly. When I test for hourly, I don’t have the templow in response.

Yep, shitty weather :slight_smile:

1 Like

Yes,

in weather forecasts, hourly entries are normally seen as point-in-time entries, not as time-ranges like the daily entries.

(Which is technically not correct, but an hourly forecast based on these models is already inaccurate enough.For more fine grained values of the next 1 or 2 hours of clouds/rain you could look for a so-called radar based “nowcast instead”. But for temperature the hourly forecast values are the best you will get.)

So there is only a single temperature for hourly forecast items, but a range (temp and temp-low) for daily forecast items.

I use National Weather Service which is free. Here’s my complete template - pick and choose what you want, This should work with other weather services also.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
    sensor:
      - name: weather_home_hourly
        unique_id: weather_hourly
        state: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
        attributes:
          forecast: "{{ hourly['weather.home'].forecast }}"
  - sensor:
      - name: "weather_forecast_latency"
        unit_of_measurement: "secs"
        state: >-
          {% set ts = as_timestamp(states("sensor.weather_home_hourly"),0) %}
          {% if ts == 0 %}
          0
          {% else %}
          {{ ((as_timestamp(now(), 0) | int(0)) - ts | int(0)) }}
          {% endif %}
  - sensor:
      - name: "weather_forecast_high"
        unit_of_measurement: "°F"
        state: >-
          {{ state_attr('sensor.weather_home_hourly','forecast') |
             selectattr('datetime', 'search', now().timestamp() | timestamp_custom('%Y-%m-%d')) | 
             map(attribute='temperature') | max
          }}
        device_class: temperature
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_forecast_low"
        unit_of_measurement: "°F"
        state: >-
          {% set sunrise = as_timestamp(state_attr('sun.sun', 'next_rising')) | as_datetime | as_local %}
          {{ (state_attr('sensor.weather_home_hourly', 'forecast')|selectattr('datetime', 'lt', sunrise.isoformat() ))| map(attribute='temperature') | list | min}} 
        device_class: temperature
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_forecast_dewpoint_high"
        unit_of_measurement: "°F"
        state: >-
          {{ state_attr('sensor.weather_home_hourly','forecast') |
             selectattr('datetime', 'search', now().timestamp() | timestamp_custom('%Y-%m-%d')) | 
             map(attribute='dew_point') | max
          }}
        device_class: temperature
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_forecast_dewpoint_low"
        unit_of_measurement: "°F"
        state: >-
          {% set sunrise = as_timestamp(state_attr('sun.sun', 'next_rising')) | as_datetime | as_local %}
          {{ (state_attr('sensor.weather_home_hourly', 'forecast')|selectattr('datetime', 'lt', sunrise.isoformat() ))| map(attribute='dew_point') | list | min}} 
        device_class: temperature
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_forecast_precipitation"
        unit_of_measurement: "%"
        state: >-
          {% set sunrise = as_timestamp(state_attr('sun.sun', 'next_rising')) | as_datetime | as_local %}
          {{ (state_attr('sensor.weather_home_hourly', 'forecast')|selectattr('datetime', 'lt', sunrise.isoformat() ))| map(attribute='precipitation_probability') | list | max}} 
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_current_condition"
        state: >-
          {{ state_attr('sensor.weather_home_hourly','forecast')[0].condition }}
        attributes:
          precipitation: "{{ state_attr('sensor.weather_home_hourly','forecast')[0].precipitation_probability }}"          
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_forecast_current"
        unit_of_measurement: "°F"
        state: >-
          {{ state_attr('sensor.weather_home_hourly','forecast')[0].temperature }}
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - sensor:
      - name: "weather_heat_cut"
        state: >-
          {%- set max_temp = states("sensor.weather_forecast_high")|float(0) %}
          {%- set weather = states("sensor.weather_current_condition") %}
          {%- set sun_on_house = states("binary_sensor.sun_angle_on_house") %}
          {%- set elevation = state_attr("sun.sun","elevation")|float(0) %}
          {%- set rising = state_attr("sun.sun","rising") %}
          {%- set offset = 0.0 %}
          {%- if sun_on_house == "on" %}
            {%- if weather == "sunny" or weather == "exceptional" %}
              {%- set offset = 1.5 %}
            {%- elif weather == "partlycloudy" %}
              {%- set offset = 1 %}
            {%- endif %}
          {%- endif %}
          {%  if max_temp >= 50 %}
          {%    set offset = offset + 1 %}
          {%  elif max_temp >= 40 %}
          {%    set offset = offset + 0.5 %}
          {%  elif max_temp >= 30 %}
          {%    set offset = offset + 0.25 %}
          {%  elif max_temp >= 20 %}
          {%    set offset = offset %}
          {%  else %}
          {%    set offset = 0 %}
          {%- endif %}
          {{ offset|int(0) }}
        availability: '{{ is_state("binary_sensor.weather_online", "on") }}'
  - binary_sensor:
      - name: sun_angle_on_house
        state: >-
          {{ states('sun.sun') == 'above_horizon' and state_attr('sun.sun','elevation')|float(0) > 7 }}
      - name: weather_open_windows
        state: >-
          {% set min = states("sensor.weather_forecast_low")|int(0) %}
          {% set dp = states("sensor.weather_forecast_dewpoint_low")|int(100) %}
          {% set precip = states("sensor.weather_forecast_precipitation")|int(100) %}
          {% if (min < 63 and min > 50) and dp < 61 and precip < 5 %}
          true
          {% else %}
          false
          {% endif %}
      - name: weather_online
        state: >-
          {{ states("sensor.weather_forecast_latency")|int(0) < 7700 }}

recorder:
  include:
    entities:
      - sensor.weather_forecast_high
      - sensor.weather_forecast_low
      - sensor.weather_forecast_dewpoint_high
      - sensor.weather_forecast_dewpoint_low
      - sensor.weather_current_condition
      - sensor.weather_forecast_current
      - sensor.weather_forecast_precipitation
      - sensor.weather_heat_cut
      - binary_sensor.sun_angle_on_house
      - binary_sensor.weather_open_windows
      - binary_sensor.weather_online
      - sensor.weather_forecast_latency

Thank you very much! I was able to get it working.

1 Like