Understand digital twins and their twin graph

In an Azure Digital Twins solution, the entities in your environment are represented by digital twins. A digital twin is an instance of one of your custom-defined models. It can be connected to other digital twins via relationships to form a twin graph: this twin graph is the representation of your entire environment.

Tip

"Azure Digital Twins" refers to this Azure service as a whole. "Digital twin(s)" or just "twin(s)" refers to individual twin nodes inside your instance of the service.

Digital twins

Before you can create a digital twin in your Azure Digital Twins instance, you need to have a model uploaded to the service. A model describes the set of properties, telemetry messages, and relationships that a particular twin can have, among other things. For the types of information that are defined in a model, see Custom models.

After creating and uploading a model, your client app can create an instance of the type. This instance is a digital twin. For example, after creating a model of Floor, you may create one or several digital twins that use this type (like a Floor-type twin called GroundFloor, another called Floor2, and so on).

Note

Digital twins in Azure Digital Twins are different from device twins in IoT Hub. IoT Hub device twins often focus on describing the aspects and capabilities of a device itself, while twins in Azure Digital Twins are more conceptual representations that can store user-defined insights about a device or many related devices.

IoT Hub device twins can be connected to Azure Digital Twins as part of an end-to-end solution that represents devices across services.

Relationships: a graph of digital twins

Twins are connected into a twin graph by their relationships. The relationships that a twin can have are defined as part of its model.

For example, the model Floor might define a contains relationship that targets twins of type Room. With this definition, Azure Digital Twins will allow you to create contains relationships from any Floor twin to any Room twin (including twins that are of Room subtypes).

The result of this process is a set of nodes (the digital twins) connected via edges (their relationships) in a graph.

Visualization

Azure Digital Twins Explorer is a visual tool for exploring the data in your Azure Digital Twins graph. You can use the explorer to view, query, and edit your models, twins, and relationships.

To read about the Azure Digital Twins Explorer tool, see Azure Digital Twins Explorer. For detailed steps on how to use its features, see Use Azure Digital Twins Explorer.

Here's what the visualization looks like:

Screenshot of Azure Digital Twins Explorer showing sample models and twins.

Create with the APIs

This section shows what it looks like to create digital twins and relationships from a client application. It contains .NET code examples that use the DigitalTwins APIs, to provide more context on what goes on inside each of these concepts.

Create digital twins

Below is a snippet of client code that uses the DigitalTwins APIs to instantiate a twin of type Room with a twinId that's defined during the instantiation.

You can initialize the properties of a twin when it's created, or set them later. To create a twin with initialized properties, create a JSON document that provides the necessary initialization values.

// Define a custom model type for the twin to be created

internal class CustomDigitalTwin
{
    [JsonPropertyName(DigitalTwinsJsonPropertyNames.DigitalTwinId)]
    public string Id { get; set; }

    [JsonPropertyName(DigitalTwinsJsonPropertyNames.DigitalTwinETag)]
    public string ETag { get; set; }

    [JsonPropertyName(DigitalTwinsJsonPropertyNames.DigitalTwinMetadata)]
    public MyCustomDigitalTwinMetadata Metadata { get; set; } = new MyCustomDigitalTwinMetadata();

    [JsonPropertyName("temperature")]
    public double Temperature { get; set; }

    [JsonPropertyName("humidity")]
    public double Humidity{ get; set; }
}

internal class MyCustomDigitalTwinMetadata
{
    [JsonPropertyName(DigitalTwinsJsonPropertyNames.MetadataModel)]
    public string ModelId { get; set; }

    [JsonPropertyName("temperature")]
    public DigitalTwinPropertyMetadata Temperature { get; set; }

    [JsonPropertyName("humidity")]
    public DigitalTwinPropertyMetadata Humidity { get; set; }
}

public class TwinOperationsSamples
{
    public async Task CreateTwinAsync(DigitalTwinsClient client)
    {
        // Initialize the twin properties
        var myTwin = new CustomDigitalTwin
        {
            Metadata = { ModelId = "dtmi:example:Room;1" },
            Temperature = 25.0,
            Humidity = 50.0,
        };

        // Create the twin
        const string twinId = "<twin-ID>";
        Response<CustomDigitalTwin> response = await client.CreateOrReplaceDigitalTwinAsync(twinId, myTwin);
        Console.WriteLine($"Temperature last updated on {response.Value.Metadata.Temperature.LastUpdatedOn}");

You can also use a helper class called BasicDigitalTwin to store property fields in a "twin" object more directly, as an alternative to using a dictionary. For more information about the helper class and examples of its use, see the Create a digital twin section of How-to: Manage digital twins.

Note

While twin properties are treated as optional and thus don't have to be initialized, any components on the twin do need to be set when the twin is created. They can be empty objects, but the components themselves must exist.

Create relationships

Here's some example client code that uses the DigitalTwins APIs to build a relationship from one digital twin (the "source" twin) to another digital twin (the "target" twin).

var rel = new BasicRelationship
{
    TargetId = "myTargetTwin",
    Name = "contains", // a relationship with this name must be defined in the model
    // Initialize properties
    Properties =
    {
        { "active", true },
    },
};
await client.CreateOrReplaceRelationshipAsync("mySourceTwin", "rel001", rel);

JSON representations of graph elements

Digital twin data and relationship data are both stored in JSON format, which means that when you query the twin graph in your Azure Digital Twins instance, the result will be a JSON representation of digital twins and relationships you've created.

Digital twin JSON format

When represented as a JSON object, a digital twin will display the following fields:

Field name Description
$dtId A user-provided string representing the ID of the digital twin
$etag Standard HTTP field assigned by the web server
$conformance An enum containing the conformance status of this digital twin (conformant, non-conformant, unknown)
<property-name> The value of a property in JSON (string, number type, or object)
$relationships The URL of the path to the relationships collection. This field is absent if the digital twin has no outgoing relationship edges.
$metadata.$model [Optional] The ID of the model interface that characterizes this digital twin
$metadata.<property-name>.desiredValue [Only for writable properties] The desired value of the specified property
$metadata.<property-name>.desiredVersion [Only for writable properties] The version of the desired value
$metadata.<property-name>.ackVersion The version acknowledged by the device app implementing the digital twin
$metadata.<property-name>.ackCode [Only for writable properties] The ack code returned by the device app implementing the digital twin
$metadata.<property-name>.ackDescription [Only for writable properties] The ack description returned by the device app implementing the digital twin
<component-name> A JSON object containing the component's property values and metadata, similar to those of the root object. This object exists even if the component has no properties.
<component-name>.<property-name> The value of the component's property in JSON (string, number type, or object)
<component-name>.$metadata The metadata information for the component, similar to the root-level $metadata

Here's an example of a digital twin formatted as a JSON object:

{
  "$dtId": "Cafe",
  "$etag": "W/\"e59ce8f5-03c0-4356-aea9-249ecbdc07f9\"",
  "Temperature": 72,
  "Location": {
    "x": 101,
    "y": 33
  },
  "component": {
    "TableOccupancy": 1,
    "$metadata": {
      "TableOccupancy": {
        "desiredValue": 1,
        "desiredVersion": 3,
        "ackVersion": 2,
        "ackCode": 200,
        "ackDescription": "OK"
      }
    }
  },
  "$metadata": {
    "$model": "dtmi:com:contoso:Room;1",
    "Temperature": {
      "desiredValue": 72,
      "desiredVersion": 5,
      "ackVersion": 4,
      "ackCode": 200,
      "ackDescription": "OK"
    },
    "Location": {
      "desiredValue": {
        "x": 101,
        "y": 33,
      },
      "desiredVersion": 8,
      "ackVersion": 8,
      "ackCode": 200,
      "ackDescription": "OK"
    }
  }
}

Relationship JSON format

When represented as a JSON object, a relationship from a digital twin will display the following fields:

Field name Description
$relationshipId A user-provided string representing the ID of this relationship. This string is unique in the context of the source digital twin, which also means that sourceId + relationshipId is unique in the context of the Azure Digital Twins instance.
$etag Standard HTTP field assigned by the web server
$sourceId The ID of the source digital twin
$targetId The ID of the target digital twin
$relationshipName The name of the relationship
<property-name> [Optional] The value of a property of this relationship, in JSON (string, number type, or object)

Here's an example of a relationship formatted as a JSON object:

{
  "$relationshipId": "relationship-01",
  "$etag": "W/\"506e8391-2b21-4ac9-bca3-53e6620f6a90\"",
  "$sourceId": "GroundFloor",
  "$targetId": "Cafe",
  "$relationshipName": "contains",
  "startDate": "2020-02-04"
}

Next steps

See how to manage graph elements with Azure Digital Twin APIs:

Or, learn about querying the Azure Digital Twins twin graph for information: