Virtual sensors produce data based on scripts, made in the script editor.
We recommend you first make your sensors in the normal way, before creating scripts. Scripts can then be accessed on the machine page in the Admin app. The drop down panel shows any existing scripts (1.). By clicking on these the script editor is opened. Alternatively you can click on the (2.) icon and click on the "Add script" menu option, to add a new script.
When creating a new script, the script editor will appear as shown in the image below.
(3.) The drop down switcher allows you to easily move from one script to another. If you've made changes you'll be warned to save these before switching.
(4.) When you start a new script, you should assign a title and description. Keep these short and unique. In the description, briefly explain what the script is for.
(5a.) The script frequency will be shown here once once you have selected an output sensor, as shown in the screen below (5b.).
(6.) The menu here allows exported scripts to be imported to new scripts. This is explained further down this article.
(7a.) Here you need to select the associated sensor(s). As shown in the image below (7b.) you need to tick the checkbox of the output sensor(s). A script requires at least 1 output sensor, which can be either a virtual sensor or a manual sensor. Input sensors can be of any type.
A sensor alias is automatically generated, based on your sensor names. You can change this, but it can only start with a letter, not a number. The alias is how you should refer to the sensor in the script itself.
To start editing a new script (8a.) or an existing one (8b.) you need to click in the editor area. For more information about the script language refer to this article: The script language. See the script examples as a reference as well: Script examples.
As you can see from the difference between (9a.) above and (9b.) below, the buttons are enabled and disabled based on the edit status of the script. Once you have written some script, or edited an existing one the "Cancel" and "Save buttons" become available.
Clicking the "Cancel" button cancels any unsaved changes. Clicking "Save" will save the script. Saving allows you to leave the script editor and return to continue editing the script another time. It does not activate the script. Once a script has been saved and has no errors, it can be activated by pushing the "Activate" button.
The "Test" button is always available and clicking it will test the script for errors. If errors are found, a dialog box will indicate on which line the error has been found. Correct these errors and then test it again, until no errors are found. You must remove all errors before your script can be activated.
The script version history is an archive of all the active versions of the script (10.). This way, sensor data can be viewed in the light of what the script was doing at any given time.
Once a script has been activated, it will receive a time stamp in the version history. Any previously active scripts will become inactive and move down the list. Historic script versions can be viewed by clicking on them, but cannot themselves be deleted, edited, or reactivated. However, when viewing a previous script version, it can be edited and then saved and activated as a new version.
Scripts can be activated and deactivated using the toggle button in the top right of the screen (11.).
The menu button in the top right (12.) gives the following options:
- "Edit Script", which is another way to activate the script text area in edit mode.
- "Duplicate script", which will create a new script based on the current one, for the same machine. It will copy the selected sensors, and script text, but requires a new name and description.
- "Export script", which allows the script to be copied and imported to a new script (6.) This is explained in more detail below.
- "Delete script".
Exporting and Importing Scripts
Exporting and importing scripts, allows scripts to be copied from one machine to another.
The first step is to go to an existing script and click the 'Export script' button (13.). This will be downloaded to your device as a .json file.
The next step is to open a new script on the chosen machine. When this opens, it is important that you import the .json file before saving the new script. Open the top right menu (14.).
Drag and drop your .json file into the download dialog (15.).
The system then identifies and lists the sensors which are referenced in the script. You then need to select the sensors in your machine (16.) which have the same function as those referenced in the script.
Once you have done this, the old sensor name is shown crossed out in red, and your new one in green (17.). To confirm your choice, click the button 'Change' for each sensor.
When you click the "Change" button for each sensor, it appears in list below (18.), showing the new alias, frequency rate and whether it is an output sensor or not. Click "Continue" to close the window and continue with the script editor.