Sensors to give the hour of min/max temperature from forecast

If you’d like to know when the temperature will be min/max (which hour of the day) and the value, here are four sensors that will get that. This assumes you have the forecast in a sensor called “weather.home_hourly” and that that sensors has an entry for each forecast with attributes ‘temperature’ and ‘datetime’. (I use met.no locally.)

- sensor:
  - name: "Min Forecast Temperature in Next Day"
    unique_id: min_forecast_temperature_in_next_day
    state: >
        {{ state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|min }}
    attributes:
      state_class: measurement
      device_class: temperature
      unit_of_measurement: °C
      icon: mdi:thermometer

- sensor:
  - name: "Min Forecast Temperature Hour in Next Day"
    unique_id: min_forecast_temperature_hour_in_next_day
    state: >
        {% set min_temp = state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|min %}
        {% set min_index = (state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|list).index(min_temp) %}
        {% set coldest_hour = as_timestamp(state_attr('weather.home_hourly','forecast')[min_index]['datetime']) | timestamp_custom("%H") %}
        {{coldest_hour}}
    attributes:
      icon: mdi:hours-24

- sensor:
  - name: "Max Forecast Temperature in Next Day"
    unique_id: max_forecast_temperature_in_next_day
    state: >
        {{ state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|max }}
    attributes:
      state_class: measurement
      device_class: temperature
      unit_of_measurement: °C
      icon: mdi:thermometer

- sensor:
  - name: "Max Forecast Temperature Hour in Next Day"
    unique_id: max_forecast_temperature_hour_in_next_day
    state: >
        {% set max_temp = state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|max %}
        {% set max_index = (state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|list).index(max_temp) %}
        {% set warmest_hour = as_timestamp(state_attr('weather.home_hourly','forecast')[max_index]['datetime']) | timestamp_custom("%H") %}
        {{warmest_hour}}
    attributes:
      icon: mdi:hours-24

2 Likes

Thank you. Just used max temperature part.
Just a small addition met.no entity weather.home_hourly reports forecast for next ~24 hours. So tehcnically it’s not a “Next day” :slight_smile:

I use the following automation to send me a daily message about the weather:-

alias: Todays Temperature
description: ""
trigger:
  - platform: time
    at: "07:30:00"
condition: []
action:
  - device_id: a47f7db07a4a9c7633ac807ab9d13457
    domain: mobile_app
    type: notify
    title: Daily Forecast
    message: >
      Good Morning! The temperature outside is currently
      {{state_attr('weather.forecast_home_hourly','temperature')}} degrees, 
      {%set highof = state_attr('weather.forecast_home_hourly','forecast')
      |       selectattr('datetime', 'search', now().timestamp() |
      timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | max %} {%
      if highof is defined and highof %} with a high of {{highof}} degrees
      {%endif%} {%set lowof =
      state_attr('weather.forecast_home_hourly','forecast') |   
      selectattr('datetime', 'search', now().timestamp() |
      timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | min %} {%
      if lowof is defined and lowof %} and a low of {{lowof}}{%endif%}. The
      weather is forecast to be {{states('weather.forecast_home_hourly')}}{% set
      currently_raining = states('weather.forecast_home_hourly') == 'rainy'
      %}{%set check_for_rain =
      state_attr('weather.forecast_home_hourly','forecast') |
      selectattr('datetime', 'search', now().timestamp() |
      timestamp_custom('%Y-%m-%d')) | selectattr('condition','equalto','rainy')
      | list%}{%if not currently_raining%}{% if check_for_rain is defined and
      check_for_rain %} and rainy. {% else %}, with no rain. {%endif%} {%else%}.
      {%endif%} The house is currently
      {{state_attr('climate.main_house','current_temperature')}} degrees.
mode: single
1 Like

I used this code and it worked very much as intended. It seems to have stopped working since 2024.03 HASS Core update. I tried to update the code to the new entities but failed so far. Has anyone worked on this too?

Thanks for pointing that out @Paluty . Here is my current version which is updated for the new get_forecasts response:

# ###################
# Temperature forecast sensors
# ###################
# Use this to convert from services to JSON: https://jsonformatter.org/yaml-to-json
- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Min Forecast Temperature in Next Day"
      unique_id: min_forecast_temperature_in_next_day
      state: >
        {{ forecast["weather.home"].forecast[:] | map(attribute='temperature')|min }}
      attributes:
        state_class: measurement
        device_class: temperature
        unit_of_measurement: °C
        icon: mdi:thermometer


- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Min Forecast Temperature Hour in Next Day"
      unique_id: min_forecast_temperature_hour_in_next_day
      state: >
        {% set min_temp = forecast["weather.home"].forecast[:] | map(attribute='temperature')|min %}
        {% set min_index = (forecast["weather.home"].forecast[:] | map(attribute='temperature')|list).index(min_temp) %}
        {% set coldest_hour = as_timestamp(forecast["weather.home"].forecast[min_index]['datetime']) | timestamp_custom("%H") | int(0) %}
        {{coldest_hour}}
      attributes:
        state_class: measurement
        unit_of_measurement: h
        icon: mdi:thermometer

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Min Forecast Date in Next Day"
      unique_id: min_forecast_date_in_next_day
      state: >
        {% set min_temp = forecast["weather.home"].forecast[:] | map(attribute='temperature')|min %}
        {% set min_index = (forecast["weather.home"].forecast[:] | map(attribute='temperature')|list).index(min_temp) %}
        {% set coldest_hour = forecast["weather.home"].forecast[min_index]['datetime'] %}
        {{coldest_hour}}
      attributes:
        state_class: measurement
        device_class: timestamp
        icon: mdi:thermometer


- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Max Forecast Temperature in Next Day"
      unique_id: max_forecast_temperature_in_next_day
      state: >
        {{ forecast["weather.home"].forecast[:] | map(attribute='temperature')|max }}
      attributes:
        state_class: measurement
        device_class: temperature
        unit_of_measurement: °C
        icon: mdi:thermometer


- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Max Forecast Temperature Hour in Next Day"
      unique_id: max_forecast_temperature_hour_in_next_day
      state: >
        {% set man_temp = forecast["weather.home"].forecast[:] | map(attribute='temperature')|max %}
        {% set man_index = (forecast["weather.home"].forecast[:] | map(attribute='temperature')|list).index(man_temp) %}
        {% set coldest_hour = as_timestamp(forecast["weather.home"].forecast[man_index]['datetime']) | timestamp_custom("%H") | int(0) %}
        {{coldest_hour}}
      attributes:
        state_class: measurement
        unit_of_measurement: h
        icon: mdi:thermometer

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Average Forecast Temperature in Next Day"
      unique_id: average_forecast_tmperature_in_next_day
      state: >
        {{ forecast["weather.home"].forecast[:] | map(attribute='temperature')|average|round(1) }}
      attributes:
        state_class: measurement
        device_class: temperature
        unit_of_measurement: °C
        icon: mdi:thermometer

He’s my original code above, now changed to use the new forecast service calls:-

alias: Todays Temperature
description: ""
trigger:
  - platform: time
    at: "07:30:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - service: weather.get_forecasts
    target:
      entity_id: weather.forecast_home
    data:
      type: hourly
    response_variable: forecast_data
  - service: notify.mobile_app__PUT_YOUR_PHONE_HERE
    data:
      title: Daily Forecast
      message: >-
        Good Morning! The temperature outside is currently {{
        forecast_data['weather.forecast_home'].forecast[0].temperature }}
        degrees,  {%set highof = forecast_data['weather.forecast_home'].forecast
        |       selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | max %} {%
        if highof is defined and highof %} with a high of {{highof}} degrees
        {%endif%} {%set lowof = forecast_data['weather.forecast_home'].forecast
        |    selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | min %} {%
        if lowof is defined and lowof %} and a low of {{lowof}}{%endif%}. The
        weather is currently
        {{forecast_data['weather.forecast_home'].forecast[0].condition}}{% set
        currently_raining =
        forecast_data['weather.forecast_home'].forecast[0].condition == 'rainy'
        %}{%set check_for_rain = forecast_data['weather.forecast_home'].forecast
        | selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) |
        selectattr('condition','equalto','rainy') | list%}{%if not
        currently_raining%}{% if check_for_rain is defined and check_for_rain %}
        with rain forecast. {% else %}, with no rain forecast. {%endif%}
        {%else%}. {%endif%} The house is currently
        {{state_attr('climate.main_house','current_temperature')}} degrees.
mode: single