Weather forecast template sensor

Hi everybody, as I’ve only copied the codes, I’m too stupid to adjust it, as the weather forecast has changed with 2024.3.

I used this sensor for the average temperature for the next 24 hours. If it updates once before sunset, it would be a dream:

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

And second I use this sensor to decide, whether my shutters should go in “summer mode” so they are closed aligned to the sun. If this updates at sunrise, would be fantastic:

{{ (state_attr('weather.openweathermap', 'forecast')|selectattr('datetime', 'lt', (now().replace(hour=23,minute=59)).isoformat()))| map(attribute='temperature') | list | max }}

Thank you very much for your help

  - trigger:
      - platform: sun
        event: sunrise
        offset: "-00:15:00"
      - platform: sun
        event: sunset
        offset: "-00:15:00"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    sensor:
      - name: Forecast Hourly
        unique_id: weather_forecast_hourly
        state: "{{ states('weather.forecast_home') }}"
        attributes:
          forecast: "{{ hourly['weather.forecast_home'].forecast }}"
    
  - sensor:
      - name: "High temperature today"
        unique_id: High_temperature_today_1
        device_class: temperature
        state: "{{ (state_attr('sensor.forecast_hourly', 'forecast')|selectattr('datetime', 'lt', (now().replace(hour=23,minute=59)).isoformat()))| map(attribute='temperature') | list | max }}"
        unit_of_measurement: °C
  - sensor:
      - name: "Average temperature today"
        unique_id: Average_temperature_today_1
        device_class: temperature
        state: "{{ (state_attr('sensor.forecast_hourly', 'forecast'))[:24] | map(attribute='temperature') | average | round(1) }}"
        unit_of_measurement: °C
1 Like

Sorry I have a really noob question. This looks like the kind of solution that I need, but where should I be adding this code? Is it in my templates.yaml file?

it needs to be loaded by configuration.yaml. you can put it in a separate templates.yaml file, but then you need to !include it.

here’s info about splitting up the configuration file:

if you’re a total noob just started, i’d start off by putting it in configuration.yaml. remember that it needs to be in a templates block.

look here for more info:

Thanks for providing these links! I’ll read them this weekend (hopefully)! I had followed a YouTube tutorial for how to !include the templates.yaml file in configuration.yaml, but the template I made with that tutorial just flat out didn’t work.

I’ve used the code mcweis used above and it worked!

EDIT: I say hopefully because I’m trying to set up, learn, and use HA with an 8mo baby who is up and down at night and very active in the day… time is not abundant lately!

EDIT: Figured it out:

 | selectattr('datetime', 'lt', (now().replace(hour=23,minute=59).timestamp()|timestamp_utc))

Any tip for dealing with a weather service that returns datetime in UTC? I’m struggling to figure out how to take (now().replace(hour=23,minute=59)).isoformat() and then turn that into UTC.

A forecast entry from pirateweather looks like:

{
   "datetime":"2024-06-23T02:00:00+00:00",
   "condition":"partlycloudy",
   "wind_bearing":230.0,
   "precipitation_probability":0.0,
   "cloud_coverage":43.0,
   "uv_index":1.79,
   "temperature":73,
   "apparent_temperature":74,
   "dew_point":56,
   "pressure":29.71,
   "wind_gust_speed":11.86,
   "wind_speed":6.26,
   "precipitation":0.0,
   "humidity":55
}