javascriptjsonhttpopenweathermapweather

Is there a way to get past precipitation data in OpenWeatherMap API?


Using OpenWeatherMap API (onecall 2.5), using 6 days of previous weather data in a certain area, why is there no precipitation in the JSON data?

Is there any data for this, and if there is, how do I receive it using a onecall 2.5 HTTP request?

Here's my full code:

const API_KEY = "XXX";
const LATITUDE = 26.640629;
const LONGITUDE = -81.872307;

const endDate = new Date();
const startDate = new Date(endDate.getTime() - (5 * 24 * 60 * 60 * 1000));

async function getWeatherData() {
  try {
    const weatherDataList = [];
    for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
      const weatherData = await (await fetch(`https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=${LATITUDE}&lon=${LONGITUDE}&dt=${Math.floor(date.getTime() / 1000)}&appid=${API_KEY}&units=imperial`)).json();

      const weatherDataLocalList = [];

      weatherDataLocalList.push(weatherData.current.temp);
      weatherDataLocalList.push(weatherData.current.humidity);
      weatherDataLocalList.push(weatherData.current.pressure * 0.02952998751);
      weatherDataLocalList.push(weatherData.current.wind_speed * 0.44704);
      weatherDataLocalList.push(1);

      weatherDataList.push(weatherDataLocalList);
    }
    predict2(weatherDataList);
  } catch (error) {
    console.error("Error fetching weather data:", error);
  }
}

getWeatherData();

And this is the URL I'm using: https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=${LATITUDE}&lon=${LONGITUDE}&dt=${Math.floor(date.getTime() / 1000)}&appid=${API_KEY}&units=imperial


Solution

  • The data returned by the One Call API 3.0 (for historical data) returns two sub-fields with precipitation data:

    Each field contains an array with the precipitation rate (mm/hr) at each hour.

    If these sub-fields are missing, that means there was no precipitation:

    If you do not see some of the parameters in your API response it means that these weather phenomena are just not happened for the time of measurement for the city or location chosen. Only really measured or calculated data is displayed in API response.


    Note this answer is for One Call API 3.0. According to this migration guide, API 2.5 had the data, but in slightly different fields: current.rain and current.snow. (API 2.5 will be shut down June 2024.)