Skip to content

IoT Time Series Client for Python

Introduction

The IoT Time Series Service allows you to interact with time series data related to assets. Refer to IoT Time Series for more information about the service.

Further implementation of the IOT TimeSeries SDK library has been shown in a sample project that you can download and test in local or on Insights Hub application. Please refer to this repository: industrial-iot-python-sdk-examples

Hint

In the IoT context, assets are referred to as entity and aspects as propertyset.
Placeholders in the following samples are indicated by angular brackets < >.

Time Series Operations

Client Name: TimeSeriesClient

Read Time Series Data

This code sample demonstrates how to invoke the IoT Time Series API for achieving the following behavior:

  • Read time series data for a single aspect of an asset.
  • Return data for a specified time range.
  • Return the latest value if no range is provided.
  • Return time series data for selected fields and limit.
# Import the RestClientConfig and UserToken from mindsphere_core module
from mindsphere_core import RestClientConfig
from mindsphere_core import UserToken

# Import the MindsphereError from mindsphere_core.exceptions module
from mindsphere_core.exceptions import MindsphereError

# Import the TimeseriesClient from timeseries module
from timeseries import TimeSeriesClient

# Import the GetTimeseriesRequest from timeseries.models module
from timeseries import GetTimeseriesRequest

# Create RestClientConfig and UserToken objects
config = RestClientConfig(proxy_host = "<proxy_host>", proxy_port = <proxy_port>)
credentials = UserToken(authorization = "<bearer_token>")

# Create the TimeSeriesClient object using the RestClientConfig and UserToken objects
timeseriesClient = TimeSeriesClient(rest_client_config = config, mindsphere_credentials = credentials)

try:
    # Create the request object
    request = GetTimeseriesRequest(
        _from="<start_time>",
        to="<end_time>",
        entity="<entity>",
        propertysetname="<property_set_name>"
    )

    # Initiate Get Timeseries API call
    timeseries = timeseriesClient.get_timeseries(request)

except MindsphereError as err:
    # Exception Handling

Read Latest Time Series Data

Read the latest uploaded value of time series data for a single aspect of an asset.

# Create the TimeSeriesClient object as shown above

try:
    # Create the request object
    request = GetTimeseriesRequest(
        entity="<entity>",
        propertysetname="<property_set_name>"
    )

    # Initiate Get Timeseries API call
    timeseries = timeseriesClient.get_timeseries(request)

except MindsphereError as err:
    # Exception Handling

Write Time Series Data

Write time series data for a specific aspect of an asset. This overwrites existing time series data.

Note

The maximum size of a write payload is 1 MB.

# Create the TimeSeriesClient object as shown above

try:
    time_series_data = Timeseries()
    time_series_data.time = "<time_value>"
    time_series_data.fields = {
        "<aspect_variable_1>": <value_1>,
        "<aspect_variable_2>": <value_2>,
        "<aspect_variable_3>": <value_3>,
        "<aspect_variable_4>": <value_4>
    }

    # Create the request object
    request = PutTimeseriesRequest(
        timeseries=[time_series_data],
        entity="<entityId>",
        propertysetname="<property_set_name>"
    )

    # Initiate Put Timeseries API call
    timeseriesClient.put_timeseries(request)

except MindsphereError as err:
    # Exception Handling

Delete Time Series Data

Delete time series data from an aspect of an asset within a given time range. This also deletes data for all variables within an aspect.

Note

Only data of up to 6 months can be deleted at once.

# Create the TimeSeriesClient object as shown above

try:
    # Create the request object
    request = DeleteTimeseriesRequest(
        _from="<start_time>",
        to="<end_time>",
        entity="<entity_id>",
        propertysetname="<property_set_name>"
    )

    # Initiate Delete Timeseries API call
    timeseriesClient.delete_timeseries(request)

except MindsphereError as err:
    # Exception Handling

Last update: February 23, 2024

Except where otherwise noted, content on this site is licensed under the Development License Agreement.