Simple temperature control

Note: fan and temp refer to variable names assigned to data pionts in the script context on the Data Source settings.

if (temp.value < set.value - 1)
{ 
fan.set(true) 
} 
else if (temp.value > set.value + 1) 
{ 
fan.set(false) 
}

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.infiniteautomation.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
               "type":"BINARY_STATE",
               "sourceType":"DATA_POINT",
               "xid":com.serotonin.m2m2.Common.generateXid(xidPrefix),
               "name":"",
               "alarmLevel":"URGENT",
               "durationType":"MINUTES",
               "duration":15,
               "state": (dp.textRenderer.type === "BINARY" 
                      && dp.textRenderer.oneLabel === "Device Ok")
            };
            
    //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}));