---
title: Use Digital Twins to Contextualize Data
slug: litmusedge/how-to-guides/digital-twins-guide
description: Refer to the following how-to guide for leveraging Digital Twins for energy monitoring. 
docTags: 
createdAt: 2023-09-08T14:49:22.093Z
---

Refer to the following how-to guide for leveraging Digital Twins for energy monitoring.&#x20;

# User Scenario

In this user scenario, the devicehub device will simulate the energy monitoring process across different plant sites. The data generated by the tags reflects real-time energy production, which allows for active analysis of energy efficiency, cost, and consumption patterns.

Digital Twins will model the asset as an energy-monitoring machine located at several plant sites. The instances for the model will be the specific energy-monitoring machines at each site.&#x20;

The power generated in the last hour will be used to calculate values for cost, energy used, and energy rate. The timestamp value will be used to generate values for the attributes of season, start hour, and energy-rate description.&#x20;

We need to add two tags for this. Tag 1, named "L3\_Machine2", will generate random values simulating the variable power of an energy-monitoring machine. Tag 2 in Devicehub collects and stores data from energy monitoring devices. It helps in analyzing energy rates and seasons by simulating timestamp values.

# Step 1: Add a Device

Follow the steps to [Connect a device](docId\:pal6ABPZbrimdU9LvGJ30) and configure the following parameters:

- **Device Type**: Simulator
- **Driver Name**: Generator
- **Enable Alias Topics**: Select the checkbox.&#x20;

# Step 2: Add Tags

After connecting the device, add the following tag. See [Add Tags](docId\:XgWOkQbTPevII7OR82LL0) to learn more.&#x20;

## Tag 1: L3\_Machine2

- **Name**: Select **S - Random value generator**
- **Value Type:** Select **float64**
- **Polling Interval**: Enter **1**
- **Tag Name**: Enter **L3\_Machine2**
- **Min value**: Enter **1**
- **Max value**: Enter **25**

Values for power generated will be sent through the topic **devicehub.alias.P2\_Electrical\_Panel.L3\_Machine2**.&#x20;

## Tag 2

The data collected from Energy Monitoring devices is stored in Devicehub by enabling the [Enable Data Store ](docId\:pal6ABPZbrimdU9LvGJ30)option on the simulator devices. The simulator device will act as a device measuring power values. The tag will simulate these timestamp values from one of the energy monitoring machines, and then the output will be written to the topic **flowToDT.EMD\_L3\_Machine2**.

To process this data, you will need to connect to a third-party database and create a Flow using Flows Manager. This flow will collect the values and determine the season and energy rate based on the date and timestamp.

```javascript
// Determine season and energy rate based on date and timestamp

var arr = msg.payload;
var arrOut = [];

var time = flow.get("tStart");
var tStart = new Date(flow.get("tStart"));
var month = tStart.getMonth();
var hour = tStart.getHours();
flow.set("month", month);
flow.set("hour", hour)

// Determine season
if (month > 5 && 10 > month){
    var season = 'Summer';
} else {
    var season = 'Winter';
}

// Determine energy rate
if (season == 'Summer'){
    if (hour >= 7 && 11 > hour || hour >= 17 && 19 > hour){
        var energyRate = 10.2;
        var energyRateDescription = "Mid-Peak";
    }
    else if (hour >= 11 && 17 > hour){
        var energyRate = 15.1;
        var energyRateDescription = "Off-Peak";
    }
    else {
        var energyRate = 7.4;
        var energyRateDescription = "On-Peak";
    }
}

else {
    if (hour >= 7 && 11 > hour || hour >= 17 && 19 > hour){
        var energyRate = 15.1;
        var energyRateDescription = "On-Peak";
    }
    else if (hour >= 11 && 17 > hour){
        var energyRate = 10.2;
        var energyRateDescription = "Mid-Peak";
    }
    else {
        var energyRate = 7.4;
        var energyRateDescription = "Off-Peak";
    }
}

for (let device = 0; device < arr.length; device++){
    
    obj = {}
    
    // Fields for SQL insert
    obj.time = time;
    obj.plant = arr[device].plant;
    obj.line = arr[device].line_tag;
    obj.asset = arr[device].name_tag;
    obj.shift = global.get("shift");
    obj.energyUsed = parseFloat((arr[device].integral).toFixed(2));
    obj.energyRate = energyRate;
    obj.energyRateDescription = energyRateDescription;
    obj.season = season;
    obj.cost = parseFloat((obj.energyUsed * energyRate).toFixed(2));

    arrOut.push(obj)
}

msg.payload = arrOut;

return msg;
```

# Sample Output Payload from Instance

This user scenario will be based on the following sample output payload from one instance.&#x20;

```json
{
  "time": "2023-04-13T17:00:00.000Z",
  "plant": "Toronto",
  "line": "Line 1",
  "asset": "Machine 2",
  "shift": 2,
  "energyUsed": 2.51,
  "energyRate": 15.1,
  "energyRateDescription": "On-Peak",
  "season": "Winter",
  "cost": 37.9,
  "timestamp": 1681408544466,
  "startHour": "2023-04-13T17:00:00.000Z"
}
```

# Step 3: Create Digital Twins Model

You will first need to create the model that will store the instances of energy monitoring machines.&#x20;

**To create the model:**

1. Navigate to **Digital Twins**.&#x20;
   The *Models&#x20;*&#x70;ane appears.&#x20;
2. From the *Models&#x20;*&#x70;ane, click the **Create a model&#x20;**&#x74;ile.&#x20;
   The *Create a Model* dialog box appears.&#x20;
   ::Image[]{src="https://api.archbee.com/api/optimize/SSUUxKZUk9bFTEPNn_6Zo/RgVgMbkVf98fnibVlKx5M_createmodel.png" size="80" width="1266" height="482" caption="The Create a Model tile" position="center" showCaption="true"}
3. Click the **Type** drop-down list and select **ASSET**.
4. Enter the name of the model in the **Name** field (for example, **Energy\_Monitoring\_Model**).
5. (Optional) Enter a short description of the model in the **Description** field.
6. Click **Add**.****
   The new model appears in the *Models&#x20;*&#x70;ane.

After creating the model, you can [configure it](docId\:zPaDX1lMmE0F44V7r6v6Q) with the following parameters.&#x20;

## Model Static Attributes

The static attributes will need to be customized for each instance.&#x20;

- Asset
- Line
- Model
- Manufacturer
- Location
- Plant

## Transformations

The transformations are based on the sample payload above. &#x20;

### energyRateDescription

```json
{
  "value": "$energyRateDescription",
  "timestamp": "$timestamp"
}
```

### energyRate

```json
{
  "value": "$energyRate",
  "timestamp": "$timestamp"
}
```

### energyUsed

```json
{
  "value": "$energyUsed",
  "timestamp": "$timestamp"
}
```

### shift

```json
{
  "value": "$shift",
  "timestamp": "$timestamp"
}
```

### startHour

```json
{
  "value": "$startHour",
  "timestamp": "$timestamp"
}
```

### season

```json
{
  "value": "$season",
  "timestamp": "$timestamp"
}
```

### cost

```json
{
  "value": "$cost",
  "timestamp": "$timestamp"
}
```

The list of transformations would display as the following.&#x20;

![](https://api.archbee.com/api/optimize/SSUUxKZUk9bFTEPNn_6Zo/-Hb6EBaBm91j0EM2rKQ-r_image.png "List of transformations")

:::hint{type="info"}
**Tip:&#x20;**&#x54;o access nested metadata keys, you should include a period after the initial dollar sign. For example:

- `$.metadata` will extract the entire metadata object.
- `$.metadata.cost` will extract the *cost&#x20;*&#x6B;ey in the metadata object.
:::

## Model Dynamic Attributes

The dynamic attributes will use the appropriate schemaID (transformation) to generate values.&#x20;

| **Name**              | **Unit (optional)** | **DataType (optional)** | **SchemaID (optional)** |
| --------------------- | ------------------- | ----------------------- | ----------------------- |
| cost                  | USD                 |                         | cost                    |
| energyRateDescription |                     |                         | energyRateDescription   |
| energyRate            | USD/kWh             |                         | energyRate              |
| energyUsed            |                     |                         | energyUsed              |
| shift                 |                     |                         | shift                   |
| startHour             |                     |                         | startHour               |
| season                |                     |                         | season                  |
| power                 | kW                  |                         |                         |

## Hierarchy

The output for the model will be based on the hierarchy, which would categorize the dynamic and static attributes into **telemetry&#x20;**&#x6F;r **properties**. It would look like the following.&#x20;

![](https://api.archbee.com/api/optimize/SSUUxKZUk9bFTEPNn_6Zo/MlNIsMtER3wa6O54LfDTK_image.png)

# Step 4: Create Instances

Once the model is configured, you will need to [Add an instance](docId\:jzOth4T0hCrTRauUSdRyK) for each energy monitoring device at each site.&#x20;

One of the instances is described below based on the topics for the data in the user scenario description.&#x20;

## Instance Static Attributes

You will customize the model static attributes to the specific instance.&#x20;

| **Attribute Name** | **Attribute Value** |
| ------------------ | ------------------- |
| Asset              | Machine 2           |
| Line               | Line 1              |
| Model              | model\_112233       |
| Manufacturer       | Siemens             |
| Location           | Canada              |
| Plant              | Toronto             |

## Instance Dynamic Attributes

You will configure the appropriate topics in the user scenario description to the dynamic attributes configured in the model.&#x20;

| **Name**              | **Unit** | **DataType** | **Topic**                                          | **SchemaID (optional)** |
| --------------------- | -------- | ------------ | -------------------------------------------------- | ----------------------- |
| power                 | kW       |              | devicehub.alias.P2\_Electrical\_Panel.L3\_Machine2 |                         |
| cost                  | USD      |              | flowToDT.EMD\_L3\_Machine2                         | cost                    |
| energyRateDescription |          |              | flowToDT.EMD\_L3\_Machine2                         | energyRateDescription   |
| energyRate            | USD/kWh  |              | flowToDT.EMD\_L3\_Machine2                         | energyRate              |
| energyUsed            |          |              | flowToDT.EMD\_L3\_Machine2                         | energyUsed              |
| shift                 |          |              | flowToDT.EMD\_L3\_Machine2                         | shift                   |
| startHour             |          |              | flowToDT.EMD\_L3\_Machine2                         | startHour               |
| season                |          |              | flowToDT.EMD\_L3\_Machine2                         | season                  |

