The guide shows how to create an expression that will return a formatted string. Before starting, create a local Modbus device, a tag with the name source_tag and the data format FLOAT (32-bit floating point).
DISCLAIMER
You can just use the format function from MosText. This guide is given for an example of working with scripts and modules
Open Monokot Server Administrator, double-click Scripts on the Server Explorer pane and go to the Modules tab. Click the New Module button and name the module Sprintf. Insert the following code into the code editor:
Press Ctrl + Enter to apply the change in the code editor.
By default, the ECMAScript 5 standard does not provide a string formatting function, but since there are a huge number of developers who code in JavaScript, most typical tasks have already been solved, as in this case. The sprintf function (https://github.com/alexei/sprintf.js) has existed for several years now and is analogous to the PHP sprintf function.
Go to the Expressions tab and click the New Expression button. For the expression that appeared, set the name Message and specify the data type STRING. Insert the following code into the expression code editor:
var val =context.data['source_tag_trig'].state.value;returnsprintf('Formatted number: %.3f', val);
Press Ctrl + Enter to apply the change in the code editor. Go to the Triggers tab and click Add Data.... Select the previously created tag (source_tag) as the data source for the trigger. Enter source_tag_trig as the name (key) of the trigger.
Go to the Imports tab and click the checkbox of the Sprintf module.
In order for the changes to take effect on the server, click the Sync button. If you switch to Go Online mode, you will see a picture similar to the following (in my case, the number 0.123456 is displayed).