# Templates beta

NOTE

Templates is currently in beta testing and only available to a few customers via our Early Adopter program. To join an Early Adopter program, contact your Cognite representative.

A Template group holds a collection of Templates. A Template defines the structure of the data objects that will be using the template. You can, for example, create templates for pumps and valves that you use in your use-case. In the template, you can further define that pumps should have inlet pressure as a property referencing a time series. An Instance of a template is the actual data object, while the template describes the structure of it. The instance can, for example, be your pump_01 with the time series timeseries_inletpressure_240 as the inlet pressure.

In this article:

# About templates

Use templates to structure and organize data to your needs. You can assign fields to the template that you then assign values, or reference other data such as time series or assets. Build your view of the data you have available in Cognite Data Fusion and simplify the applications and solutions you are working on by accessing data through templates. Templates remove the gap between the application’s data model and the preexisting structure of the relevant data. It is easy to scale your solution across more data without having to change or redeploy your application code or configuration. Instead, you can work with your template in Cognite Data Fusion, use the tooling available there to scale to more data, with governance.

# Create a template group

To create a template, you first need to create a template group to store it in.

This example request creates a template group intended for a pump dashboard:

from cognite.client import CogniteClient
from cognite.client.data_classes import TemplateGroup
c = CogniteClient()
template_group_1 = TemplateGroup(
    "Valve dashboard",
    "Template group containing templates for a pump dashboard"
)
c.templates.groups.create(template_group_1)
1
2
3
4
5
6
7
8

# Create a template for pumps

You can use templates to describe your expectation for the structure of data objects.

This example shows how to create a template group with a pump template to be used by a dashboard. pumps will have two properties, one for the time series holding data points for the inlet pressure of the pump, and another for the pump generation. Also, there is a built-in property for the external_id of each pump.

from cognite.client import CogniteClient
from cognite.client.data_classes import TemplateGroup,TemplateGroupVersion
c = CogniteClient()
template_group = TemplateGroup(
    "Pump-Dashboard",
    "Template group for a pump dashboard"
)
c.templates.groups.create(template_group)
schema = '''
    "The schema of pumps as seen by the dashboard"
    type Pump @template {
        "Inlet pressure"
        inlet_pressure: TimeSeries,
        "Generation"
        gen: String
    }
'''
template_group_version = TemplateGroupVersion(schema)
c.templates.versions.upsert(template_group.external_id, template_group_version)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# Create instances of a pump template

When you have an object you need in your solution, you can create a data record of it as an instance.

This example shows how to create a pump instance using the template created in the previous example.

from cognite.client import CogniteClient
from cognite.client.data_classes import TemplateGroup,TemplateGroupVersion, TemplateInstance, ConstantResolver
c = CogniteClient()
pump_1 = TemplateInstance(
    external_id="asset_1",
    template_name="Pump",
    field_resolvers={
        "inlet_pressure": ConstantResolver("timeseries_inletpressure_240"),
        "gen": ConstantResolver("Hex")
        }
    )
pump_2 = TemplateInstance(
    external_id="asset_2",
    template_name="Pump",
    field_resolvers={
        "inlet_pressure": ConstantResolver("timeseries_inletpressure_249"),
        "gen": ConstantResolver("hex")
        }
    )
template_group_list = c.templates.versions.list("Pump-Dashboard")
latest_version = template_group_list[0].version
c.templates.instances.upsert("Pump-Dashboard", latest_version, [pump_1, pump_2])
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

# Retrieve pump instances for a dashboard

To list pump instances in your dashboard or solution, run a graphQL query.

from cognite.client import CogniteClient
c = CogniteClient()
query = '''
{
    pumpQuery {
        items {
            _externalId
            inlet_pressure {
                externalId
                datapoints(limit: 100) {
                    timestamp
                    value
                }
            }
            gen
        }
    }
}
'''
result = c.templates.graphql_query("Pump-Dashboard", 1, query)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Last Updated: 6/24/2021, 1:24:59 PM