## Overview

Algorithms in the SDE allow derived metrics to be calculated from raw metrics. An example of an algorithm might calculate velocity given position and time. Derived metrics are then available to plot on graphs or appear on data tables when performing analysis of a sensors' observations.

## Basic Steps

- Navigate to Integrations --> Algorithms
- Enter in a unique ID e.g. MY_ALGORITHM
- Enter in a comma-separated list of input metrics e.g. z
- Enter in a comma-separated list of output metrics e.g. el
- Add code for the
**filter**code hook that specifies any conditions for when this algorithm applied. Return a boolean**true**or**false** - Add code the
**apply**code hook that specifies the main part of the algorithm that converts the input metric to the output metric. Return the newly constructed output time-series data.

## Basic Example

This (fictitious) very basic algorithm just adds 50 to the value of the temperature metric:

Here is the code for easy copy-and-paste:

function apply(context, timeSeries) {

var output = api.buildSensorMetricSeries(timeSeries.getSensorId(), 'R', 'ohm');

var obsList = timeSeries.asObservations();

for(var i = 0; i < obsList.size(); ++i) {

var observation = obsList.get(i);

output.add(observation.getTimestamp(), observation.getValues()[0] + 50)

}

log.info("Output is: " + output)

return output

}

Let's go thru this line-by-line

- The filter method just returns
**true**which means this algorithm applies to all sensors - The apply method line 2 creates the output time-series data with a metric of
**R**and unit**ohm**. This is just an example, and realistically you would never convert temperature**T**to resistance**R.** - On line 3, the timeSeries argument is converted to a list of
**observations** - We then loop thru each existing temperature observation and simply add 50 the value and add it to the output time-series we initialized on line 2.
- Finally, we print out the output and return it

## Verification and Testing

To test your algorithm directly, you can choose a sensor that has the input metric you are creating an algorithm for and matches the constraints you specified in the **filter** method. Or, if they are no constraints (e.g return **true**), then you can choose any sensor that has the input metric associated with your algorithm.

Click **Test** below the editor to test it once you have selected the sensor using the sensor picker above the editor.

You can also test it by saving the algorithm integration and then attempting to **graph** the data. You should see your new output metric in the dropdown when choosing metrics to graph. For example this is what the very basic example described above would look like when graphing it:

This is what the results would look like in a data table. Notice the difference of **50**:

## Comments

0 comments

Please sign in to leave a comment.