Automation to retrieve and Publish hourly weather

Templated script variables can help to reduce duplication and improve legibility.

- id: rain_notification
  alias: "Rain notification for the day"
  trigger:
    - platform: time
      at: '07:00:00'
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.forecast_home
      data:
        type: hourly
      response_variable: my_hourly_forecast

    - variables:
        rainy_times: >
          {{ my_hourly_forecast['weather.forecast_home'].forecast
            | selectattr('condition', 'eq', 'rainy') 
            | map(attribute='datetime') | map('as_datetime')
            | select('ge', today_at('07:00'))
            | select('lt', today_at('22:00'))
            | map('as_timestamp') | map('timestamp_custom', '%H:%M')
            | list }}
        rain_msg:
          - "Rain is expected at the following times: "
          - "{{ rainy_times | join('\n') }}"
          - "Don't forget your umbrella!"
        no_rain_msg: "No rain in the forecast. Enjoy your day!"
        msg: "{{ rain_msg | join('\n') if rainy_times else no_rain_msg }}"

    - service: notify.mobile_app_pixel_7_pro
      data_template:
        message: '{{ msg }}'
        title: "Hourly Forecast Notification"
        data:
          persistent: true 

    - service: notify.telagram_notifier
      data_template:
        message: '{{ msg }}'
        title: "Rain Forecast"

NOTES

Let me know if the newlines in the messages are correctly passed to the two notifiers (I don’t have a means of testing it).


BTW, is telagram the actual spelling in your system or is that a typo (and should be telegram)?

1 Like