Meteorologisk institutt (Met.no) forecast data template

Hi,
want to read out the low temp of next day. Found some template, which fails, cause there is no data array.

{{ state_attr(‘weather.forecast_home_2’, ‘forecast’)[0].temperature }}

I checked the provided docu, but there is only information about installing the integration.

Where can I find a detailed description of this integeration, or how did the correct templete look like?

With best regards

Gerhard

Hi Gerhard, do you have an entity weather.forecast_home_2?
Are you using the Developer tools → template ?

Open your Home Assistant instance and show your template developer tools.

This can be very helpful to troubleshoot/create.

Hi Nick,
yes, the name of my entity is correct.
I used the dev tools (template) to test the template I got from some conversation and adopt it, but doesn’t work.
I see 5 day forecast in my UI, but can’t get this values out via template. The sample uses indices to extract the element 0 of an array, but there is no array, the dev tools told me …
With best regards
Gerhard

This is the temperature from my weather integration for tomorrow:

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

and does work.

But: it doesn’t work just like that since you have to retrieve the weather first.

1 Like

Hi Nick,
ok, I thought it is already done:

I have this element in my UI …
If there is extra work to do, please give my a pointer how and where (I am still a newbee).
Thanks a lot.
With best regards
Gerhard

You will need to use the weather.get_forecasts action.

Refer to the first example in the documentation for the Weather integration.

It shows how to create a Trigger-based Template Sensor that reports today’s hourly temperature and automatically updates every hour.

I have modified the example to meet your requirements (get tomorrow’s daily temperature, updated every hour and whenever Template Entities are reloaded).

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
      - trigger: event
        event_type: event_template_reloaded
    action:
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home_2
        response_variable: daily
    sensor:
      - name: Temperature forecast tomorrow
        unique_id: temperature_forecast_tomorrow
        state: "{{ daily['weather.forecast_home_2'].forecast[1].temperature }}"
        unit_of_measurement: '°C'
        device_class: temperature

If you want to see another example of using weather.get_forecasts


EDIT

Modification. Changed platform to trigger to avoid complaint by Config Helper in VS Code.

Thanks.

But my biggest problem with HA:
How can I find out in which file things have to go?

I put this in configuration.yaml, but it fails.

For a newbee it is not that easy to find out, where to put this sample code … sorry.

With best regards

Gerhard

Is there a related error message in Settings → Logs?

You said you put it in the configuration.yaml file. Does that file already contain a template: key?

Because if you copy-pasted my example into the file, the first line of the example is a template: key so if there’s already a template: key in the file you will have duplicated it (it should never be duplicated).


NOTE

If your configuration.yaml file contains something that looks like this:

template: !include templates.yaml

It means the configuration for Template entities are located in a separate file named templates.yaml.

Examine your configuration.yaml file and let me know if it already contains a template: key and if it has an !include or not.

If it does contain a template: key (but no !include), paste my example directly under it and ensure there’s only one instance of the template: key.

If this is the very first Template entity you have ever created, restart Home Assistant in order to load it. If you already have other Template entities, you can simply use Developer Tools → YAML → Reload Template Entities.

Ok, not that easy. I have to learn lot about this ‘yaml’ …

Ok, to get things structured I created a templates.yaml file.
I included it.
And I learned, that the key ‘template:’ is in configuration.yaml and the content is in templates.yaml. Otherwise I get some message that the line ‘template:’ isn’t allowed.

Ok, now I have the same problem as I get when I copied the code directly into configuration.yaml before which does’n contain a ‘template’ key before:

It looks like, that something was changed in HA and this syntax isn’t valid any longer.

This error was maybe misinterpreted by me, I first thought, that this code is not for configuration.yaml.

With best regards

Gerhard

PS: By the way, is it usual that syntax is broken with updated very often? My working eMail notification service is also in error and the message is more or less the same.

Note that the syntax parsing/error codes in vscode is not maintained by the Home Assistant project, and is occasionally incorrect or out of date.

So when you see errors in vscode take it with a grain of salt. You can try ignoring it and check your configuration with the official config check tool in Developer Tools.

That’s entirely optional. As explained, it can be placed within configuration.yaml as long as it’s done correctly.

It’s different, not the same. As explained by karwosts, that message isn’t produced by Home Assistant. It’s produced by the VS Code editor, specifically by a plug-in used by VS Code called Home Assistant Config Helper.

It’s still valid.

Recently, Home Assistant renamed several keys used in automations (and Trigger-based Template entities) such as platform and service (to trigger and action, respectively). The old names still work but Config Helper identifies them anyways.

To make Config Helper’s message go away, replace the word platform with trigger. Refer to my original example posted above which I have already updated.


NOTE

If you wish, you can remove the second trigger. It’s there merely as a convenience for you.

It’s sole purpose is to ensure that the Trigger-based Template Sensor produces a value the moment you execute Reload Template Entities. You get an initial result immediately

Without that second trigger, the Sensor’s initial value will be unknown and will remain that way until the Time Pattern Trigger fires (on the hour, every hour). You have to wait for the initial result.

I was having some similar issues (also a bit new to HA, YAML, etc.) and finally figured out a workaround to extract full weather data by using Attributes within the Trigger Template Sensor. Maybe this can help you or the next person to stumble upon this thread…

The key here is that the number of characters in an Entity’s State is limited to something like 250, but this is not the case for an Entity’s State Attributes (or at least it’s a much higher limit). Outputting the full forecast data into an Attribute, instead of the State, allows me to keep the templates.yaml file minimal and gives me freedom when creating other Template Sensors, as I can test and see the outputs in the Template section of the DevTools menu without having to create dummy datasets.

Here’s the code I included in my templates.yaml file to get started:

- trigger:
    - trigger: time_pattern
      seconds: "/10"
  action:
    - action: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.forecast_home
      response_variable: hourly
  sensor:
    - name: Weather Forecast Data
      unique_id: weather_forecast_data
      state: "{{ hourly['weather.forecast_home'].forecast[0].datetime }}"
      attributes: 
        fcdata: "{{ hourly['weather.forecast_home'].forecast | to_json }}"

Note that my time pattern is set to every 10 seconds “/10” - this was just to ensure data loaded quickly when testing, in reality it can be hourly or longer.

Once that’s in your templates.yaml file (for formatting, the very first line has no indent in that file), you can open Template tab in DevTools and try out the code below as an example. This may not be the most efficient way to do this, but it’s what I could make work.

{#- PARSE WEATHER FORECAST FOR TEMPS -#}
{#- Declare empty lists to append to -#}
{%- set temps = namespace(forecast=[], today=[], night=[]) -%}
{#- Loop through the 24 hourly forecasts -#}
{%- for item in state_attr('sensor.weather_fc_data', 'fcdata') -%}
{#- Build list of all forecast temps -#}
  {%- set temps.forecast = temps.forecast + [item.temperature] -%}
  {#- Set date_time to today's date and build list of temps on today's date -#}
  {%- set date_time = item.datetime | as_datetime | as_local -%}
  {%- if date_time.date() == now().date() -%}
    {%- set temps.today = temps.today + [item.temperature] -%}
  {%- endif -%}
  {#- Build list of nighttime temps only (from 10pm to 8am) -#}
  {%- if date_time.hour<8 or date_time.hour>=22 -%}
    {%- set temps.night = temps.night + [item.temperature] -%}
  {%- endif -%}
{%- endfor -%}
{#- Process output lists ot get helpful data like highs and lows -#}
{%- set forecast_high, forecast_low = temps.forecast | max, temps.forecast | min -%}
{%- set today_high, today_low = temps.today | max, temps.today | min -%}
{%- set night_low = temps.night | min -%}
Today's High: {{today_high}}
Nightly Low: {{night_low}}

Edit to add: This is for the default Met.No Weather Integration.