Script Examples

June 2nd, 2020


Smoothness Example

This Script will generate the the same smoothness value as the smoothness event detector. You can use this to calculate what your low limit should be on the smoothness detector. Note that generate history my not give the same results with this as it uses the real time updates for the boxcar. Give your point variable the name “signal” and set to context update.

var boxcarLength = 60;
var boxcar = signal.last(boxcarLength);
//Note that as in the detector if there isn't at least 3 values it is considered smooth
if(boxcar.size() <3)
  return 1;
  var prev = Number.NaN;
  var lastAngle = Number.NaN;
  var sumErr = 0;
  var count = 0;
  //Walk backwards across the list to perform calc in time order
  for(var i=boxcar.size()-1; i>=0; i--){
    var value = boxcar.get(i).value;
    if(!isNaN(prev)){
      var opp = value - prev;
      var hyp = Math.sqrt(0.1 + opp * opp);
      var angle = Math.asin(opp / hyp);
      if(!isNaN(lastAngle)){
      var diff = angle - lastAngle;
      var norm = diff / Math.PI;
      sumErr += norm <0 ? -norm : norm;
      count++;
    }
  	lastAngle = angle;
  }
  prev = value;
}
var err = sumErr / count;
return (1.0 - err);

JsonEmport Example

This script adds a binary state event detector to all data points identified by a watchlist XID. Discussion may occur about this script in this forum thread: https://forum.mango-os.com/topic/3708/a-script-jsonemport-example

var xidPrefix = "ED_15m_status_"; //Used to see if a point already has the detector
var watchListXid = "WL_5275e48f-5819-4971-8462-0d1ed8ea8264"; //Identify the points to add to

var dataPoints = JSON.parse(JsonEmport.getConfiguration("dataPoints")).dataPoints; var watchLists = JSON.parse(JsonEmport.getConfiguration("watchLists")).watchLists;

function findByXid(list, xid) { for( var k = 0; k < list.length; k+=1 ) if( list[k].xid === xid ) return list[k]; return undefined; }

var wl = findByXid(watchLists, watchListXid); if(typeof wl === 'undefined') throw "Couldn't find watchlist with XID " + watchListXid + " to add detectors to";

var importPoints = []; function addDetectorToDataPoint(dp) { for( var l = 0; l < dp.eventDetectors.length; l+=1 ) { if(dp.eventDetectors[l].xid.startsWith(xidPrefix)) //Don't add it twice return; }

var ed = { //Gotten by configuring one and exporting it
           &quot;type&quot;:&quot;BINARY_STATE&quot;,
           &quot;sourceType&quot;:&quot;DATA_POINT&quot;,
           &quot;xid&quot;:com.serotonin.m2m2.Common.generateXid(xidPrefix),
           &quot;name&quot;:&quot;&quot;,
           &quot;alarmLevel&quot;:&quot;URGENT&quot;,
           &quot;durationType&quot;:&quot;MINUTES&quot;,
           &quot;duration&quot;:15,
           &quot;state&quot;: (dp.textRenderer.type === &quot;BINARY&quot;
                  &amp;&amp; dp.textRenderer.oneLabel === &quot;Device Ok&quot;)
        };

//Note that compatibility is not being checked, but it should just fail
// the individual points during the import if isn't a binary point.
dp.eventDetectors.push(ed);
importPoints.push(dp);

}

if(wl.type === 'static') { //Okay, just look over an XID list for(var j = 0; j < wl.dataPoints.length; j+=1) { var dp = findByXid(dataPoints, wl.dataPoints[j]); addDetectorToDataPoint(dp); } } else if(wl.type === 'query') { //Okay, use the query to get the data points from the DataPointQuery var addToPoints = DataPointQuery.query(wl.query); for(var j = 0; j < addToPoints.length; j+=1) { var dp = findByXid(dataPoints, addToPoints[j].getXid()); addDetectorToDataPoint(dp); } }

//Mango 3.5 will allow us to //JsonEmport.setImportDuringValidation( true );

JsonEmport.doImport(JSON.stringify({"dataPoints":importPoints}));

Copyright © 2024 Radix IoT, LLC.