javaweatherapache-httpclient-5.x

How to pull historical data from AmbientWeather


I'm trying to understand how to use the API for Ambient Weather (https://ambientweather.docs.apiary.io) and I thought I knew what to do, but I'm missing something.

When I try to pull data from my weather station, it only gives me one data point "lastdata", even though I've set endDate to a week before.

public static void main(String[] args) throws Exception {
    String API_URL = "https://api.ambientweather.net/v1/devices";
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -7);
    long endDate = calendar.getTimeInMillis();
    System.out.println("attempting to pull data using " + endDate + " which is " + new Date(endDate));
    int limit = 2;
    
    String url = String.format("%s?applicationKey=%s&apiKey=%s&endDate=%s&limit=%s", API_URL, applicationKey, apiKey, endDate, limit);
    System.out.println("URL [" + url + "]");

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet request = new HttpGet(url);
    org.apache.hc.client5.http.impl.classic.CloseableHttpResponse response = httpClient.execute(request);

    HttpEntity entity = response.getEntity();
    String jsonResponse = EntityUtils.toString(entity);
    System.out.println("output [" + jsonResponse + "]");
    
    int dateUtcLoc = jsonResponse.indexOf("\"dateutc\":"); 
    while (dateUtcLoc > -1) {
        int commaAfterLoc = jsonResponse.indexOf(",",dateUtcLoc);
        String datapointMsString = jsonResponse.substring(dateUtcLoc + 10, commaAfterLoc);
        System.out.println("received " + datapointMsString + " which is " + new Date(Long.parseLong(datapointMsString)));
        jsonResponse = jsonResponse.substring(commaAfterLoc);
        dateUtcLoc = jsonResponse.indexOf("\"dateutc\":");
    }
    response.close();
}

The output I get is just one data point from "now". I would have expected two data points and from one week ago.

attempting to pull data using 1740525231967 which is Tue Feb 25 18:13:51 EST 2025
URL [https://api.ambientweather.net/v1/devices?applicationKey= ... &apiKey= ... &endDate=1740525231967&limit=2]
output [[{"macAddress":"30:83 ... ","lastData":{"dateutc":1741129920000,"tempf":64.2,"humidity":60,"windspeedmph":2.01,"windgustmph":2.24,"maxdailygust":15.88,"winddir":207,"uv":0,"solarradiation":2.02,"hourlyrainin":0,"dailyrainin":0,"weeklyrainin":0,"monthlyrainin":0,"totalrainin":112.378,"battout":1,"tempinf":70.3,"humidityin":44,"baromrelin":29.905,"baromabsin":29.291,"temp1f":57.9,"humidity1":66,"temp2f":55.8,"humidity2":63,"batt1":1,"batt2":1,"batt_co2":1,"feelsLike":64.2,"dewPoint":50.02,"feelsLike1":57.9,"dewPoint1":46.6,"feelsLike2":55.8,"dewPoint2":43.4,"feelsLikein":69.1,"dewPointin":47.3,"lastRain":"2025-02-27T21:59:00.000Z","tz":"America/New_York","date":"2025-03-04T23:12:00.000Z"},"info": ...
received 1741129920000 which is Tue Mar 04 18:12:00 EST 2025

Solution

  • You need to specify the device id in the url. So the format is:

    https://api.ambientweather.net/v1/devices/<device id>?<parameters>
    

    and an example:

    https://api.ambientweather.net/v1/devices/00-10-FA-63-38-4A?apiKey=YOURAPIKEY&applicationKey=YOURAPPKEY&limit=10&endDate=2025-02-01
    

    This URL (using a fake mac address - replace with your own) requests all data (limited to 10 items) for the device ending on date 2025-02-01 and going back in time based on data available (every 5 mins here).

    [Without the device id, the API is listing your devices (1) and providing the latest data point. That's one way to determine your device id.]

    And the result:

    [
      {
        "dateutc": 1738368000000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.826,
        "baromabsin": 29.265,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50.2,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50.2,
        "dewPoint1": 46.8,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738368000000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738368000000.json",
        "date": "2025-02-01T00:00:00.000Z"
      },
      {
        "dateutc": 1738367700000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.82,
        "baromabsin": 29.259,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50.4,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50.4,
        "dewPoint1": 47,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:55:00.000Z"
      },
      {
        "dateutc": 1738367400000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.814,
        "baromabsin": 29.253,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50.2,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50.2,
        "dewPoint1": 46.8,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:50:00.000Z"
      },
      {
        "dateutc": 1738367100000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.82,
        "baromabsin": 29.259,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50.2,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50.2,
        "dewPoint1": 46.8,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:45:00.000Z"
      },
      {
        "dateutc": 1738366800000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.817,
        "baromabsin": 29.256,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50,
        "dewPoint1": 46.6,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:40:00.000Z"
      },
      {
        "dateutc": 1738366500000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.817,
        "baromabsin": 29.256,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50,
        "dewPoint1": 46.6,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:35:00.000Z"
      },
      {
        "dateutc": 1738366200000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.817,
        "baromabsin": 29.256,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50,
        "dewPoint1": 46.6,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:30:00.000Z"
      },
      {
        "dateutc": 1738365900000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.826,
        "baromabsin": 29.265,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50,
        "dewPoint1": 46.6,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:25:00.000Z"
      },
      {
        "dateutc": 1738365600000,
        "tempinf": 73.8,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.817,
        "baromabsin": 29.256,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 50,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 50,
        "dewPoint1": 46.6,
        "feelsLikein": 72.2,
        "dewPointin": 38.6,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:20:00.000Z"
      },
      {
        "dateutc": 1738365300000,
        "tempinf": 73.6,
        "battin": 1,
        "humidityin": 28,
        "baromrelin": 29.826,
        "baromabsin": 29.265,
        "hourlyrainin": 0,
        "eventrainin": 0,
        "dailyrainin": 0,
        "weeklyrainin": 0,
        "monthlyrainin": 0.012,
        "yearlyrainin": 0.012,
        "temp1f": 49.8,
        "humidity1": 88,
        "batt1": 1,
        "feelsLike1": 49.8,
        "dewPoint1": 46.4,
        "feelsLikein": 72,
        "dewPointin": 38.5,
        "lastRain": "2025-01-01T10:04:00.000Z",
        "time": 1738281600000,
        "loc": "5caa03aaf28215347b0f9fef166d2129/daily/1738281600000.json",
        "date": "2025-01-31T23:15:00.000Z"
      }
    ]
    

    Another method to obtain Your mac address is on your account page under 'Devices'.

    The API docs are poor - what I did was scan github (or google) for ambient weather projects and look at source examples.

    What would be more useful would be to get "hourly observations for a given date" - as it is you'd have to request a whole bunch of data and cull out the hourly values - so there should be a better way.