Skip to end of metadata
Go to start of metadata

Triggers allow you to define a logic function in a script that can perform operations as a thing event is activated. A reference value and a timestamp is supplied with the event which can be taken as an input argument to the trigger function. Typical operations include data validation, sending an email if a value is over a predefined level, etc.

Trigger scripts are created by following these simple steps:

1. Define trigger function


GadgetKeeper provides support for two trigger function signatures:

Signature Description
function onEvent(event){}
"event" is a single event object.
function onEvents(events){}

"events" is an array of events.

  • Useful when several events occur in a batch
  • These can be processed together faster than one by one.

The "event" object has the following properties:

id Event Id, UUID object Generated by GK.
timestamp Time then event was created, long. Specified by Thing.
value Event value as data type declared in Event property, object.

For example, this function sets three variables for a single event object:

The "events" array is a JavaScript array as defined here:

events.length number of event objects in array
events[i] single event object in array

For example, this function sets three arrays to the corresponding properties of the event objects in the "events" array

 

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

  1. Select the trigger in the "Triggers" section of the thing
  2. Click "Edit Script

 

 

This code follows the example illustrated in Thing Trigger Settings and Program Thing Interaction to activate the start/stop methods in the "Airconditioner" thing if the temperature value goes above/below a certain value:

2. Comply to expected event type


The  onEvent(s) function accepts event/events with a property "value" that has a data type which was defined for the array in its Create/Edit Event dialog box

  • Whenever primitive types are specified (boolean, string, int, long, float, double, string), standard Java Script types are used:

    The following code shows two primitive types as a function input (event) argument

  • When "array" is specified as the data type for the event, the "value" property of a single event object is a JavaScript array.

    For example, the following code shows how an "array" data type is implemented in an input (event) argument

     

  • When "structure" is specified as a data type for the event, the "value" property of a single event object is a JavaScript object.

    For example, the following code shows how a structured data type of "weather" with fields "temperature"(float) and "condition"(string) is implemented in an input (event) argument:

3. Code trigger 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 to a REST service
  • retrieve value from a REST service
  • retrieve value from device via remote property
  • fire an event
  • No labels