Bulk Logging Sensor Data

In this tutorial, we will cover the steps to bulk POST your sensor data via the Leftover Labs API. This allows you to store multiple data points in a single request.

If you haven’t set up your account and created a Graph and Dataset yet, you can do so by following this guide here.

API Endpoint [API Docs]

The Leftover Labs API endpoint for bulk posting sensor data is:

https://api.leftoverlabs.com/graph/data/bulk-create/

Required Headers

Ensure you include the following headers in your POST request:

  • Content-Type: application/json
  • api-key: YOUR_API_CREDENTIALS_KEY
  • dataset-id: YOUR_DATASET_ID

Request Payload

Your request should include the following JSON payload with multiple data points:

        
{
    "data_points": [
        {
            "sensor_value": "10",
            "timestamp": "2023-07-15T14:30:00Z"
        },
        {
            "sensor_value": "15",
            "timestamp": "2023-07-15T14:35:00Z"
        },
        {
            "sensor_value": "20",
            "timestamp": "2023-07-15T14:40:00Z"
        }
    ]
}
        
    

You must include a timestamp for each data point to specify when the data was collected. This is required to ensure accurate time tracking, especially when uploading historical data.

Timestamp Format

The timestamp should be in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ

Format Components:

  • YYYY: Four-digit year
  • MM: Two-digit month
  • DD: Two-digit day
  • T: Separator between date and time
  • HH: Two-digit hour (24-hour format)
  • MM: Two-digit minute
  • SS: Two-digit second
  • Z: UTC timezone indicator

Example Implementation

Here is an example Python code snippet to help you bulk POST your sensor data:

        
import requests
import json

# Your API credentials and dataset ID
API_KEY = "your_api_key_here"
DATASET_ID = "your_dataset_id_here"

# API endpoint
url = "https://api.leftoverlabs.com/graph/data/bulk-create/"

# Headers
headers = {
    "Content-Type": "application/json",
    "api-key": API_KEY,
    "dataset-id": DATASET_ID
}

# Data to send (multiple data points)
data = {
    "data_points": [
        {"sensor_value": "10", "timestamp": "2023-07-15T14:30:00Z"},
        {"sensor_value": "15", "timestamp": "2023-07-15T14:35:00Z"},
        {"sensor_value": "20", "timestamp": "2023-07-15T14:40:00Z"},
        {"sensor_value": "25", "timestamp": "2023-07-15T14:45:00Z"},
        {"sensor_value": "30", "timestamp": "2023-07-15T14:50:00Z"}
    ]
}

# Make the POST request
response = requests.post(url, headers=headers, data=json.dumps(data))

# Check the response
if response.status_code == 200:
    print("Success! Data points registered.")
    print(response.json())
else:
    print(f"Error: {response.status_code}")
    print(response.text)
        
    

When to Use Bulk API

The bulk API is particularly useful in scenarios where:

  • The monitoring service has been temporarily down
  • You've collected multiple data points offline and need to upload them all at once
  • You want to reduce the number of API calls for better performance

Unlike the regular API endpoint which has a rate limit of one request every 5 seconds per dataset, the bulk API allows you to send multiple data points in a single request, making it more efficient for batch uploads.

Co funded by the EU

Funded by the European Union. Views and opinions expressed are however those of the author(s) only and do not necessarily reflect those of the European Union or the European Education and Culture Executive Agency (EACEA). Neither the European Union nor EACEA can be held responsible for them.

Project Number: 2023-3-CY02-KA210-YOU-000173087