Device status behind gateway
It’s common requirement to indicate device status or trigger alarm upon any communication failure. You can easily check for device connection status using app.device[X], whereby X is the entered device name. However, there are times that your sensors or devices could be connected behind other gateway or any server. Hence you may always get a good connection status from gateway even though the sensor is down. So the only way to check for connection status is to monitor for timeout since the targeted sensor or device’s data change.
The first step is to associate current time as virtual tag, which will be used as limit reference and will be keep on shifting dynamically.
//seconds since 1970 in integer
setTag( "sec1970", ((new Date()).getTime()/1000)|0);
The second step is to update the last known timestamp added with timeout limit, whenever good data is received.
var LIM = 5;
var d = (new Date()).getTime();
setTag( "sensor001_time", (d/1000+LIM)|0); //last known update time plus timeout.
Finally you just need to add 2 tags into alarm for tag to tag comparison.
You only need to trigger this script every second or slower, depending what’s the additional delay that you can accept. Everytime the tags are changed, the Alarm task will be triggered to check. If you have a slow machine, then try to trigger as slow as possible.
TIPS: The trailing
|0 in above examples is just the shortest and fast syntax to convert floating point to integer. You may convert a floating point tag to integer tag with expression just like this: