Saved Value Device Mapper Scripts

Saved value Device Mapper scripts open up many scripting possibilities.

  • The result of these scripts will be saved and the script will not be re-evaluated when reading tracks or generating reports.
  • This saved value field scripts can be used to keep a history of Custom Fields.
  • When changing the script or reprocessing tracks, the stored value won't be modified.

Developers may also think of saved value to mean persistable field scripts.

1. Make sure you're familiar with the concepts of Device Mapper and Device Mapper Scripts

2. Log in to an application and go to Admin > Device Mapper > select the device model >

3. Navigate to Scripts and press on the +Script button 

Screenshot 2024-03-22 at 11.24.26.png

4. Fill in the name of the script, add the Output variable, and enable Persist Result > Yes

Screenshot 2024-03-22 at 11.26.54.png

5. Press on the Open Script Editor button 

Usage examples

Ensure to utilize the appropriate Output variable for each example and click on "Test Script" before saving in the Script Editor. 

1. Example: Custom Fields history

Output -> TemperatureThreshold (Custom Variable created in SiteAdmin of type double)

Script:

// -----------------------------

var temp = user.getCustomFieldValue('TempThreshold'); // User custom field, needs to be defined first and should be of type number

if(temp != null && temp != undefined) {
return parseFloat(temp);
}

return null

// -----------------------------

Now TempThreshold can be used in Event rules or reporting.

 

2. Example: Handle key=value data from the device (2 scripts)

Script 1

Output -> Temperature1

// ------------- SCRIPT ------------------
var val1 = fields.get('CustomString', null);

if(val1) {
var kvp = val1.split('=');
if(kvp.length ==2 && kvp[0] == 'temp1') {
return parseFloat(kvp[1]);
}
}

return null; // keep previous value

// ----------- END SCRIPT ----------------

 

Script 2

Output -> Temperature2

// ------------- SCRIPT ------------------
var val1 = fields.get('CustomString', null);

if(val1) {
var kvp = val1.split('=');
if(kvp.length ==2 && kvp[0] == 'temp2') {
return parseFloat(kvp[1]);
}
}

return null; // keep previous value

// ----------- END SCRIPT ----------------

 

3. Example: Counter (+1)

Output -> CustomAnalog1

Script:

var crashed = fields.get('Crash', false);
var currentCrashCount = fields.get('CustomAnalog1', 0);

if(crashed) {
return currentCrashCount + 1;
}

return currentCrashCount;

 

4. Example: Counter (cumulative)

Output -> CustomAnalog1. (Note: fuelDiff needs to be a difference of fuel between the current device report and the previous one)

Script:

var fuelDiff = fields.get('fuelDiff', 0);
var totalFuel = fields.get('CustomAnalog1', 0);

return totalFuel + fuelDiff;