[PETITION] Don't delete the forecast attribute

Based on this example, for periodically retrieving Calendar Events and storing them in the attribute of a Trigger-based Template Sensor, here’s the same thing for a Weather Forecast (untested because I am not running the beta of 2023.9).

In this example, an automation periodically gets the hourly weather forecast and transfers it to a Template Sensor. Afterwards, you can access the forecast’s details directly from the Template Sensor.


Automation

Create an automation that uses a Time Pattern Trigger to retrieve the forecast every hour and then posts it via a custom Home Assistant Event named weather_forecast_hourly.

alias: Get Weather Forecast Hourly
description: ""
trigger:
  - platform: time_pattern
    hours: /1
condition: []
action:
  - service: weather.get_forecast
    data:
      type: hourly
    target:
      entity_id: weather.home
    response_variable: hourly
  - event: weather_forecast_hourly
    event_data:
      hourly: "{{ hourly}}"
mode: single

Trigger-based Template Sensor

Create a Trigger-based Template Sensor that uses an Event Trigger to receive the hourly forecast, via weather_forecast_hourly, and reports it in an attribute named forecast. The sensor’s state simply reports the time when the sensor was updated.

template:
  - trigger:
      - platform: event
        event_type: weather_forecast_hourly
    sensor:
      - name: Weather Forecast Hourly
        unique_id: weather_forecast_hourly
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ trigger.event.data.hourly.forecast }}"

You can access the sensor’s forecast attribute in the usual manner:

{{ state_attr('sensor.weather_forecast_hourly', 'forecast') }}
5 Likes