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.

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.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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.clients.time_series_client import TimeSeriesClient

# Import the GetTimeseriesRequest from timeseries.models module
from timeseries.models 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 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

Any questions left?

Ask the community


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