Search code examples
thingsboard

How do I delay clearing a ThingsBoard alarm?


I have a rule chain in ThingsBoard that does a Create Alarm when temperature is outside threshold and does a Clear Alarm otherwise. I receive a message using a Telegram bot when these events occur. That all works fine.

However when the temperature is hovering around the threshold, I can get many notifications as it goes in and out of the threshold temperature. That is somewhat annoying.

I would like to have the Clear Alarm activity only trigger if it is more than 5 minutes (say) since the last Create Alarm event was triggered.

Any tips on how to achieve this?


Solution

  • I finally worked out how to do this.

    I added some server attributes to my device that define the temperatures that trigger alarms. I have a rule chain for controlling these alarms with the following nodes:

    enter image description here

    1. Enrichment - originator attributes to add the relevant attributes into the metadata associated with this message
    2. Filter - script to detect if the temperature is outside the expected range
    3. Filter - script to detect if the delay period has expired since the last time the alarm was triggered
    4. Action - create alarm when script detects that temp is out of range
    5. Action - clear alarm when script detects that delay period has expired
    6. Transformation - script to update last alarm time attribute
    7. Action - save attributes to persist updated alarm time attribute
    8. Transformation - script to create a message about alarm set or cleared
    9. Rule chain to handle sending the message to a Telegram bot

    As an example, here is the script for checking if the delay period has expired before clearing the alarm:

    var alarmTime = Number(metadata.ss_lastWaterTempAlarmTime);
    var alarmDelay = Number(metadata.ss_clearAlarmTimeDelay);
    return metadata.ts >= alarmDelay + alarmTime;
    

    ss is the prefix added for server side attributes that have been added to metadata.

    You can see the complete rule chain json in my Aquamon repo.