☁️ Push measurement data to alitiq-Engine API with python ☁️

Updated . Posted . Visible to the public.

Write your measurement to the alitiq-Engine API is very simple following this guide. You just need your data as a pandas.DataFrame with a DateTimeIndex. Besides that you need to check out the following information about your data:

  • timezone: Specify the timezone of your data. Common options include:
    • Europe/Berlin (includes daylight saving time)
    • Etc/GMT-1 (standard time, no daylight saving)
    • UTC (default)
  • window_boundary: Define where the timestamp is located within the time window. Options are:
    • begin
    • center
    • end (default)
  • power_measure: Specify the SI unit of your data. Available options are:
    • W
    • kW
    • MW (default)
    • MWh
    • kWh
    • Wh
  • interval_in_minutes: Define the time interval between measurements. This is especially important if your data is in energy units rather than power.

Besides that information, you need your individual x-api-key and the id_location of the entity you want to add measurements for given by alitiq.

import pandas as pd
import requests

# Your data (example with dummy values)
data = pd.DataFrame(
    [230, 235, 240.0, 245.6, 256.0],
    index=[
        pd.Timestamp('2022-03-06T09:45:00.000'),
        pd.Timestamp('2022-03-06T10:00:00.000'),
        pd.Timestamp('2022-03-06T10:15:00.000'),
        pd.Timestamp('2022-03-06T10:30:00.000'),
        pd.Timestamp('2022-03-06T10:45:00.000')
    ],
    columns=['power']
)

# Setup metadata for the data
x_api_key = 'your_api_token'
id_location = 'your_location_id'
interval_in_minutes = 15
timezone = "Europe/Berlin"
window_boundary = 'end'
power_measure = 'kW'

# Add metadata to the DataFrame
data["id_location"] = id_location
data["interval_in_minutes"] = interval_in_minutes
data["timezone"] = timezone
data["window_boundary"] = window_boundary
data["power_measure"] = power_measure
data["dt"] = data.index

# Create a JSON-serializable payload
payload = [row.to_dict() for idx, row in data.iterrows()]

# Chunk the payload if necessary
chunk_size = 10000
chunked_payload = [payload[i:i + chunk_size] for i in range(0, len(payload), chunk_size)]

# Define headers and base URL
headers = {
    "Content-Type": "application/json",
    "x-api-key": x_api_key,
}
base_url = "https://engine.alitiq.com/measurement/add"

# Push data to the API
for chunk in chunked_payload:
    response = requests.post(base_url, json=chunk, headers=headers)
    print(response.text)

Notes:

  • Chunking: For large time series, the data will be chunked. Ensure that each chunk does not exceed 10,000 samples.
  • Upsert Support: You can write data as much as you want for the same time. The value will be updated, the old value will be gone.
  • Inspect Measurements: When you inspect measurements via the API, you will see the data as it is stored in our database. By default, data is stored in MW, with timestamps in UTC and located at the end of the window.

For more detailed information, please refer to the official documentation (coming soon).

Last edit
Daniel
Keywords
python, pandas, Engine, API, push, post, measurements
Posted by Daniel to alitiq Knowledge (2024-08-23 09:15)