Max temperature forecast

Hello,

so far I have written two sensors with the following command, which contain the maximum daily temperature for today and tomorrow:

### Temperatur Forecast
  - platform: template
    sensors:
      temperatur_morgen:
       value_template: "{{ state_attr('weather.home', 'forecast')[1].temperature }}"
      temperatur_heute:
       value_template: "{{ state_attr('weather.home', 'forecast')[0].temperature }}" 

Now it seems that this no longer works. I have already read a lot in the forum and seen many, many scripts, none of which I can apply to my case.

So here is my question:
How can I set the two sensors back to today’s and tomorrow’s maximum temperatures?

If it is relevant, I use the integrations met.no and Deutscher Wetterdienst.

Can anyone help?
Thank you very much!

Dennis

The forecast attribute was deprecated more than 7 months ago. In order to get that information you must use the weather.get_forecasts service if the weather integration you use has not updated to provide the sensors you want.

For Met.no you can use the following:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: result
    sensor:
      - name: Temperatur Morgen
        unique_id: max_temperatur_morgen
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[1]['temperature'] }}
      - name: Temperatur Heute
        unique_id: max_temperatur_heute
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[0]['temperature'] }}

Note that trigger-based template sensors need to be configured under the template integration, not under the sensor integration like your previous sensors. If you simply copy paste the above configuration into sensors.yaml, it will not work.

3 Likes

Hey @Didgeridrew

Thank you very much for your help.
The script already looks great.

But I still have a small problem integrating the script into my conf. I’m not sure how to position it. If I simply paste it in, my config reports an error.

My template path currently looks like this:

template:
  sensor:
    - name: Heizung Esszimmer Temperatursensor
      state: "{{ state_attr('climate.smart_thermostat_2109301685957490857248e1e978bea9','current_temperature')}}"
      unit_of_measurement: "°C"
      device_class: temperature 
      state_class: measurement

(...)

I still don’t understand how to sort the -trigger.

Could you give me another tip here?

All your state-based sensors can be a single “item” in the list, the trigger-based sensor will be another “item”. You use hyphens to indicate the start of the configuration for each item in the list of template entities:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: result
    sensor:
      - name: Temperatur Morgen
        unique_id: max_temperatur_morgen
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[1]['temperature'] }}
      - name: Temperatur Heute
        unique_id: max_temperatur_heute
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[0]['temperature'] }}

  - sensor:
      - name: Heizung Esszimmer Temperatursensor
        state: "{{state_attr('climate.smart_thermostat_2109301685957490857248e1e978bea9','current_temperature')}}"
        unit_of_measurement: "°C"
        device_class: temperature 
        state_class: measurement

#Make sure to indent the rest of your state-based sensors so they line up with Heizung Esszimmer Temperatursensor above. 
#If you have other domain template entities like binary sensors, their block will need a hyphen too.
(...)
1 Like

That looks perfect! I think that’s how it works.
Thank you very much

In the meantime, the syntax changed and although the current version still works, I have an updated version:

template:
  - triggers:
      - trigger: time_pattern
        hours: /1
      - trigger: homeassistant
        event: start
    actions:
      - action: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: result
    sensor:
      - name: Temperatur Morgen
        unique_id: max_temperatur_morgen
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[1]['temperature'] }}
      - name: Temperatur Heute
        unique_id: max_temperatur_heute
        device_class: temperature
        unit_of_measurement: °C
        state: >
          {{ result['weather.home'].forecast[0]['temperature'] }}

Edit: corrected for @123’s comment

Your updated example isn’t using all of the latest syntax.

According to the documentation, trigger: is now triggers: and action: is now actions: (just like in automations).

I believe the change was made about 2 months ago.

1 Like

AFAIK: this does return the current temp, not the max for today
:man_facepalming: :blush:

That is from the original post - and in my test this night, it showed 30.9° at 01:00, which would definitely be the day’s high.

Thanks, that is correct and works - I was just using, what the Studio Code Server Addon was telling me.
Seems it is not on the very latest state.

Sorry, you’re right. :+1:

TBH: I have been struggling with complex templates to retrieve that value from the hourly forecast… :upside_down_face:

Now, as I saw your post I was trying this but still with hourly :blush:

That Addon includes an extension called Home Assistant Config Helper which validates the syntax of Home Assistant’s implementation of YAML.

In the case where multiple versions of syntax are valid, it won’t necessarily guide you to use the latest version. Therefore what you originally posted contains valid syntax but is not an example of the latest valid syntax (which seem to be the entire point of your resurrection of this topic that has been dormant for a year; to post an example with updated syntax).

The documentation remains the authority for the latest information.