Sensor with forecasted temperature in x hours

I am using the met.no integration and would like to create a sensor with the forecasted outside temperature at my location in x hours, e.g. in 3 hours from now. How?

If you want this x hours ahead then you could run a template on it

{{ state_attr('weather.home', 'forecast')[2].temperature }}

Or at a specific time

{{ (state_attr('weather.home', 'forecast')|selectattr('datetime', 'eq', (today_at('5:00').astimezone(utcnow().tzinfo) + timedelta (days=1)).isoformat())|list|first).temperature }}

Thanks, man. Works like a charm!

Here are two ways to achieve your goal of reporting the forecasted temperature in X hours. Simply replace weather.forecast_home_hourly with your entity’s entity_id and replace 3 with whatever number of hours you want. The template will calculate the correct future time.

If the forecast doesn’t contain the data for the desired future time, both of these examples will report unknown. For example, if you set a future time that exceeds the forecast’s available data.

{% set ns = namespace(temp='unknown') %}
{% for fh in state_attr('weather.forecast_home_hourly', 'forecast')
  if (fh.datetime | as_datetime).hour == utcnow().hour + 3 %}
{% set ns.temp = fh.temperature %}
{% endfor %}
{{ ns.temp }}
{% set future_time = ((utcnow() + timedelta(hours=3)).replace(minute=0,second=0,microsecond=0)).isoformat() %}
{{ state_attr('weather.forecast_home_hourly', 'forecast')
  | selectattr('datetime', 'eq', future_time)
  | map(attribute='temperature')
  | first | default('unknown', true) }}

@vingerha
I couldn’t get your second example to work with my hourly forecast. The predicted temperature for 12:00 today is 4.0.

It failed because the template adds a 1-day timedelta to the future time. If I remove it then the template reports the correct temperature for 12:00 today.

Swahlgren reported it “works like a charm” but I don’t see how that’s possible given that the template attempts to retrieve data for tomorrow at 5:00.

Did I misunderstand swahlgren’s requirements? I thought the goal was to get the temperature in X hours.

In addition, if a future time doesn’t exist in the forecasted data, the same error message will occur because first doesn’t gracefully handle an empty list. The default filter is needed to mitigate this possibility.


EDIT

Regarding the following template, I discovered that the zeroth index doesn’t necessarily contain data for the current hour. It all depends on when the met.no integration is scheduled to update its hourly forecast data. Therefore you can’t rely on index 2 to contain the forecast for 3 hours in the future.

{{ state_attr('weather.home', 'forecast')[2].temperature }}
                                          ^
                                          |
    Not guaranteed to represent the desired *hourly* time offset 
1 Like

Works for me, can’t speak for all integrations

For me at 7:50 this morning, using met.no, index 0 contained the hourly forecast for 08:00. That disabused me from the notion that I could rely on the zeroth index to represent the current hour’s forecast.

Yeah… agree that mine is not as precise in trying to get to the closest 3-hours-from-now…at 07:59 it will get 10

My bad. I was too quick to assume it returned the correct temperature. I’ll give your solution a go.

Copy-paste either of my two examples into the Template Editor then replace the entity_id for hourly forecast with your own. It should report the temperature for 3 hours from now. Change the 3 to any other positive value to confirm the template behaves correctly.

Used your first suggestion and now the sensor shows the correct temperature, thanks!

1 Like