Maximum temperature of Met.no

I would like to create my own sensor that retrieves the maximum temperature from met.no. My weather entity is called weather.home.

Are there any code examples, etc. for this?

Thank you very much!

Yes there’s plenty of examples, have you tried searching?

Yes, I’ve seen that too:

These codes always just don’t work; there are always errors.

It would be very helpful if you showed your code and where you are stuck (what errors?).

If I try to take this code here:

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

which has also been marked as a solution, I always get this error:

No aggregated item, sequence was empty.

Read the release notes, forecast in the weather sensor has been deprecated, multiple posts explain how to create a workaround template sensor

The code below is for finding max wind speed the next days. You can use it as an example of how to find max temperature.

template:
    - trigger:
        - platform: state
          entity_id: weather.orstad_utsyn
      action:
        - service: weather.get_forecasts
          data:
            type: daily
          target:
            entity_id: weather.orstad_utsyn
          response_variable: wd
      sensor:
        - unique_id: vindstyrke_i_morgen
          name: "Vindstyrke i morgen"
          unit_of_measurement: "m/s"
          state: "{{ [state_attr('weather.orstad_utsyn', 'wind_gust_speed')|float(0), (wd['weather.orstad_utsyn'].forecast | rejectattr('wind_gust_speed', 'undefined') | map(attribute='wind_gust_speed')|list|max)]|max|round(1) }}"

The following example reports the maximum temperature in an hourly weather forecast. If you want the maximum temperature for a daily forecast, replace the value hourly with daily.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: event
        event_type: event_template_reloaded
    action:
      - variables:
          w: weather.home
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: '{{ w }}'
        response_variable: r
    sensor:
      - name: 'Temperature Max'
        unique_id: 'temperature_max'
        unit_of_measurement: '°C'
        device_class: temperature
        state: >
          {{ r[w].forecast | map(attribute='temperature')
            | reject('string') | max }}
4 Likes

UndefinedError: 'r' is undefined

This error always appears for me.

You made a mistake somewhere because the example I posted works without any errors for me.

Compare your version to my example. Start by confirming this line:

response_variable: r

:tada:

Thank you!

I don’t know what I did wrong earlier…
Now it works!
Thank you!

1 Like

this works perfectly for me. But i would like to get the max temperature of the current day. In that case i would use type = daily. But how i can write the first and not the max temperature in temperature_max?

Edit: I found the answer in a post from you here: How to migrate from weather integration approach to new weather.get_forecast service? - #13 by 123
Thank you anyway

1 Like

For anyone interested, here’s a code that works to retrieve the max temperature of the current day:
(replace weather.home by the id of your weather integration)

template:
  - trigger:
      - platform: state
        entity_id: weather.home
    action:
      - variables:
          w: weather.home
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: '{{ w }}'
        response_variable: r
    sensor:
      - name: 'Temperature Max'
        unique_id: 'temperature_max'
        unit_of_measurement: '°C'
        device_class: temperature
        state: "{{ r[w].forecast[0].temperature }}"
5 Likes

Hey, I am trying to tweak this solution to my need.

I basically would need to know the lowest temperature in the next twelve hours. So when I come home at say 6pm and leave the next day a 6am again and the temperature drops below zero I get a warning to cover my windshield.

I can’t figure out how to limit the forecast temperatures to twelve. A bonus would be to know when the lowest temperature would be forecasted.

Maybe someone can give me hint.


state: >
  {{ r[w]['forecast'][:12]
    | map(attribute='temperature')
    | reject('string') | min }}
1 Like

Thanks. Does the [:12] mean that only the first 12 temperature entries are being watched at? My understanding would be that [0] means only the first entry is being looked at and by putting a “:” before the number you are then looking at the entries specified by the number afterwards? Just trying to understand an learn :slight_smile:

I have “outsourced” my templates in the config.yaml to templates.yaml and wrote the code in the templates.yaml, however I get an error and the sensor remains unknown. Which confuses me as this is the new service.

UPDATE: I have used the wrong entity for the variable w. It is now working, but I still would like to understand why this error pops up.

This is a good tutorial on string slicing.

1 Like

Is this not working anymore or am I doing something wrong? I put the code into my configuration.yml, rebootet HA, but in the Dev Tools it still shows the value “unknown” for sensor.temperature_max.
The met.no integration works and shows the current and min/max temperatures, but I need to access the daily max in a condition for an automation.

I just tested the example and it still works.

Ensure that the value of variable w contains the entity_id of your met.no weather entity.

In my case, it’s weather.forecast_home.

      - variables:
          w: weather.forecast_home

Sorry can i ask you a question about this.

My implementation

is basically the same but the “daily” forecast for today (i also pull from *.forecast[0]) seems to be very inconsistent and very low in the morning and very high at noon, for Example:

  • get_forecast at 1am: temperature = 17°C
  • get_forecast at 5pm: temperature = 23°C

I would have expected the daily forecast to give me (nearly) the same values throughout the whole day but this seems to change drastically

I always try to gather todays max temperature at 1am in the morning to de/activate all automations related to todays temperature

So my current guess is, that this is not the max temperature of the day, but some sort of mean temperature?

What i will check next

  • Get an hourly forecast
  • Extract the max temperature of the coming 24 values/hours

Taken from the examples above (AND NOT YET TESTED) i think about something like this:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: event
        event_type: event_template_reloaded
    action:
      - variables:
          w: weather.home
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: '{{ w }}'
        response_variable: r
    sensor:
      - name: 'Temperature Max'
        unique_id: 'temperature_max'
        unit_of_measurement: '°C'
        device_class: temperature
        state: >
          {{ r[w].forecast[:24] | map(attribute='temperature')
            | reject('string') | max }}

My Question

So my basic question is, what exactly did i not understand about the daily forecast of met.no. I am guessing that i did not understand what daily forecast[0] really is

Thank you

EDIT:

From what i saw here : Definitive guide to Weather integrations :sun_behind_rain_cloud: - Community Guides - Home Assistant Community
The met.no service anyways returns only 24 values for a whole day

So i can use the unchanged example (without [:24]) and will get my expected result.

I think the bottomline is:

Use the HOURLY weather report to get the DAILY max-temp as posted above
This was what confused me in the first place i think

See the quote above? The HOURLY weather forecast is used, but the temperature_max returned is DAILY

Am i right?