Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Script properties Property scripts are created by following these simple steps:

Table of Contents

1. Set property type as "script"

...

Open the property create/edit dialog by clicking "New" or "Edit" in the "Properties" section of the "thing"

  1. set type to "script"
  2. Select the Data Type which is to be used for the argument (function input) and/or return value (output)
  3. check "Readonly" if you want your property to be readable but not settable (only the get<PropertyName> function is required in the script)
  4. Check "Private" if you want your property to be unreadable (except for script functions of properties/methods/triggers within the thing)
  5. 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:

    • <Name> is the property name with an upper-cased first letter.
    • "set<Name>" function does not need to be provided if you set the property as "Readonly" as described in step 1.3.

To enter the script, the "Edit Script" dialog box must be opened:

    1. Select the script property in the "Properties" section on the thing
    2. Click "Edit Script"

The example shows the formatting for the get/set functions of a "Temperature" script property:

    • The temperature value is accessed from a persistent property called "Temp"
    • An email is sent if the temperature is too high in "getTemperature( )"
    • An "out of range" message is shown if the value for "setTemperature( )" is out of range

 

Info

Note: If "Readonly" is checked, the set<Name> function is not required in the script (only the get<Name> function 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:

    Code Block
    language javascript
    //returns float
    function getTemperature(){
            return 2.8;
    }
    
    //expects float
    function setTemperature(value){
            var example=value*value;
    }
  • 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:

    Code Block
    language javascript
    //returns float[]
    function getTemperatures(){
            return new Array(2.8, 3.4, 5);
    }
    
    //expects float[]
    function setTemperatures(value){
            var len=value;
            var example=value[0];
    }
  • When structures are specified as a data type, JavaScript objects are used.

    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:

    Code Block
    language javascript
    //returns Weather  
    function getWeather(){
            var w=new Object();
            w.condition="Scattered Clouds";
            w.temperature=30;
            return w;
    }
     
    //expects Whether
    function setWeather(value){
            if(value.condition=="Sunny" && value.temperature==20){
               log.debug("Oh Happy Day!");
            }
    }

4. Code property logic

...

You have a lot of options. See our Scripting Recipes for more details.

...