Getting forcasts in a REST Command

Dear all,

I’m having a blast with my new HA setup. I got a cool REST Command running and I want more! Here’s the idea: I want to POST the weather forecast data to a little server of mine, that then displays it on an eink display.

In the default dashboard there’s a weather card that show the temperature for example (state_attr("weather.forecast_home", "temperature") gets me that value in the template editor too). What that weather card on my dashboard also shows is, once I tap on it, the forecast. Great. This, I believe is powered by the new action, called weather.get_forecast. So far so good! I can even inspect the action’s YAML in the developer tools:

action: weather.get_forecasts
target:
  entity_id: weather.forecast_home
data:
  type: daily

I can run said action and get a pretty response:

weather.forecast_home:
  forecast:
    - condition: rainy
      datetime: "2024-09-06T10:00:00+00:00"
      wind_bearing: 128.6
      temperature: 19.9
      templow: 19.1
      wind_speed: 3.31
      precipitation: 0.2
      humidity: 94
    - condition: cloudy
      datetime: "2024-09-07T10:00:00+00:00"
      wind_bearing: 111.5
      temperature: 25.3
      templow: 16.1
      wind_speed: 4.11
      precipitation: 0
      humidity: 82

Note the forecast key! I would expect I would be able to do something like this in my template state_attr("weather.forecast_home", "forecast"). Alas, no luck. If I look at the States tab in the dev tools I can find the weather.forecast_home entity but no associated forecast key. What gives?

The REST Command I intend to make looks like this (not sure that tojson works):

  send_forecast:
    url: "http://192.168.178.191/weather"
    method: POST
    headers:
      Content-Type: application/json
    payload: '{{ weather.forecast_home.forecast |tojson }}'

okayyy, im also not a pro in Home Assistant but i have a guess:

I think the templates are primarely to display already existing entity states and/or their attributes.
When you perform the action, you will get the data, but this data will not be saved as a state or attribute. You already found this out right here:

If I look at the States tab in the dev tools I can find the weather.forecast_home entity but no associated forecast key

Option 1:
You could do a workaround and make a script or automation that performs the action and stores this somewhere (for example helper variables)

This script is what i could type up really quick

alias: test weather
sequence:
  - action: weather.get_forecasts
    target:
      entity_id: weather.wetterzuhause
    data:
      type: daily
    response_variable: weather_forecast
  - data:
      entity_id: input_text.weather_forecast_temperature
      value: "{{ weather_forecast['weather.wetterzuhause'].forecast[0].temperature }}"
    action: input_text.set_value
description: ""

This script calls the weather.get_forecasts action and stores the “answer” in the response_variable “weather_forecast”. the “wetterzuhause” is just how my weather is named in my HomeAssistant.
But then watch out: at least for me it gives a nested variable like this:

weather_forecast:
  weather.wetterzuhause:
    forecast:
      - datetime: '2024-09-06T05:00:00+00:00'
        cloud_coverage: 43
        precipitation_probability: 40
... (a lot more)

so for the curent day temperature i take the first element (0) out of this forecast variable like you see in the script too:

{{ weather_forecast['weather.wetterzuhause'].forecast[0].temperature }}

(for the next day you would to 1, then 2 etc
I store this in a helper variable that i created quickly (input_text.weather_forecast_temperature).

You can use this variable very easy in templates like you did before, so in my example:

{{ states('input_text.weather_temperature') }}

Option 2:
A easier way:
If you want to use another Integration like AccuWeather, which you can easily install in HomeAssistant, but i think you need an API key, which is free. On the website i see they have a limitation of 50 calls a day, so watch out for that
Here the Link to the Integration Pagehttps://www.home-assistant.io/integrations/accuweather/.
If you have this set up you get forecast Entitys in your HomeAssistant, you can also see them easily in your States in the DevTools then and can use them in Templates.

Maybe someone has a way better or easier way, but those are my thoughts ^^

1 Like

I feel that you are right; templates are used to display already present data.
This is why I’m so puzzled by the fact weather.forecast_home doesn’t have a key called forecast. I have a weather forecast card on my (initial) dashboard that shows daily AND hourly forecasts.
What also puzzles me is that I have an entity called weather.forecast_home and that doesn’t include a forecast.

I was expecting get_forecasts to run periodically and populate something. It seems this expectation is wrong.

When you write:

You can flip data and action right? Such that it is in the same order as the entry above it.

yes ofc, sorry i copy pasted so much around that it switched

1 Like

I finally got something to work!

The forecast call:

action: weather.get_forecasts
metadata: {}
data:
  type: hourly
response_variable: morning_forecast
target:
  entity_id: weather.forecast_home

The REST call references a variable I set earlier in the weather.get_forecast call:

action: rest_command.send_forecast
metadata: {}
data:
  morning_forecast: "{{ morning_forecast }}"

Where the REST call is defined as such:

  send_forecast:
    url: "http://192.168.178.191:8000/weather"
    method: POST
    headers:
      Content-Type: application/json
    payload: '{{ morning_forecast | to_json }}'

Still seems weird but I’ll live with it! Thank you for the help.

@JannekKn is correct that the call to get_forecasts retrieves the forecast but it only retreives. it does not store it anywhere. if you define a response_variable, it stores the results in your response_variable but only for the scope of that variable.

if you need this only on demand, periodically when you call get_forecast, then immediately sending it to where you want with the rest_command perhaps does the job you want.

however if this is something that you’d retrieve on a regular basis, such that doing a get_forecasts each time is inefficient, the typical thing to do is to create a sensor that stores the information and then you update the sensor on a regular (perhaps hourly?) basis.

so for example i have:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_weather_bellevue
        response_variable: hourly
    sensor:
      - name: forecast next hour
        unique_id: hourly_forecast_met
        state: "{{ hourly['weather.forecast_weather_bellevue'].forecast[0].temperature }}"
        unit_of_measurement: °F  
        attributes:
          forecast: "{{ hourly['weather.forecast_weather_bellevue'].forecast }}"
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start

now i have a sensor named sensor.forecast_next_hour that has the info in it stored in its attributes:

1 Like