By setting the property type as "script", you can define set/get functions to perform operations as another property value (persistent or remote) is accessed. Typical operations include validation of incoming values, sending an email if a value is over a predefined level, etc.
Property scripts are created by following these simple steps:
1. Set property type as "script"
Open the property create/edit dialog by clicking "New" or "Edit" in the "Properties" section of the "thing"
- set type to "script"
- Select the Data Type which is to be used for the argument (function input) and/or return value (output)
- check "Readonly" if you want your property to be readable but not settable (only the get<PropertyName> function is required in the script)
- Check "Private" if you want your property to be unreadable (except for script functions of properties/methods/triggers within the thing)
- Check "Nullable" if the "null" is acceptable as property value
2. Define get/set property access functions
All script properties must be provided with a script with the get/set access functions defined. These two functions have the form: "get<Name>" and "set<Name>", where:
To enter the script, the "Edit Script" dialog box must be opened:
The example shows the formatting for the get/set functions of a "Temperature" script property:
Note: If "Readonly" is checked, the set<Name>is not required in the script (only the get<Name> is required)
3. Comply to declared data type
The set/get functions must accept/return the data type that was predefined in the other property being accessed. This was declared in its Create/Edit Property dialog box. This same data type must be selected when defining the data type for the script property in step 1.2. If another data type is selected, an error message will be shown.
Whenever primitive types are specified (boolean, string, int, long, float, double, string), standard Java Script types are used
For example, the following code shows a primitive type as a return argument, and then as a function input argument:
When arrays are specified as a data type, Java Script arrays are used
For example, the following code shows how an array is implemented as a return argument, and then as a function input argument:
For example, the following code shows how a structured data type of "Weather" with fields "temperature" (float) and "condition" (string) is implemented as a return argument, and then as a function input argument:
4. Code property logic
You have a lot of options. See our Scripting Recipes for more details.
Just some ideas:
- validate incoming values and save the value in internal persistent property
- validate incoming value and pass it to the device via remote property
- send an email
- send the value with a REST service
- retrieve value with a REST service
- retrieve value from device via remote property
- fire an event