New weather forecast template

I’m a “non-HA dev” (a user, not a developer) but I know just enough about Github to know where to find Home Assistant’s Issues, PRs, Discussions, etc (because they don’t have those technical discussions here).

BTW, I posted that link to dispel the notion that the change was the product of short-sightedness (because that accusation has been made more than once).

Any way to call forecast with in a TTS? I am just trying to read out the next day’s data:

service: tts.google_translate_say
data:
  entity_id: media_player.master_bedroom_speaker
  message: >-
    Ok Goodnight mode triggered. The alarm system activated and the home is
    secure. The weather for tomorrow is forecasted as 
    {{state_attr('weather.downstairs','forecast')[0].condition}}
    with a low of {{ state_attr('weather.downstairs','forecast')[0].templow }}  
    and a high of {{ state_attr('weather.downstairs','forecast')[0].temperature }}

You will need to either set up a template sensor to hold the required data or call the weather.get_forecasts service in your script before the TTS service call (see Post #92 ) and modify your TTS templates as required.

first service:

service: weather.get_forecasts
target:
  entity_id: weather.downstairs
data:
  type: daily
response_variable: response

then

service: tts.google_translate_say
data:
  entity_id: media_player.master_bedroom_speaker
  message: >-
    Ok Goodnight mode triggered. The alarm system activated and the home is
    secure. The weather for tomorrow is forecasted as 
    {{ response['weather.downstairs'].forecast[0].condition}}
    with a low of {{ response['weather.downstairs'].forecast[0].templow }}  
    and a high of {{ response['weather.downstairs'].forecast[0].temperature }}
2 Likes

This worked perfect once I figured out that you can’t run services individually in an automation to test but had to run the entire automation for the response variable to be valid from one service to the other.

Thanks for the help!!

1 Like

This is the way.

Thank you for this, I’ve been fighting with this for weeks, and this finally worked. And yea, it’s impossibly difficult to troubleshoot!

I’ve spent 6hrs trying to get a plot of the temperature from an hourly weather forecast to work. I’ve tried every damn template and example in every damn post. Feel like I’m being gaslit… Anyone know what is going wrong here?

I have the default weather.forecast_home from the Meteorologisk institutt (Met.no) integration ( http://homeassistant.local:8123/config/integrations/integration/met )

In dev tools services ( http://homeassistant.local:8123/developer-tools/service ) i can do:

service: weather.get_forecasts
data:
  type: hourly
target:
  entity_id:
    - weather.forecast_home

and i get back:

weather.forecast_home:
  forecast:
    - condition: rainy
      datetime: "2024-05-18T15:00:00+00:00"
      wind_bearing: 337.9
      cloud_coverage: 52.3
      temperature: 19.7
      wind_speed: 13.7
      precipitation: 0.2
      humidity: 64
    - condition: partlycloudy
      datetime: "2024-05-18T16:00:00+00:00"
      wind_bearing: 60.5
      cloud_coverage: 69.5
      temperature: 18.4
      wind_speed: 13.7
      precipitation: 0
      humidity: 71
    - condition: sunny
      datetime: "2024-05-18T17:00:00+00:00"
      wind_bearing: 98.3
      cloud_coverage: 4.7
      temperature: 18.4
      wind_speed: 4.3
      precipitation: 0
      humidity: 70
etc

But as we see in this huge thread, those attributes are not supposed to be used anymore, so we need to make a custom sensor to replace it.

In the config helpers ( http://homeassistant.local:8123/config/helpers ) I have created a template sensor:

This is using a template that worked for numerous users on march 8th ( Unsure How To Use Hourly Forecast Information (Since 2024.3.0) · Issue #112628 · home-assistant/core · GitHub ), although I did see that the weather.home has changed to weather.forecast_home between then and now, so I made that adjustment

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    sensor:
      - name: Weather Hourly
        state: "{{ states('weather.forecast_home') }}"
        attributes:
          temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}"
          dew_point: "{{ state_attr('weather.forecast_home', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.forecast_home', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.forecast_home', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.forecast_home', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.forecast_home', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.forecast_home', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('weather.forecast_home', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.forecast_home', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('weather.forecast_home', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('weather.forecast_home', 'precipitation_unit') }}"
          forecast: "{{ hourly['weather.forecast_home'].forecast }}"

However it is shown as unavailable and doesn’t get any data:

Which means then it fails to plot data in a plotly graph card:

type: custom:plotly-graph
hours_to_show: 16
time_offset: 3h
entities:
  - entity: sensor.smile_anna_outdoor_temperature
    line:
      width: 3
      color: orange
  - entity: sensor.smile_anna_outdoor_temperature
    name: Temperature yesterday
    time_offset: 1d
    line:
      width: 1
      dash: dot
      color: orange
  - entity: sensor.weather_hourly
    attribute: temperature
    unit_of_measurement: °C
    time_offset: 0d
    name: Forecast temperature
    line:
      width: 1
      dash: dot
      color: grey

Any help would be appreciated

The Template Helper only supports basic state-based Template Sensors or Binary sensors. The “State template” field should only contain Jinja templating, YAML configuration is not supported in the field.

Attributes are not supported and, as stated above, neither are trigger-based sensors… they must be set up in your YAML configuration.

1 Like

Thanks for the reply. Can you point me to the yaml configuration file you’re referring to?

The gui menu should mention this incompatibility. It has a choice for “template sensor” but then only actually works some some types and silently fails for the rest

And mate, yeah sure that was “As stated earlier” but it was …in comment number 96/129 of a forum thread. Let’s not pretend that’s a valid replacement to having clear signposting in the software itself

1 Like

I don’t disagree with you that the editor could use some attention… this seems to be a relatively common point of confusion since the introduction of Template Helpers and their UI editor earlier this year.

Maybe start a discussion on the frontend git explaining what you think would make the Helper editor more self-explanatory or easy to use.

1 Like

FYI the reason you’re confused is because you think the whole yaml chunk is the template, when templates are actually just the code between {% %}, {{ }}, and {# #} tags.

1 Like

Can anyone spot what I’m missing? This is using the format @petro suggested in New weather forecast template - #21 by petro

template:
- trigger:
  - platform: time_pattern
    minutes: "/2"
  action:
    - service: weather.get_forecasts
      data:
        type: daily
      target:
        entity_id: weather.tomorrow_io_home_daily
      response_variable: daily
    - variables:
        today: "{{ daily['weather.tomorrow_io_home_daily'].forecast[0] }}"
  sensor:
    - name: "Today's High Temperature"
      unique_id: tomorrow_io_d0_high_temp
      state: "{{ today.temperature }}"
      unit_of_measurement: °F
    - name: "Today's Low Temperature"
      unique_id: tomorrow_io_d0_low_temp
      state: "{{ today.templow }}"
      unit_of_measurement: °F
    - name: "Today's Precipitation"
      unique_id: tomorrow_io_d0_precipitation
      state: "{{ today.precipitation }}"
      unit_of_measurement: "in"
    - name: "Today's Rain Chance"
      unique_id: tomorrow_io_d0_precipitation_probability
      state: "{{ today.precipitation_probability }}"
      unit_of_measurement: "%"

This is all I get:

The opening {{ were missing from the templates. But you edited the code in the post, so it may be different now.

I recently got a similar issue with the weather forecast, but am not sure how to get this part working again. Could somebody help me what kind of template (and where) I should be building? It was working with the ‘met.no’ integration

 #Weather forecast temperature next day
   - platform: template
     sensors:
       temp_forecast_tomorrow:
         friendly_name: "Temperature forecast tomorrow"
         unit_of_measurement: '°C'
         value_template: "{{ state_attr('weather.de_woud', 'forecast') | map(attribute='temperature') | first }}" 

Thanks, I wasn’t the brightest light earlier this week. Think I got it working now! :slight_smile:

Hi Folks, needing some assistance here.

After reading through all the replies and the code on this thread, I’m still having an issue setting this up.

this is my sensor code:

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: state
      entity_id: weather.forecast_home
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      data:
        type: daily
      target:
        entity_id: weather.forecast_home
      response_variable: daily
  sensors:
    weather_forecast_daily:
      value_template: "{{ daily['weather.forecast_home', 'forecast']['temperature'] | float }}"
      attribute_templates:
        forecast: "{{ daily['weather.forecast_home', 'forecast'] }}"

When checking through Developer Tools > Template, I get the following error:

UndefinedError: ‘daily’ is undefined

what am I missing here?

calling the weather.get_forecast service gives full response

service: weather.get_forecasts
data:
  type: daily
target:
  entity_id:
    - weather.forecast_home

The template editor tool does not have access to the values from the service call unless you provide them in the form of a variable, this is the cause of the error.

The template’s issues include:

  1. Mashed together legacy and contemporary template sensor syntax. Only contemporary syntax will work for trigger-based sensors.
  2. daily['weather.forecast_home', 'forecast'] is not a thing… you likely want daily['weather.forecast_home']['forecast']
  3. attribute_templates was deprecated over 3 years ago.
  4. No index number to determine which forecast to pull the temperature from.

Describe what information you want the sensor to hold and we can probably help you sort it out.

Try this.

{{daily['weather.forecast_home'].forecast[0].temperature }}

hi i need some help with fixing some issues the new weather template causes the first couple errors i get i been try to fix is

Template variable error: 'dict object' has no attribute 'weather.peterborough_forecast' when rendering '{{ has_value(hourly['weather.peterborough_forecast'].forecast[0].temperature) }}'
Referenced entities weather.peterborough_forecast are missing or not currently available
Error rendering availability template for sensor.today_rain: AttributeError: 'float' object has no attribute 'lower'
Error rendering availability template for sensor.temperature_forecast_next_hour: UndefinedError: 'dict object' has no attribute 'weather.peterborough_forecast'

i been trying a float(0) default(0) etc but here is my code… what i trying to do is. if it doesnt get anything just place unavaliable or 0 etc… and not throw a log error
here is my code

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id:
            - weather.peterborough_forecast
            - weather.home
        response_variable: hourly
    sensor:
      - name: Today Rain
        unique_id: today_rain
        state: "{{ hourly['weather.home'].forecast[0].precipitation |float(0)}}"
        #state: "{{ forecast['weather.home'].forecast[0].precipitation }}"
        availability: "{{ has_value(hourly['weather.home'].forecast[0].precipitation) }}"
        unit_of_measurement: mm

      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.peterborough_forecast'].forecast[0].temperature | float(0)}}"
        #state: "{{ forecast['weather.peterborough_forecast'].forecast[0].temperature }}"
        availability: "{{ has_value(hourly['weather.peterborough_forecast'].forecast[0].temperature) }}"
        unit_of_measurement: °C