Device Mapper Scripting example: modify an input with a script

In this example we will show you how to modify a mapped variable (input) and generate a new value as an output.

You can imagine the situation where you want to alter the original Temperature value of your device and make some calculation, for example, divide it by 10.

Steps to change an output of a variable using scripting

1. First map to the Variable of your choice in the device mapper (in this example, we will use Temperature).

2. ​Now this Temperature should be used as an output in the Scripting section (the script will be applied to this variable).

The following image shows the result of choosing this output for the script. Follow the steps described until you get the same configuration as in the image.

Finally click in Edit (3) to get the script editor opened.

3. Now you need to paste the code in the editor, and test the script. Follow the steps in the image. 

The following example code is quite simple, but it does the job of dividing the original temperature input by 10

var temperature = fields.get ('Temperature', 0);
log('original temperature value: '+ temperature);

temperature = temperature / 10;

return temperature;

Note: you need to add the variable to the list of fields if you want to test your code. You can also use SimOne to make tests on the script simulating a real device.



4. Finally, save the Device Mapper configuration.