Hourly Weather-Forecast between 10am and 6 pm

Hello everyone,

I would like to create a weather forecast template that shows me the hourly weather forecasts for the current (or next day) for the times between 10 am and 6 pm (specifically the attributes condition, cloud_coverage and temperature).

So, for example, if it is 7 a.m., I would need the values for the current day and, for example, at 7 p.m. those for the next day. In the course of this time, e.g. at 2 pm only the remaining forecasts until 6 pm of the current day.

As far as I could find out, only a relative daily or hourly forecast is possible.

Can any of you tell me if this is possible?

Many thanks in advance!
Braak

something like this, you need to test and tweak and possibly there is a more esthetic way to write the conditions :slight_smile:

template:
         
  - trigger:
      - platform: time_pattern
        minutes: /10
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: forecast
    sensor:
      - name: Weather Forecast Something
        unique_id: weather_forecast_something
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: > 
            {% set ns = namespace(fc =[]) %}
            {% for item in forecast['weather.home'].forecast %}
            {% if ( 10 <= item.datetime[11:13]|int <= 18 ) and item.datetime|as_datetime < today_at('18:01')  %}
            {% set  ns.fc = ns.fc + [item] %}
            {% elif ( 10 <= item.datetime[11:13]|int <= 18 ) and now() > today_at('18:01') %} 
            {% set  ns.fc = ns.fc + [item] %}
            {% endif %}
            {% endfor %}
            {{ ns.fc }}

just updated it a bit

Wow, this looks promising!!

:star_struck:

I like the approach: at least for my templating skills it looks very aesthetic - thank you!! :heart:

I’ll get back as soon as kids are in bed and I can start tweaking… :wink:

It only took me three months to try out your suggestion: and yippee - thank you! It works! :))

:heart:

Thank you @vingerha !!

I’ve now also tried to create another template sensor based on it with only the cloud cover from the previous sensor you created:

The template looks like this:

      value_template: >
        {% for hour in state_attr( 'sensor.weather_forecast_pv', 'forecast') %} 
        {{ hour.cloud_coverage }} 
        {% endfor %}

And also delivers, I think correctly, e.g. this result:

        0.0 
         
        6.5
         
        22.3 
         
        10.7
         
        0.0 
         
        0.0 

But if I make a sensor from it as follows…

- platform: template 
  sensors: 
    daily_cloud_coverage:
      value_template: >
        {% for hour in state_attr( 'sensor.weather_forecast_pv', 'forecast') %} 
        {{ hour.cloud_coverage }} 
        {% endfor %}
      friendly_name: "Tageswolkenbedeckung"
      unique_id: daily_cloud_coverage 
      unit_of_measurement: "%"

… I get no values displayed:

Since this is, as already mentioned, my first templating attempt, I hope you can help me to get the cloud-coverage-sensors values…

Thank you again!

I have no clue how your data looks like but assuming you have multiple ‘hour’ sections under ‘forecast’ then the for-loop will give you value one, then loop back and give you the next value then loop etc. If the data is adhering forecast>hour>cloud_coverage then at least you would see the last value in the loop, since it does not do that three is something fishy…post the data too (not all if it is a lot)

If this is in your config this way, that is an error. unit of measurement should be on a a separate line. If this is just a typo here, and it is on a separate line, then the state cannot be several numbers after one another (because of the loop). Then the output is expected to be a single number.

@Edwin_D
" If this is just a typo here, […]"

Yes, it was a typo in the post (corrected it already). Sorry for that!
:see_no_evil:

@vingerha
" […] post the data too (not all if it is a lot)."

I am not sure if I understand what you ment, but the value_templates data actually ist already posted above:

   0.0 
    
   6.5
    
   22.3 
    
   10.7
    
   0.0 
    
   0.0

Or am I misunderstanding what you ment?

As I said, the way the template is now the sensor can hold exactly one number in its state, not six numbers in one string like your template outputs. If you want the full text with the six numbers, then remove the unit_of_measurement line. Without it the text is allowed.

If you use an attribute to hold the information then you can have multiple numerical values in a data structure like the original example. But then you need to change the template to something more akin to the original template.

All right, I think I’m getting the hang of it… Thanks @Edwin_D ! And if I understand it correctly, I have the impression that no further template sensor is actually necessary, because the first one already contains all attributes for the given time period and therefore also cloud_coverage.

But how can I read the values for cloud coverage from vinghera’s original template sensor for an automation, for example?

You can add separate attributes like in this thread (you need to replace some of the names from there with yours. Then you have separate attributes to test for in automations:

Okay, thanks! :heart:

But in my case it‘s not a weather.forecast_weather_pv entity but a template sensor sensor.forecast_weather_pv : isn’t that a problem?

@vingerha

Your sensor now worked perfectly - or nearly. Today I found out, that the first two “forecasted” hours from now (18:39) on, are not 10:00 and 11:00 in the next (2025-08-09 morning, but 17:00 and 18:00 In the (2025-08-08, so todays) past:

forecast:
  - condition: sunny
    datetime: "2025-08-08T17:00:00+00:00"
    wind_bearing: 241.5
    cloud_coverage: 0
    uv_index: 0.4
    temperature: 25.3
    wind_speed: 5.8
    precipitation: 0
    humidity: 64
  - condition: sunny
    datetime: "2025-08-08T18:00:00+00:00"
    wind_bearing: 216.8
    cloud_coverage: 0.8
    uv_index: 0.1
    temperature: 22.8
    wind_speed: 6.5
    precipitation: 0
    humidity: 72
  - condition: sunny
    datetime: "2025-08-09T10:00:00+00:00"
    wind_bearing: 237.2
    cloud_coverage: 0
    uv_index: 6.9
    temperature: 26.7
    wind_speed: 7.9
    precipitation: 0
    humidity: 60
  - condition: sunny
    datetime: "2025-08-09T11:00:00+00:00"
    wind_bearing: 236.4
    cloud_coverage: 0
    uv_index: 7.6
    temperature: 27.5
    wind_speed: 10.4
    precipitation: 0
    humidity: 56
  - condition: sunny
    datetime: "2025-08-09T12:00:00+00:00"
    wind_bearing: 236.5
    cloud_coverage: 0
    uv_index: 7.4
    temperature: 28
    wind_speed: 12.6
    precipitation: 0
    humidity: 51
  - condition: sunny
    datetime: "2025-08-09T13:00:00+00:00"
    wind_bearing: 238.7
    cloud_coverage: 0
    uv_index: 6.2
    temperature: 28.3
    wind_speed: 13
    precipitation: 0
    humidity: 47
  - condition: sunny
    datetime: "2025-08-09T14:00:00+00:00"
    wind_bearing: 242.4
    cloud_coverage: 0
    uv_index: 4.5
    temperature: 28.3
    wind_speed: 12.2
    precipitation: 0
    humidity: 47
  - condition: sunny
    datetime: "2025-08-09T15:00:00+00:00"
    wind_bearing: 245.9
    cloud_coverage: 1.6
    uv_index: 2.7
    temperature: 28
    wind_speed: 10.8
    precipitation: 0
    humidity: 49
  - condition: sunny
    datetime: "2025-08-09T16:00:00+00:00"
    wind_bearing: 245.5
    cloud_coverage: 3.9
    uv_index: 1.3
    temperature: 27.5
    wind_speed: 8.3
    precipitation: 0
    humidity: 55
friendly_name: Weather Forecast PV

The data is as it comes from the source…nothing that I can change, do note (if not already done so) that these datetime are in GMT

Great! No, I hadn’t thought of that – thanks for pointing it out!

:heart_eyes:

So what I saw yesterday at 6pm here was that here in Germany (GMT+2), it’s already 6 p.m., while according to GMT time, it’s only 4 p.m. Sure, then it shows me two hours (the GMT zone) for the current day according to the current calculation (10-6 p.m.), even though it’s already after 6 p.m. for me.

But finally that just means I would have to change the formula to subtract 2 hours. So if I want to know the forecast for the current day from 10 a.m. to 6 p.m. (GMT-2) and after that the forecast for the next day (same time), and I’m calculating in GMT, I just set the if / elif threshold value to 16:01 (GMT) instead of 18:01, like this, right?

          forecast: > 
            {% set ns = namespace(fc =[]) %}
            {% for item in forecast_pv['weather.forecast_home'].forecast %}
            {% if ( 10 <= item.datetime[11:13]|int <= 18 ) and item.datetime|as_datetime < today_at('16:01')  %}
            {% set  ns.fc = ns.fc + [item] %}
            {% elif ( 10 <= item.datetime[11:13]|int <= 18 ) and now() > today_at('16:01') %} 
            {% set  ns.fc = ns.fc + [item] %}
            {% endif %}
            {% endfor %}
            {{ ns.fc }}

The timestamps you’re seeing are in UTC, not GMT. Most of the time they’re interchangeable, but GMT fluctuates with winter/summer time changes, while UTC does not.
If you set your formula to deduct 2 hours, it’ll work for the next couple of months but then be off by an hour when you switch back to winter time.

I really suck at templating, but have a read here. There should be the info you need to handle UTC > local time conversions properly.

@vingerha
To learn something for templating: would you mind explaining what this part of the template does?

item.datetime[11:13]|int

What do the 11 and 13 stand for ?
:grimacing:

It allows to extract a substring. in this case the datetime attribute is actually a string so if you want to get text out of that you can use this.
Example…type this in Developer tools > Templates

{% set x = 'abcdefghijklm' %}
{{ x[2:4] }}     characters at indexes 2 and 3 → `'cd'`
{{ x[-1] }}      last character → `'m'`
{{ x[0:-1] }}    all but the last character → `'abcdefghijkl'`

Wow, perfect explanation - thank you again :heart_hands:

While we’re at it: over the years I’ve always found many helpful people - like you - who have always explained many aspects of templating to me.

However, I would like to learn it properly from scratch, but I can’t find a good source for a Jinja2 introduction. Of course, I don’t mean that there are no really very good Jinja2 explanations. But such help pages live - especially for beginners - from the examples that illustrate their theoretical explanations. And unfortunately I can’t find such a comprehensive help or explanation page that uses concrete HomeAssistant examples.

But maybe you or someone else knows a good source for this or has a recommendation for how a beginner can learn the Jinja2 templating syntax from scratch with concrete HA examples?

Thanks in advance!
:heart: