# Transformation

Transformation节点用于更改入站的消息字段例如：发起者、消息类型、playload和元数据。

* 变更发起者
* 脚本
* 邮件转换

### 变更发起者

![](/files/-MK4Ca0iEC80GAfPRaEx)

Thingsboard中的所有入站消息都有“发起者”字段该字段标识提交消息的实体可以是Device, Asset, Customer, Tenant等。

当提交的消息是来自另一个实体的消息处理的情况下使用此节点；例如：设备上传遥测并且遥测应复制到更高级别的资产或客户中。

在这种情况下管理员必须在**Save Timeseries**节点之前添加此节点。

发起者可以更改为:

* 发起人的客户
* 发起人的租户
* 关系查询所标识的相关实体

在’Relations query’管理员可以选择所需的**Direction**和**relation depth level**还可以使用必需的关系类型和实体类型配置**Relation filters**。

![](/files/-MK4Cc60o6mSj9QIVBho)

如果找到多个相关实体则**仅第一个实体当作**新的发起者其他实体则被丢弃。

如果未找到相关实体/客户/租户则使用**Failure**链否则**Success**链。

出站邮件将具有新的发起者ID。

### 脚本

![](/files/-MK4CeKEkCSQlr_p1_-3)

使用已配置的JavaScript函数更改消息payload、Metadata或消息类型。

JavaScript函数接收3个输入参数：

* `msg` - 消息payload
* `metadata` - 消息metadata
* `msgType` - 消息类型

脚本应返回以下结构

```
{   
    msg: new payload,
    metadata: new metadata,
    msgType: new msgType 
}
```

![](/files/-MK4ChrA2vrt96MvyelL)

结果对象中的所有字段都是可选的如果未指定则将从原始消息中获取

来自此节点的出站消息将是使用已配置的JavaScript函数构造的新消息。

\
**Example**

节点收到带有**payload**的消息:

```
{
    "temperature": 22.4,
    "humidity": 78
}
```

Original **Metadata**:

```
{ "sensorType" : "temperature" }
```

原始**Message Type** - POST\_TELEMETRY\_REQUEST<br>

应该执行以下修改:

* 将消息类型更改为’CUSTOM\_UPDATE’
* 将其他属&#x6027;***version***&#x6DFB;加到payload中值&#x4E3A;***v1.1***
* 将元数据中的\_**sensorType**\_属性值更改&#x4E3A;***roomTemp***

以下转换函数将执行所有必要的修改:

```
var newType = "CUSTOM_UPDATE";
msg.version = "v1.1";
metadata.sensorType = "roomTemp"
return {msg: msg, metadata: metadata, msgType: newType};
```

在下一面教程中你可以看到使用该节点的真实示例：

* 转换入站遥测
* RPC调用回复

### 邮件转换

![](/files/-MK4ClcHY7zv_baOMpwU)

通过使用从消息元数据派生的值填充电子邮件字段后将消息转换为电子邮件并设置’SEND\_EMAIL’为输出消息类型后使用**发送邮件**节点发送。

可以将所有电子邮件字段配置为元数据中的值。

![](/files/-MK4CodP7VyO4XofY7Rr)

例如入站消息在元数据中具有**deviceName**字段并且电子邮件正文应包含其值。

在这种情况下可以像下面的示例一样引用**deviceName**值`${deviceName}`:

```
 Device ${deviceName} has high temperature
```

如果入站消息元数据包含引用存储在数据库中的**attachments**字段可以做为电子邮件的附件使用。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.yuncitys.com/shu-ju-chu-li-gui-ze-yin-qing/gui-ze-lian-gong-neng-jie-dian/transformation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
