Debugging templates and service response_variables

Hi All,
I just finished figuring out how to migrate to the new “forecast doesn’t include the forecast in attributes” update and learned a few things about debugging templates from several others that I will collect here.

1. Trigger on event_template_reloaded to update whenever you update templates
@FloatingBoater commented that adding this allows the template to be updated each time you reload. This is great if you want your template to update every hour but don’t want to wait each time you change something

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded

2. Debug service response_variables by running the service then translating the result to YAML and pasting it into the template tool.
It is not possible to execute a service in the template editor, so instead you need a two-step process.

First run the action in the Developer Tools->Service and you will get your results in YAML format. E.g.,

service: weather.get_forecast
data:
  type: hourly
target:
  entity_id: weather.home
response_variable: forecast

produces:

forecast:
  - condition: sunny
    precipitation_probability: 0
    datetime: "2023-09-24T10:00:00+00:00"
    wind_bearing: 249.3
    cloud_coverage: 0.7
    temperature: 15.6
    wind_gust_speed: 31.3
    wind_speed: 15.1
    precipitation: 0
    humidity: 68
  - condition: sunny
    precipitation_probability: 0
    datetime: "2023-09-24T11:00:00+00:00"
    wind_bearing: 254.9
    cloud_coverage: 0.8
    temperature: 16.5
    wind_gust_speed: 33.1
    (etc...)

Then copy the output into JSONFormatter, as suggested by @taras133, to get in JSON format:

    "forecast": [
        {
            "condition": "sunny",
            "precipitation_probability": 0,
            "datetime": "2023-09-24T10:00:00+00:00",
            "wind_bearing": 249.3,
            "cloud_coverage": 0.7,
            "temperature": 15.6,
            "wind_gust_speed": 31.3,
            "wind_speed": 15.1,
            "precipitation": 0,
            "humidity": 68
        },
        {
            "condition": "sunny",
            "precipitation_probability": 0,
            "datetime": "2023-09-24T11:00:00+00:00",
            "wind_bearing": 254.9,
            "cloud_coverage": 0.8,
            "temperature": 16.5,
            "wind_gust_speed": 33.1
        }
]

You can then put this in the Developer Tools → Template editor as:

{% set forecast =
{
    "forecast": [
        {
            "condition": "sunny",
            "precipitation_probability": 0,
            "datetime": "2023-09-24T10:00:00+00:00",
            "wind_bearing": 249.3,
            "cloud_coverage": 0.7,
            "temperature": 15.6,
            "wind_gust_speed": 31.3,
            "wind_speed": 15.1,
            "precipitation": 0,
            "humidity": 68
        },
        {
            "condition": "sunny",
            "precipitation_probability": 0,
            "datetime": "2023-09-24T11:00:00+00:00",
            "wind_bearing": 254.9,
            "cloud_coverage": 0.8,
            "temperature": 16.5,
            "wind_gust_speed": 33.1
      }
]
} 
%}

and then debug your template as needed (for me it was finding the minimum)

{{ forecast.forecast[:] | map(attribute='temperature')|min }}

While this is a PITA, there is one part that is especially frustrating, and that is that you have to put the pasted-in JSON in the template before you do your own code. This means a ton of scrolling up and down if you have a week’s worth of data in your JSON. So I suggest only taking the first few entries from JSON output to make it easier.

With this I was able to convert my old (simple) “find the minimum temperature in the next day” template sensor:

- sensor:
    name: "Min Forecast Temperature in Next Day"
    unique_id: min_forecast_temperature_in_next_day
    state: >
        {{ state_attr('weather.home_hourly','forecast')[:] | map(attribute='temperature')|min }}
    attributes:
      state_class: measurement
      device_class: temperature
      unit_of_measurement: °C
      icon: mdi:thermometer

into the new (and improved?) version that uses response_variables:

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecast
      target:
        entity_id: weather.home
      data:
        type: hourly
      response_variable: forecast
  sensor:
    - name: "Min Forecast Temperature in Next Day"
      unique_id: min_forecast_temperature_in_next_day
      state: >
        {{ forecast.forecast[:] | map(attribute='temperature')|min }}
      attributes:
        state_class: measurement
        device_class: temperature
        unit_of_measurement: °C
        icon: mdi:thermometer

(I’m still not sure if those last parts need to be in attributes, though…)

12 Likes