Get weather forecast in ESPhome

Now that we have to make a service call to get weather info, how would I translate the following automation into esphome yaml so that I can grab the entities and display home on an esp32 based device:-

actions:
  - target:
      entity_id: weather.forecast_home
    data:
      type: hourly
    response_variable: forecast_data
    action: weather.get_forecasts

So I know how to make service calls in esphome, but I am not sure how to get the return value into an object I can then parse to get the data out.

More forum searching and I think I have my answer here:-

I’m not sure if my syntax is correct, can anyone confirm:-

template: !include templates.yaml
## Weather based template trigger to send esphome devices data

- trigger:
    - trigger: time
      at: "07:00:00"
  action: 
    - action: weather.get_forecasts
      data:
        type: daily
      target: 
        entity_id: weather.forecast_home
      response_variable: forecast_data
  sensor:
    - name: "Forecast Temperature (current)"
      unique_id: forecast_temperature_current
      unit_of_measurement: "%"
      state: >
          {{ forecast_data['weather.forecast_home'].forecast[0].temperature }}
    - name: "Forecast  Temperature (high)"
      unique_id: forecast_temperature_high
      unit_of_measurement: "%"
      state: >
          {{ forecast_data['weather.forecast_home'].forecast |      
          selectattr('datetime', 'search', now().timestamp() |
          timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | max }}
    - name: "Forecast  Temperature (low)"
      unique_id: forecast_temperature_low
      unit_of_measurement: "%"
      state: >
          {{ forecast_data['weather.forecast_home'].forecast |      
          selectattr('datetime', 'search', now().timestamp() |
          timestamp_custom('%Y-%m-%d')) | map(attribute='temperature'){ | min }}

Update: seems to work, for anyone else that wants to use this.

1 Like