ThingsBoard 3.4:如何通过关联获取高层资产属性并触发邮件通知?
在ThingsBoard中处理资产层级关联的两个场景方案
一、设备活跃时获取顶层资产Asset1的属性
你的资产层级是Asset1 → Asset2 → Asset3 → 设备(均为Contains关联关系),当设备活跃时,可通过规则链的实体关系遍历+属性获取实现需求,具体有两种方式:
方式1:可视化规则链节点组合
- 触发节点:选择
Device Active或Telemetry Upload(根据设备活跃的定义选择),接收设备活跃事件。 - 遍历父实体:
- 新增
Entity Relation节点,设置:- 关系方向:
FROM - 关系类型:
Contains - 最大结果数:1
该节点会获取设备的父实体Asset3。
- 关系方向:
- 重复上述步骤,再添加两个
Entity Relation节点,依次获取Asset3的父Asset2、Asset2的父Asset1。
- 新增
- 获取Asset1属性:新增
Get Entity Attributes节点,选择Asset1的实体ID(从上游节点传入),指定要获取的属性范围(如SERVER_SCOPE)和属性键,即可得到Asset1的属性。
方式2:脚本节点实现遍历与属性获取
在规则链中添加Script节点,使用ThingsBoard内置的tbApi工具完成遍历和属性查询,示例代码:
// 从设备向上遍历到Asset3 const asset3 = await tbApi.getEntityRelations(entityId, "FROM", "Contains", null, 1); if (asset3.length === 0) return {msg, metadata, msgType}; // 从Asset3遍历到Asset2 const asset2 = await tbApi.getEntityRelations(asset3[0].to.id, "FROM", "Contains", null, 1); if (asset2.length === 0) return {msg, metadata, msgType}; // 从Asset2遍历到Asset1 const asset1 = await tbApi.getEntityRelations(asset2[0].to.id, "FROM", "Contains", null, 1); if (asset1.length === 0) return {msg, metadata, msgType}; // 获取Asset1的服务器端属性(可替换为CLIENT_SCOPE/SHARED_SCOPE) const asset1Attrs = await tbApi.getEntityAttributes(asset1[0].to.id, "SERVER_SCOPE", ["attr_key1", "attr_key2"]); // 将属性存入消息元数据,供后续节点使用 metadata.put("asset1_attributes", JSON.stringify(asset1Attrs)); return {msg, metadata, msgType};
二、设备数值超阈值时给Asset1、Asset2发送邮件通知
需先确保:
- Asset1和Asset2已在
SERVER_SCOPE属性中配置email字段; - ThingsBoard已在
Settings → Mail Server中配置好邮件发送服务器。
具体实现步骤:
1. 阈值判断
新增Script Filter节点,过滤出数值超阈值的设备消息,示例代码:
// 假设监控的字段是temperature,阈值为80 const currentValue = msg.temperature; return currentValue > 80; // 返回true则进入后续节点
2. 遍历资产并获取邮箱
添加Script节点,遍历获取Asset1和Asset2的邮箱属性,并发送邮件,示例代码:
// 遍历到Asset3 const asset3 = await tbApi.getEntityRelations(entityId, "FROM", "Contains", null, 1); if (asset3.length === 0) return {msg, metadata, msgType}; // 遍历到Asset2并获取邮箱 const asset2Id = asset3[0].to.id; const asset2Attrs = await tbApi.getEntityAttributes(asset2Id, "SERVER_SCOPE", ["email"]); const asset2Email = asset2Attrs.email; // 遍历到Asset1并获取邮箱 const asset1 = await tbApi.getEntityRelations(asset2Id, "FROM", "Contains", null, 1); if (asset1.length === 0) return {msg, metadata, msgType}; const asset1Attrs = await tbApi.getEntityAttributes(asset1[0].to.id, "SERVER_SCOPE", ["email"]); const asset1Email = asset1Attrs.email; // 收集有效收件人 const recipients = []; if (asset1Email) recipients.push(asset1Email); if (asset2Email) recipients.push(asset2Email); // 发送邮件 if (recipients.length > 0) { await tbApi.sendEmail( recipients.join(","), "设备数值超阈值告警", `设备${entityName}的监测值(${msg.temperature})已超过阈值80,请及时处理。` ); } return {msg, metadata, msgType};
替代方案:可视化节点组合
如果不想写脚本,可按以下节点顺序组合:Telemetry Upload → Script Filter(阈值判断) → Entity Relation(获取Asset3) → Entity Relation(获取Asset2) → Get Entity Attributes(获取Asset2邮箱) → Entity Relation(获取Asset1) → Get Entity Attributes(获取Asset1邮箱) → Send Email(收件人填两个邮箱的逗号分隔值)
内容的提问来源于stack exchange,提问作者Akhil
相关产品推荐
相关产品推荐

