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 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).

Screenshot 2024-04-05 at 13.15.26.png

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.

3. Press on Open Script Editor button 
Screenshot 2024-04-05 at 13.20.10.png

4.. 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 the device simulator to generate sampling data for the script.

Screenshot 2024-04-05 at 13.20.50.png

4. Save the device mapper script.

5. Save the device mapper configurations

Screenshot 2024-04-05 at 13.21.38.png