Energybox
Last updated
Last updated
// Decode an uplink message from a buffer
// payload - array of bytes
// metadata - key/value object
/** Decoder **/
// decode payload to string
var payloadStr = decodeToString(payload);
// decode payload to JSON
var data = decodeToJson(payload);
// use assetName and assetType instead of deviceName and deviceType
// to automatically create assets instead of devices.
// var assetName = 'Asset A';
// var assetType = 'building';
// Result object with device/asset attributes/telemetry data
var result = {
// Use deviceName and deviceType or assetName and assetType, but not both.
deviceName: data.deviceName,
deviceType: "Hantec Energybox",
attributes: {},
telemetry: data.telemetry
};
/** Helper functions **/
function decodeToString(payload) {
return String.fromCharCode.apply(String, payload);
}
function decodeToJson(payload) {
// covert payload to string.
var str = decodeToString(payload);
// parse string to JSON
var data = JSON.parse(str);
return data;
}
return result;
{
"ruleChain": {
"additionalInfo": {
"description": ""
},
"name": "Decoder Hantec Energybox",
"type": "CORE",
"firstRuleNodeId": null,
"root": false,
"debugMode": false,
"configuration": null,
"externalId": null
},
"metadata": {
"firstNodeIndex": 6,
"nodes": [
{
"additionalInfo": {
"layoutX": 1219,
"layoutY": 302
},
"type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode",
"name": "Save Timeseries",
"debugMode": false,
"configuration": {
"defaultTTL": 0
},
"externalId": null
},
{
"additionalInfo": {
"layoutX": 923,
"layoutY": 206
},
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode",
"name": "Save Client Attributes",
"debugMode": false,
"configuration": {
"scope": "CLIENT_SCOPE"
},
"externalId": null
},
{
"additionalInfo": {
"layoutX": 494,
"layoutY": 309
},
"type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode",
"name": "Message Type Switch",
"debugMode": false,
"configuration": {
"version": 0
},
"externalId": null
},
{
"additionalInfo": {
"layoutX": 935,
"layoutY": 401
},
"type": "org.thingsboard.rule.engine.action.TbLogNode",
"name": "Log RPC from Device",
"debugMode": false,
"configuration": {
"scriptLang": "TBEL",
"jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);",
"tbelScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);"
},
"externalId": null
},
{
"additionalInfo": {
"layoutX": 934,
"layoutY": 462
},
"type": "org.thingsboard.rule.engine.action.TbLogNode",
"name": "Log Other",
"debugMode": false,
"configuration": {
"scriptLang": "TBEL",
"jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);",
"tbelScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);"
},
"externalId": null
},
{
"additionalInfo": {
"layoutX": 933,
"layoutY": 525
},
"type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode",
"name": "RPC Call Request",
"debugMode": false,
"configuration": {
"timeoutInSeconds": 60
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 209,
"layoutY": 307
},
"type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode",
"name": "Device Profile Node",
"debugMode": false,
"configuration": {
"persistAlarmRulesState": false,
"fetchAlarmRulesStateOnStart": false
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 939,
"layoutY": 303
},
"type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode",
"name": "decoder",
"debugMode": true,
"configuration": {
"scriptLang": "JS",
"jsScript": "\r\nvar power = msg[\"Power[W]\"];\r\nvar heatMeterEnergy = msg[\"PowerUsage[kwh]\"];\r\nvar flowTemperature = msg[\"Tin[C]\"];\r\nvar returnFlowTemperature = msg[\"Tout[C]\"];\r\nvar flowRate = msg[\"MassFlowRate[kg/s]\"];\r\n\r\n\r\nvar msg = {power: power, heatMeterEnergy: heatMeterEnergy, flowTemperature: flowTemperature, returnFlowTemperature: returnFlowTemperature, flowRate: flowRate};\r\n\r\n\r\nreturn {msg: msg, metadata: metadata, msgType: msgType};",
"tbelScript": "\r\nvar power = msg.telemetry[0][\"Power[W]\"];\r\nvar heatMeterEnergy = msg.telemetry[0][\"PowerUsage[kwh]\"];\r\nvar flowTemperature = msg.telemetry[0][\"Tin[C]\"];\r\nvar returnFlowTemperature = msg.telemetry[0][\"Tout[C]\"];\r\nvar flowRate = msg.telemetry[0][\"MassFlowRate[kg/s]\"];\r\n\r\n\r\nvar msg = {power: power, heatMeterEnergy: heatMeterEnergy, flowTemperature: flowTemperature, returnFlowTemperature: returnFlowTemperature, flowRate: flowRate};\r\n\r\n\r\nreturn {msg: msg, metadata: metadata, msgType: msgType};"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 298,
"layoutY": 633
},
"type": "org.thingsboard.rule.engine.action.TbCreateAlarmNode",
"name": "Not Active",
"debugMode": true,
"configuration": {
"scriptLang": "TBEL",
"alarmDetailsBuildJs": "var details = {};\nif (metadata.prevAlarmDetails) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n delete metadata.prevAlarmDetails;\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"alarmDetailsBuildTbel": "var details = {};\nif (metadata.prevAlarmDetails != null) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n metadata.remove('prevAlarmDetails');\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"useMessageAlarmData": false,
"overwriteAlarmDetails": false,
"alarmType": "Inactive",
"severity": "CRITICAL",
"propagate": true,
"relationTypes": [],
"propagateToOwner": false,
"propagateToOwnerHierarchy": false,
"propagateToTenant": false,
"dynamicSeverity": false
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 300,
"layoutY": 691
},
"type": "org.thingsboard.rule.engine.action.TbClearAlarmNode",
"name": "Active",
"debugMode": true,
"configuration": {
"scriptLang": "TBEL",
"alarmDetailsBuildJs": "var details = {};\nif (metadata.prevAlarmDetails) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n delete metadata.prevAlarmDetails;\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"alarmDetailsBuildTbel": "var details = {};\nif (metadata.prevAlarmDetails != null) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n metadata.remove('prevAlarmDetails');\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"alarmType": "Inactive"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 620,
"layoutY": 635
},
"type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode",
"name": "To Active Telemetry",
"debugMode": true,
"configuration": {
"jsScript": "var msg = {};\nmsg.activityState = false;\nreturn {msg: msg, metadata: metadata, msgType: \"POST_TELEMETRY_REQUEST\"};"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 621,
"layoutY": 689
},
"type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode",
"name": "To Active Telemetry",
"debugMode": true,
"configuration": {
"jsScript": "var msg = {};\nmsg.activityState = true;\nreturn {msg: msg, metadata: metadata, msgType: \"POST_TELEMETRY_REQUEST\"};"
},
"externalId": null
},
{
"additionalInfo": {
"description": null,
"layoutX": 893,
"layoutY": 659
},
"type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode",
"name": "Save Timeseries",
"debugMode": true,
"configuration": {
"defaultTTL": 2592000,
"skipLatestPersistence": null,
"useServerTs": null
},
"externalId": null
}
],
"connections": [
{
"fromIndex": 2,
"toIndex": 1,
"type": "Post attributes"
},
{
"fromIndex": 2,
"toIndex": 3,
"type": "RPC Request from Device"
},
{
"fromIndex": 2,
"toIndex": 4,
"type": "Other"
},
{
"fromIndex": 2,
"toIndex": 5,
"type": "RPC Request to Device"
},
{
"fromIndex": 2,
"toIndex": 7,
"type": "Post telemetry"
},
{
"fromIndex": 6,
"toIndex": 2,
"type": "Success"
},
{
"fromIndex": 6,
"toIndex": 8,
"type": "Alarm Created"
},
{
"fromIndex": 6,
"toIndex": 9,
"type": "Alarm Cleared"
},
{
"fromIndex": 7,
"toIndex": 0,
"type": "Success"
},
{
"fromIndex": 8,
"toIndex": 10,
"type": "Created"
},
{
"fromIndex": 9,
"toIndex": 11,
"type": "Cleared"
},
{
"fromIndex": 10,
"toIndex": 12,
"type": "Success"
},
{
"fromIndex": 11,
"toIndex": 12,
"type": "Success"
}
],
"ruleChainConnections": null
}
}