Search code examples
pythonpython-requestshttp-status-code-422

NREL PVwatts API


Good day, everyone. I'm trying to access pvwatts via API using python, however the error status code 422 always shows up. I had checked the parameters and I found nothing -I also had tried the website version for my location and it worked. Can you please help me and teach me about the problem? I am still a newbie in python however I need this for my final project.

I had tried to fill more parameters and even tried with other code, yet it is not working -and sometimes it getting worse. For the code that I tried is:

import requests

# Define the PVWatts API URL
url = 'https://developer.nrel.gov/api/pvwatts/v8.json'

# Define your API key
api_key = 'xoxoxoxoxoxoxoxo'

# Define the parameters for the PVWatts API request
params = {
    'format': 'json',
    'api_key': api_key,
    'system_capacity': 12,  # kW
    'module_type': 0,  # 0 for standard, 1 for premium
    'losses': 14,  # percentage
    'array_type': 1,  # 0 for fixed open rack, 1 for fixed roof mount, 2 for 1-axis tracking, 3 for 1-axis backtracking, 4 for 2-axis tracking
    'tilt': 15,  # degrees
    'azimuth': 0,  # degrees
    'address': 'Cakung, Bekasi',  # location address
    'lat': -6.1833,  # latitude
    'lon': 106.9499,  # longitude
    'dataset': 'nsrdb', # climate dataset
    'timeframe': 'hourly',  # 'hourly' or 'monthly'
    'dc_ac_ratio': 1.2, / # DC/AC ratio
    'gcr': 0.4  # ground coverage ratio
}

# Send the API request
response = requests.get(url, params=params)

# Check if the request was successful (HTTP status code 200)
if response.status_code == 200:
    data = response.json()
    # Process the response data as per your requirements
    # For example, you can access the estimated energy production
    ac_annual = data['outputs']['ac_annual']
    print(f"Estimated annual AC energy production: {ac_annual} kWh")
else:
    print(f"Request failed with status code {response.status_code}")

the final result of the code is to shows the graphs for the current model -however, I want to learn it from the scratch, so I tried to not using the code that existing -also because my prof want to publish it, I can't use the existing code without modification.


Solution

  • A working example with DEMO_KEY can be found on https://developer.nrel.gov/docs/solar/pvwatts/v8/

    Removing the address seem to do it since you have lat and lon

    I believe the address must match an existing weather station and it has precedence of lat,long and if it misses you get a 422

    params = {
        "api_key": 'DEMO_KEY',
        "azimuth":0,
        "lat": -6.1833,  # latitude
        "lon": 106.9499,  # longitude
        "system_capacity":4,
        "losses":14,
        "array_type":1,
        "module_type":0,
        "gcr":0.4,
        "dc_ac_ratio":1.2,
        "inv_eff":96.0,
        "radius":0,
        "dataset": "nsrdb",
        "tilt": 10
    }
    

    gives:

    Estimated annual AC energy production: 5626.015769442323 kWh