I am trying to make a query to an external API (weatherstack) and in the test, Zapier returns the error 'Please define output or return early'.
I've tried many things, but I don't know what's wrong 😥
Below is the code I am using in 'Code by zapier':
import requests
def query_time(input_data):
# Take latitude and longitude values from Zapier input.
lat = input_data['latitude']]
lon = input_data['longitude']]
# Take the date from the Zapier input
date = input_data['date']]
# Set the default time
start_time = '06:00:00'
end_time = '23:59:59'
# Combine date and time to create time variables
start_time = f'{date}T{start_time}'
end_time = f'{date}T{end_time}'
# Set query parameters
base_url = 'http://api.weatherstack.com/historical'
api_key = 'API_KEY'
# Make the API query
url = f'{base_url}?access_key={api_key}&query={lat},{lon}&historical_date={date}&hourly=1'
response = requests.get(url)
# extract the parameters you need from the response
if response.ok:
data = response.json()
hourly_data = data['historical'][date]
temp_min = float('inf')
temp_max = float('-inf')
temp_sum = 0
count = 0
for hourly in hourly_data:
time = hourly['time']
if start_time <= time <= end_time:
temp = hourly['temperature']
temp_min = min(temp_min, temp)
temp_max = max(temp_max, temp)
temp_sum += temp
count += 1
temp_avg = round(temp_sum / count, 2)
# Print the results of the query
print(f'Maximum temperature: {temp_max}°C')
print(f'Minimum temperature: {temp_min}°C')
print(f'Average temperature: {temp_avg}°C')
else:
print('Error in making API query')
# Define the output fields and their values
output = {
'temp_max': temp_max,
'temp_min': temp_min,
'temp_avg': temp_avg
}
# Return the results of the query
return output
Thank you very much for your time 🤗