Averaging hourly weather forecast

I wish to average 24 hourly forecasts to get an average daily temperature, which I then use to determine whether to heat or cool the house. I currently have the following:

- platform: template
  sensors:
    average_forecast_temperature:
      friendly_name: "Day's Forecast Average"
      value_template: "{{ average(state_attr('weather.openweathermap', 'forecast')[0]['temperature'],state_attr('weather.openweathermap', 'forecast')[1]['temperature'],state_attr('weather.openweathermap', 'forecast')[2]['temperature'],state_attr('weather.openweathermap', 'forecast')[3]['temperature'],state_attr('weather.openweathermap', 'forecast')[4]['temperature'],state_attr('weather.openweathermap', 'forecast')[5]['temperature'],state_attr('weather.openweathermap', 'forecast')[6]['temperature'],state_attr('weather.openweathermap', 'forecast')[7]['temperature'],state_attr('weather.openweathermap', 'forecast')[8]['temperature'],state_attr('weather.openweathermap', 'forecast')[9]['temperature'],state_attr('weather.openweathermap', 'forecast')[10]['temperature'],state_attr('weather.openweathermap', 'forecast')[11]['temperature'],state_attr('weather.openweathermap', 'forecast')[12]['temperature'],state_attr('weather.openweathermap', 'forecast')[13]['temperature'],state_attr('weather.openweathermap', 'forecast')[14]['temperature'],state_attr('weather.openweathermap', 'forecast')[15]['temperature'],state_attr('weather.openweathermap', 'forecast')[16]['temperature'],state_attr('weather.openweathermap', 'forecast')[17]['temperature'],state_attr('weather.openweathermap', 'forecast')[18]['temperature'],state_attr('weather.openweathermap', 'forecast')[19]['temperature'],state_attr('weather.openweathermap', 'forecast')[20]['temperature'],state_attr('weather.openweathermap', 'forecast')[21]['temperature'],state_attr('weather.openweathermap', 'forecast')[22]['temperature'],state_attr('weather.openweathermap', 'forecast')[23]['temperature'])|round(1)}}"
      unit_of_measurement: °F
      device_class: temperature

This works perfectly, but is extremely verbose. Is there a more effecient way to express the template? Thank you.

value_template: >-
  {{ state_attr('weather.openweathermap', 'forecast')
      | map(attribute='temperature') | average | round(1) }}

EDIT: Missed the round all the way at the end

Thank you, but I need to limit it to indexes 0-23, as there are more than that in the array. Something akin to this is what I’m looking for (but this is a syntax error):

{{ state_attr('weather.openweathermap', 'forecast')[0..23]
      | map(attribute='temperature') | average | round(1) }}

Oh I didn’t realize that wasn’t the whole list. Close but you use a : to specify a subrange instead of ... Also the end is exclusive and if you want everything up to that end you can omit the 0. So this should do it I believe:

{{ state_attr('weather.openweathermap', 'forecast')[:24]
      | map(attribute='temperature') | average | round(1) }}
2 Likes

That gives the same result as my verbose expression, and is so much more concise! Thank you.

3 Likes

This worked great for me … until the weather forecast service changed around 2023.10

When I throw this into Template tool it reports "TypeError: ‘NoneType’ object is not subscriptable"

{{ state_attr('weather.my_place', 'forecast')[:8] | map(attribute='temperature') | average | round(1) }}

Has anybody found a solution using the new weather.get_forecast service?

Thank you