How to get historical data about a thing

Hi!

I want to fetch the historical values of a thing and believe that seriesV2HistoricData is the right function to call. However I am struggling to know what to fill in as property (uuid). I have tried with thing id, thing property id, and something called "user_id" which is returned by thingsV2List

I get this error flutter: {"id":"jl+qqBjn","code":"bad_request","status":400,"detail":"failed to decode request body with content type \"application/x-www-form-urlencoded; charset=utf-8\": is not a valid index for type []uuid.UUID"}

Can anyone help me how to request the historical data of a thing?

I've never done it but from the doc it seems you need to send something like this on a unix command line

curl -X POST "https://api2.arduino.cc/iot/v2/series/historic_data" 
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" 
-H "Content-Type: application/json" 
-d '{
   "from": "START_TIMESTAMP",
   "properties": ["PROPERTY_ID1", "PROPERTY_ID2"],
   "to": "END_TIMESTAMP"
}'

(all in one line don't add the new lines, this is just tp make it readable)

you need to provide your access token, the timestamps and properties in a JSON

may be with flutter it could be something like

String apiUrl = "https://api2.arduino.cc/iot/v2/series/historic_data";
String accessToken = "YOUR_ACCESS_TOKEN";

void fetchData() async {
    final response = await http.post(
      Uri.parse(apiUrl),
      headers: {
        "Authorization": "Bearer $accessToken",
        "Content-Type": "application/json",
      },
      body: jsonEncode({
        "from": "START_TIMESTAMP",
        "properties": ["PROPERTY_ID1", "PROPERTY_ID2"],
        "to": "END_TIMESTAMP",
      }),
    );

    if (response.statusCode == 200) {           // Successfully fetched data
      final data = jsonDecode(response.body);
      print(data);
    } else {                                    // Handle error
      print("Error: ${response.statusCode}");
      print("Response: ${response.body}");
    }
  }
1 Like

I have tried with thing id and thing pid, e.g.:

  String history = "";
  List<String> properties = [
    "thing id / thing pid",
  ];
  try {
    final response = await http.post(
      Uri.parse("https://api2.arduino.cc/iot/v2/series/historic_data"),
      body: {
        "from": "2023-09-05",
        "properties": json.encode(properties),
        "to": "2023-09-07",
      },
      headers: {"Authorizatoin": "Bearer $arduinoAccessToken"},
    );
    history = response.body;
  } catch (e) {
    print(e);
  }
  return history;

but still get {"id":"KXaV5G48","code":"bad_request","status":400,"detail":"failed to decode request body with content type \"application/x-www-form-urlencoded; charset=utf-8\": is not a valid index for type []uuid.UUID"}

Do you have any clue what I need instead for the property?

you use

it seems the content type should be application/json according to the documentation you pointed at

hence my suggested

the properties seems to be UUIDs

1 Like

Adding the Content-Type header seems to be solving some issue, although now i get {"id":"RtnAsibk","code":"unauthorized","status":401,"detail":"not authorized"}
Even though I have a valid access token. Do you know what could cause this?

did you spell Authorization correctly (you had Authorizatoin)

did you use IDs for these ?

1 Like

Thanks.. That one was embarrasing. It seems as if I am getting closer. I do not receive any data but I hope playing around with the time will fix it.

we have all been there
:wink:

print the full answer, not just the body. you might get extra information.

if you did not provide the right IDs that could be a reason why the answer is empty

1 Like

Seems as if this wasn't the function I wanted to query after all

Request sending of historical data of properties by email

So the request is working (why my mail inbox is filled with historical data unintentionally). I will look for some other function.

that's what the doc states

seriesV2HistoricData
historic_data series_v2
Request sending of historical data of properties by email

may be you wanted * seriesV2BatchQuery or seriesV2BatchQueryRaw?

1 Like

propertiesV2Timeseries seems very promising to being what I want. I now get the historical data. Thanks for all the help!

cool

have fun

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.