Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
As part of my code, I monitor PLC tags to generate alerts. To limit the number of alerts logged while a sensor reading is on the edge of generating an alert, I wrote a some code that act like a debounce timer. The timer starts when the value either hits LoLo or HiHi alert states. If the value leaves that state before the timer times out, then the timer is reset and doesn't start until it hits the alert state again. Below is the code I use for this.
My question is: Is there a better way of doing this in ThingWorx? Perhaps one that is more efficient than what I wrote?
var time = me.AlertDebounce * 10; /* AlertDebounce is in seconds */
if ((me.Device_Value < me.LoLo_Alarm_SP) || (me.Device_Value > me.HiHi_Alarm_SP)) {
var trigger = true;
/* If trigger remains true after for loop completes, then generate alarm. */
/* If Device_Value leaves alarm state setpoint, trigger goes false and */
/* time is copied to x, causing the for loop to finish. */
for (var x = 0; x < time; x++) {
pause(100); /* for loop paused for 100 milliseconds (0.1 seconds) */
if ((me.Device_Value < me.LoLo_Alarm_SP) || (me.Device_Value > me.HiHi_Alarm_SP)) {
trigger = true;
} else {
trigger = false;
x = time;
}
}
}
There is no out of the box already implemented functionality for this, although it seems like something you could propose in our ideas boards here https://community.ptc.com/t5/ThingWorx-Ideas/idb-p/thingworxideas
The logic seems ok, the only concern I could think of, how many properties and how often could reach the alert state to trigger this timer? If it happens on several hundred properties, depending on your system resources, the pause multiplied by 100, could possibly oversaturate your system.
Thanks for the response Posipova.
I have the same concern about the amount of pauses. I won't have hundreds of properties in the same Thing that I need to monitor and alert, but I will have hundreds of Things with a few properties that I will want to alert on. A property alert triggers a subscription that calls the same service that has the debounce programming in it.
Would adjusting the pause time to match the scan rate of the property (usually 1000) decrease the chances of oversaturation? The amount of time being paused would remain the same, but the number of cycles in the for...loop would decrease.
You could try and grab a stacktrace to see how it behaves and whether threads start overlapping.