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 }}
3 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 }}"
2 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