Weather Summaries with ChatGPT

Weather Summaries with ChatGPT

This has been QUITE the time-suck. Wow.

I am so close. But I need help.

I have an API call that just isn’t sending the data for some reason.

This is a rest sensor, which produces a response from ChatGPT and saves it as an attribute. It works; just not with the template.

# Weather
  - platform: rest
    name: Weather GPT Response
    resource: https://api.openai.com/v1/chat/completions
    method: POST
    scan_interval: 86400
    headers:
      Authorization: Bearer redacted 
      Content-Type: "application/json"
    payload: >
      {
        "model": "gpt-4-1106-preview",
        "messages": [
          {
            "role": "user",
            "content": "Analyze today's hourly forecast and provide a short summary emphasizing the overall feel of the day and any important events. Forecast for Today: {{ state_attr('weather.forecast_home', 'forecast') | tojson }}"
          }
        ]
      }
    value_template: "1"  
    json_attributes_path: "$.choices[0].message" 
    json_attributes:
      - "content" # Store the full analysis
      - "role"
    timeout: 60
The template itself works fine, though. The output produced in dev tools is:

"content": "Analyze today's hourly forecast and provide a short summary emphasizing the overall feel of the day and any important events. Forecast for Today: [{"condition": "partlycloudy", "datetime": "2024-03-30T16:00:00+00:00", "humidity": 44, "precipitation": 0.0, "temperature": 77, "templow": 66, "wind_bearing": 105.0, "wind_speed": 11.43}, {"condition": "cloudy", "datetime": "2024-03-31T16:00:00+00:00", "humidity": 53, "precipitation": 0.0, "temperature": 78, "templow": 57, "wind_bearing": 113.0, "wind_speed": 10.31}, {"condition": "cloudy", "datetime": "2024-04-01T16:00:00+00:00", "humidity": 60, "precipitation": 0.0, "temperature": 78, "templow": 61, "wind_bearing": 137.9, "wind_speed": 12.74}, {"condition": "cloudy", "datetime": "2024-04-02T16:00:00+00:00", "humidity": 57, "precipitation": 0.0, "temperature": 83, "templow": 65, "wind_bearing": 195.4, "wind_speed": 11.62}, {"condition": "rainy", "datetime": "2024-04-03T16:00:00+00:00", "humidity": 59, "precipitation": 0.07, "temperature": 87, "templow": 69, "wind_bearing": 235.5, "wind_speed": 22.12}, {"condition": "partlycloudy", "datetime": "2024-04-04T16:00:00+00:00", "humidity": 46, "precipitation": 0.26, "temperature": 76, "templow": 66, "wind_bearing": 312.1, "wind_speed": 12.3}]"

I have also tested the API call in Postman with the outputted sample data, and that works. But the API call in the rest sensor just produces a generic response that says I need to give it the data because it does not have access to live data.

The use case was to produce an hourly forecast, send it to ChatGPT, and get an accurate and insightful forecast of the day ahead in natural language.

I have tried to work this out for HOURS. We’re going on about 8 hours for this ONE use case. Crazy. One thing just leads to another. But I’m very close now. Any help would be appreciated.