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