--- tags: ThingsBoard --- # Thingsboard Alarms [toc] <br/> ## Originator * Alarm originator is an entity that causes the alarm. <br/> ## Type * Alarm type helps to identify the root cause of the alarm. ex: High Humidity <br/> ## Severity Level * Critical * Major * Minor * Warning * Indeterminate * (Normal) <br/> ## Lifecycle * Active * unacknowledged * acknowledge * Clear * unacknowledged * acknowledge <br/> ## [Alarm Rules in Device Profile](https://thingsboard.io/docs/user-guide/device-profiles/#alarm-rules) * Fixed Threshold * Enter a fixed value as a threshold * Advanced Thresholds * Enable or disable rule based on the value of “temperatureAlarmFlag” attribute * Timeseries -> dynamic source type -> current device / temperatureAlarmTreshold * add key filter -> attribute -> temperatureAlarmFlag -> Boolean equal true * go to devices and setup two attributes (temperatureAlarmThreshold, temperatureAlarmFlag) * Dynamic Thresholds based on the tenant or customer attributes (recommend) * To avoid configuration of the attribute for each device * key filter -> alarmEnabled -> Boolean -> True equal -> current tenant * voltageAlarmEnabled (tenant or customer attribute) <br/> ## Alarms created by device profile in Rule Chain ### Test data and metadata ![](https://i.imgur.com/2dhvMD6.png) * Alarm msg (test in debug mode) ``` //data { "tenantId": { "entityType": "TENANT", "id": "51a597d0-7771-11ed-8fbb-8db0f3b43c8f" }, "customerId": { "entityType": "CUSTOMER", "id": "13814000-1dd2-11b2-8080-808080808080" }, "type": "LowVoltage", "originator": { "entityType": "DEVICE", "id": "fcb41fe0-779c-11ed-805a-39b86a964058" }, "severity": "WARNING", "status": "ACTIVE_UNACK", "startTs": 1681196395047, "endTs": 1681196395047, "ackTs": 0, "clearTs": 0, "details": null, "propagate": true, "propagateToOwner": false, "propagateToTenant": false, "propagateRelationTypes": ["Contains"], "id": { "entityType": "ALARM", "id": "76c26b80-d836-11ed-8cdd-ed62a627ee0a" }, "createdTime": 1681196395064, "name": "LowVoltage" } ``` ``` //metadata { "deviceType": "smart light", "isNewAlarm": "true" } ``` ![](https://i.imgur.com/XLi5G89.png) After new Alarm created, Outbound message will contain additional property inside **Metadata - isNewAlarm** with true value. Message will be **passed via Created chain**. After existing Alarm updated, Outbound message will contain additional property inside **Metadata - isExistingAlarm** with true value. Message will be **passed via Updated chain**. [Rule Engin Overview](https://thingsboard.io/docs/user-guide/rule-engine-2-0/overview/) <br/> ### Build an alarm count for a device * Start from Device Profile Node ![](https://i.imgur.com/pakUFP6.png) * Logic Flow 1. Alarm Filter (Create / Clear) 2. Get current alarm amount 3. Modify alarm amount 4. Update alarm amount #### Alarm Filter ```javascript if(metadata['isNewAlarm']){ metadata['actionType'] = 'Alarm Created'; }else if(metadata['isClearedAlarm']){ metadata['actionType'] = 'Alarm Cleared'; }else{ metadata['actionType'] = 'Alarm Updated'; } return {msg: msg, metadata: metadata, msgType: msgType}; ``` ```json //input-metadata { "deviceType": "smart light", "isClearedAlarm": "true" } ``` ```json //output-metadata { "actionType": "Alarm Cleared", "deviceType": "smart light", "isClearedAlarm": "true" } ``` <br/> #### Get Current Alarm Amount ![](https://i.imgur.com/aJH8PJW.png) <br/> * In device detail create alarm attribures (type: shared attributes) ![](https://i.imgur.com/hu9J4nK.png) <br/> #### Modify Alarm Amount ```javascript // original version var alarmAmount = metadata['shared_' + msg.severity]; switch (metadata.actionType) { case 'Alarm Created': ++alarmAmount; break; case 'Alarm Updated': break; case 'Alarm Cleared': --alarmAmount; break; } var data = {}; data[msg.severity] = alarmAmount; var newMatadata = { deviceName: metadata.deviceName, deviceType: metadata.deviceType }; var newMsgType = "POST_ATTRIBUTES_REQUEST" return {msg: data, metadata: newMatadata, msgType: newMsgType}; ``` * Original version will exist an error when two parameters transferred into the node at the same time. ```javascript // update version var alarmAmount = metadata['shared_' + msg.severity]; var totalNumber = Number(alarmAmount); var create = 0; var clear = 0; switch (metadata['actionType']) { case 'Alarm Created': create++; break; case 'Alarm Updated': break; case 'Alarm Cleared': clear--; break; } var overall = create+clear; totalNumber += overall; var data = {}; data[msg.severity] = totalNumber; var newMatadata = { deviceType: metadata.deviceType }; var newMsgType = "POST_ATTRIBUTES_REQUEST"; return {msg: data, metadata: newMatadata, msgType: newMsgType}; ``` <br/> #### Update Alarm Amount ![](https://i.imgur.com/P7PVnGc.png) <br/> ## Appendix ### Device Profile Node **Persist state of alarm rules** - forces the rule node to store the state of processing. This setting is useful if you have duration or repeating conditions. If enabled, and if the incoming message matches at least one of the alarm conditions, it will cause additional write operation to persist in the state. **Fetch state of alarm rules** - forces rule node to restore the state of processing on initialization. On rare occasions. Assuming you have many devices that send data frequently or constantly, you can avoid loading the state from the DB on initialization. The Rule Node will fetch the state from the database when the first message from a specific device arrives. <br/> ## Reference [TB alarm brief intro](https://thingsboard.io/docs/user-guide/alarms/) [Send email on alarm](https://thingsboard.io/docs/user-guide/rule-engine-2-0/tutorials/send-email/) [Device Profiles](https://thingsboard.io/docs/user-guide/device-profiles/#notifications-about-alarms) [Create and Clear Alarm](https://thingsboard.io/docs/user-guide/rule-engine-2-0/tutorials/create-clear-alarms/) [Alarm Dashboard Mobile Setting](https://thingsboard.io/docs/mobile/alarm-dashboard/#:~:text=Alarm%20details%20dashboard%20is%20configurable%20in%20the%20alarm,Mobile%20dashboard%20field%3B%206%20Click%20Apply%20changes%20button%3B) [Alarm Count](https://thingsboard.io/docs/user-guide/rule-engine-2-0/action-nodes/)