Weather.get_forecasts

Hi all.
Hope you can help me, cause I’m going crazy over this…
I have this template sensor to show hourly weather forecast.:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    sensor:
      - name: Vaermelding_timebasert_info
        state: "{{ states('weather.forecast_home') }}"
        attributes:
          temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}"
          dew_point: "{{ state_attr('weather.forecast_home', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.forecast_home', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.forecast_home', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.forecast_home', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.forecast_home', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.forecast_home', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('weather.forecast_home', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.forecast_home', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('weather.forecast_home', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('weather.forecast_home', 'precipitation_unit') }}"
          forecast: "{{ hourly['weather.forecast_home'].forecast }}"

and it works great, but i shows the weather at the wrong time by two hours…
it say it will be sunny at 0500, but regarding to the app (met,no) it will be sunny at 0700 (and yes the app is the same as met.no i use for my weather in HA)

So it’s two hours “early”
i believe it can have something to do with the timestamp or something…
I live i Norway…

well i dont know why this is doing this wrong…

Please help me with this, before i explode in frustration :stuck_out_tongue:
I have search the web…
and i even asked chatgpt for help, but it just f**ked the code up even more :wink:

Best regards

Anyone who know and can help?

This template is not doing anything with datetime. What makes you think it is two hours off?

There’s a good chance it is not, but you are just interpreting it wrong. Datetime is usually stored in UTC - so that time is not depending on where you live. If it is summertime in Norway, there’s a good chance Norwegian time is +2 hours after UTC. If you format the UTC datetime as local time it will display correctly.

But since you do not show where you get the datetime from, what it is and what you do with it, it is hard to help you further. Anyway, there’s nothing wrong with the template, and ChatGPT is not going to help you in any meaningful way.

There’s a lot here though on conversion, this is a good start:

1 Like

You need to get the information you want from the forecast attribute by filtering it by the timestamp.

Copy this into Developer Tools / Template:

{{ state_attr('sensor.vaermelding_timebasert_info', 'forecast') }}

and paste the result here.

[{‘condition’: ‘cloudy’, ‘precipitation_probability’: 15.0, ‘datetime’: ‘2024-06-09T17:00:00+00:00’, ‘wind_bearing’: 309.9, ‘cloud_coverage’: 87.7, ‘temperature’: 15.3, ‘wind_gust_speed’: 4.31, ‘wind_speed’: 0.81, ‘precipitation’: 0.0, ‘humidity’: 58}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 21.0, ‘datetime’: ‘2024-06-09T18:00:00+00:00’, ‘wind_bearing’: 251.5, ‘cloud_coverage’: 36.2, ‘temperature’: 15.1, ‘wind_gust_speed’: 7.69, ‘wind_speed’: 3.69, ‘precipitation’: 0.0, ‘humidity’: 56}, {‘condition’: ‘rainy’, ‘precipitation_probability’: 46.5, ‘datetime’: ‘2024-06-09T19:00:00+00:00’, ‘wind_bearing’: 227.6, ‘cloud_coverage’: 81.6, ‘temperature’: 14.5, ‘wind_gust_speed’: 7.0, ‘wind_speed’: 3.31, ‘precipitation’: 0.3, ‘humidity’: 58}, {‘condition’: ‘rainy’, ‘precipitation_probability’: 40.5, ‘datetime’: ‘2024-06-09T20:00:00+00:00’, ‘wind_bearing’: 235.6, ‘cloud_coverage’: 99.8, ‘temperature’: 12.5, ‘wind_gust_speed’: 12.19, ‘wind_speed’: 6.0, ‘precipitation’: 0.3, ‘humidity’: 72}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 17.8, ‘datetime’: ‘2024-06-09T21:00:00+00:00’, ‘wind_bearing’: 241.5, ‘cloud_coverage’: 99.2, ‘temperature’: 11.4, ‘wind_gust_speed’: 10.69, ‘wind_speed’: 4.81, ‘precipitation’: 0.0, ‘humidity’: 82}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 7.4, ‘datetime’: ‘2024-06-09T22:00:00+00:00’, ‘wind_bearing’: 245.3, ‘cloud_coverage’: 84.6, ‘temperature’: 10.7, ‘wind_gust_speed’: 8.69, ‘wind_speed’: 4.0, ‘precipitation’: 0.0, ‘humidity’: 84}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 4.4, ‘datetime’: ‘2024-06-09T23:00:00+00:00’, ‘wind_bearing’: 257.6, ‘cloud_coverage’: 42.8, ‘temperature’: 10.0, ‘wind_gust_speed’: 7.31, ‘wind_speed’: 3.39, ‘precipitation’: 0.0, ‘humidity’: 87}, {‘condition’: ‘clear-night’, ‘precipitation_probability’: 3.6, ‘datetime’: ‘2024-06-10T00:00:00+00:00’, ‘wind_bearing’: 278.7, ‘cloud_coverage’: 0.7, ‘temperature’: 9.5, ‘wind_gust_speed’: 6.39, ‘wind_speed’: 3.5, ‘precipitation’: 0.0, ‘humidity’: 89}, {‘condition’: ‘clear-night’, ‘precipitation_probability’: 2.4, ‘datetime’: ‘2024-06-10T01:00:00+00:00’, ‘wind_bearing’: 297.9, ‘cloud_coverage’: 1.5, ‘temperature’: 8.8, ‘wind_gust_speed’: 6.5, ‘wind_speed’: 3.19, ‘precipitation’: 0.0, ‘humidity’: 88}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T02:00:00+00:00’, ‘wind_bearing’: 305.3, ‘cloud_coverage’: 1.1, ‘temperature’: 8.1, ‘wind_gust_speed’: 5.89, ‘wind_speed’: 3.11, ‘precipitation’: 0.0, ‘humidity’: 87}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T03:00:00+00:00’, ‘wind_bearing’: 307.2, ‘cloud_coverage’: 1.2, ‘temperature’: 7.8, ‘wind_gust_speed’: 5.61, ‘wind_speed’: 3.11, ‘precipitation’: 0.0, ‘humidity’: 84}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T04:00:00+00:00’, ‘wind_bearing’: 308.3, ‘cloud_coverage’: 0.7, ‘temperature’: 8.5, ‘wind_gust_speed’: 5.81, ‘wind_speed’: 3.11, ‘precipitation’: 0.0, ‘humidity’: 79}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T05:00:00+00:00’, ‘wind_bearing’: 300.6, ‘cloud_coverage’: 30.8, ‘temperature’: 10.0, ‘wind_gust_speed’: 6.11, ‘wind_speed’: 3.19, ‘precipitation’: 0.0, ‘humidity’: 73}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T06:00:00+00:00’, ‘wind_bearing’: 298.6, ‘cloud_coverage’: 83.6, ‘temperature’: 11.4, ‘wind_gust_speed’: 6.39, ‘wind_speed’: 3.31, ‘precipitation’: 0.0, ‘humidity’: 68}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T07:00:00+00:00’, ‘wind_bearing’: 293.2, ‘cloud_coverage’: 86.4, ‘temperature’: 12.8, ‘wind_gust_speed’: 6.89, ‘wind_speed’: 3.5, ‘precipitation’: 0.0, ‘humidity’: 65}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T08:00:00+00:00’, ‘wind_bearing’: 267.2, ‘cloud_coverage’: 98.6, ‘temperature’: 13.9, ‘wind_gust_speed’: 7.11, ‘wind_speed’: 3.39, ‘precipitation’: 0.0, ‘humidity’: 66}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 0.4, ‘datetime’: ‘2024-06-10T09:00:00+00:00’, ‘wind_bearing’: 261.6, ‘cloud_coverage’: 99.5, ‘temperature’: 15.5, ‘wind_gust_speed’: 7.11, ‘wind_speed’: 3.61, ‘precipitation’: 0.0, ‘humidity’: 64}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 3.2, ‘datetime’: ‘2024-06-10T10:00:00+00:00’, ‘wind_bearing’: 259.3, ‘cloud_coverage’: 92.3, ‘temperature’: 17.3, ‘wind_gust_speed’: 7.61, ‘wind_speed’: 3.39, ‘precipitation’: 0.0, ‘humidity’: 60}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 1.6, ‘datetime’: ‘2024-06-10T11:00:00+00:00’, ‘wind_bearing’: 239.6, ‘cloud_coverage’: 68.1, ‘temperature’: 18.4, ‘wind_gust_speed’: 7.0, ‘wind_speed’: 2.39, ‘precipitation’: 0.0, ‘humidity’: 54}, {‘condition’: ‘cloudy’, ‘precipitation_probability’: 0.0, ‘datetime’: ‘2024-06-10T12:00:00+00:00’, ‘wind_bearing’: 52.7, ‘cloud_coverage’: 86.7, ‘temperature’: 18.0, ‘wind_gust_speed’: 8.61, ‘wind_speed’: 4.0, ‘precipitation’: 0.0, ‘humidity’: 60}, {‘condition’: ‘partlycloudy’, ‘precipitation_probability’: 1.3, ‘datetime’: ‘2024-06-10T13:00:00+00:00’, ‘wind_bearing’: 348.7, ‘cloud_coverage’: 50.0, ‘temperature’: 19.1, ‘wind_gust_speed’: 8.11, ‘wind_speed’: 2.19, ‘precipitation’: 0.0, ‘humidity’: 45}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 12.7, ‘datetime’: ‘2024-06-10T14:00:00+00:00’, ‘wind_bearing’: 347.6, ‘cloud_coverage’: 5.9, ‘temperature’: 19.9, ‘wind_gust_speed’: 5.89, ‘wind_speed’: 1.81, ‘precipitation’: 0.0, ‘humidity’: 44}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 13.0, ‘datetime’: ‘2024-06-10T15:00:00+00:00’, ‘wind_bearing’: 2.8, ‘cloud_coverage’: 9.1, ‘temperature’: 20.0, ‘wind_gust_speed’: 6.5, ‘wind_speed’: 2.61, ‘precipitation’: 0.0, ‘humidity’: 42}, {‘condition’: ‘sunny’, ‘precipitation_probability’: 7.6, ‘datetime’: ‘2024-06-10T16:00:00+00:00’, ‘wind_bearing’: 40.8, ‘cloud_coverage’: 2.9, ‘temperature’: 19.9, ‘wind_gust_speed’: 6.81, ‘wind_speed’: 2.5, ‘precipitation’: 0.0, ‘humidity’: 42}]

Well look at these pictures and you see for your self that it is two hours off…



No, it is not, it says +00:00 at the end, meaning UTC, if your time zone is +02:00, then represented in your local time you need to add two hours. When displaying, it needs to be displayed in local time.

19:00:00+00:00 (UTC) is the same as 21:00:00+02:00 (CET)

But how do i fix this then?
What do i have to do, to make this right then??

First question is maybe, if you are using a weather card, why do you need to extract the contents in attributes of your own? The card should take care of this.

If you need to format it yourself, you need to use the proper locale to display it, The rhread I linked to before has many functions to format a time in locale readable format.

Well i would like have an automation that notify me when it is supposed to start raining in an hour…

If that is what you want, you’ll need to do time calculations. If you use now(), for the that is also in UTC, then you do not need to worry. It is just when displaying that you need to think about it. This is exactly why UTC is used, so you do. ot need to worry about local time complexity.

There’s also this to help you to display a lot of time things in local time once you get to the point where you want to display time information:

You can use the list index number to get the second item, there is no need to use or alter the datetime string.

{{ state_attr('sensor.vaermelding_timebasert_info', 'forecast')[1]['condition'] == 'rainy' }}

Thank you!

You are official my hero :slight_smile:

An alternative to get the condition from the first forecast that is later than the current time rather than relying on its list position:

{{ state_attr('sensor.vaermelding_timebasert_info', 'forecast')
   |selectattr('datetime','>=',utcnow().isoformat())
   |map(attribute='condition')|first }}

Similarly, pulling the chance of rain in three hours’ time:

{{ state_attr('sensor.vaermelding_timebasert_info', 'forecast')
   |selectattr('datetime','>=',(utcnow()+timedelta(hours=3)).isoformat())
   |map(attribute='precipitation_probability')|first }}

Where does “sensor.vaermelding_timebasert_info” come from? I dont have that entity…

it’s the sensor that the op created in his first post. see this part:

that defined the name of that sensor.

2 Likes