Building a Trigger for a Generator with a Thing Key Generator
This section describes an example of building a trigger for a generator with a Thing key generator.
This generator has a fuel tank with a property key of fuel that contains the amount of fuel in the tank (measured in gallons). The generator also has an attribute with a key of capacity that specifies the size of the fuel tank. Additionally, there is an alarm with a key of status that describes the fuel level, with 0 (zero) representing an acceptable amount of fuel and 1 (one) indicating that the fuel level is low.
The trigger is defined to execute when the fuel property changes for the thing generator. The capacity attribute is read to determine if the percentage remaining in the tank is greater than or less than 10%. If it is greater than 10%, the status alarm is set to 0 (zero) and the trigger ends successfully. If the tank is 10% or less, the status is set to 1 and the trigger ends successfully.
To build the Generator sample trigger, do the following:
-
In the Trigger screen, click New Trigger.
-
Type the name of the trigger, for example, Generator fuel level.
-
Right-click the Start node to display the trigger event window.
-
Select
. -
In the Thing key box, type generator and in the Property key box, type fuel.
-
Click Add.
-
In the Trigger Actions list, select the Thing action, and then drag thing.attr.find onto the canvas.
-
In the Thing key box, type or select from the Suggestions list $(event.thing.key).
Alternatively, in the Thing key box, you could type generator because the preceding Event variable contains this string.
-
In the Attribute key box, type capacity, and then click Add.
-
Create the route from the property.change event triangle to the 0.thing.attr.find action.
-
Under Trigger Actions, select the Math action.
-
Drag math.expr onto the canvas.
-
In the Expression box, specify $(event.prop.value) / $(action.0.value) * 100 to divide the current fuel value by the capacity of the tank and multiply that result by 100 to give the percentage.
-
Create the success route from 0.thing.attr.find to 1.math.expr.
-
Drag math.expr onto the canvas.
-
In the Expression box, specify $(action.1.value) <= 10 to compare the percentage of fuel remaining to 10%.
-
Create the success route from 1.math.expr to 2.math.if.
-
In the Trigger Actions list, select the Alarm action, and then drag alarm.publish onto the canvas.
-
In the Thing key box, type or select from the Suggestions list $(event.prop.value).
Alternatively, in the Thing key box, you could type generator because the preceding Event variable contains this string.
-
In the Alarm key box, type status, and in the State box, type 1, and then click Add.
-
Create the true route from 2.math.if to 3.alarm.publish.
This is the route the logic will follow when the fuel value is 10% or less.
-
Drag another alarm.publish onto the canvas.
-
In the Thing key box, type or select from the Suggestions list $(event.prop.value).
Alternatively, in the Thing key box, you could type generator because the preceding Event variable contains this string.
-
In the Alarm key box, type status, and in the State box, type 0, and then click Add.
-
Create the false route from 2.math.if to 4.alarm.publish.
This is the route the logic will follow when the fuel value is greater than 10%.
-
In the Trigger Actions list, select the End action, and then drag Success onto the canvas. .
-
Create the success route from 3.alarm.publish and 4.alarm.publish to 0.
-
Click Add at the bottom of the screen.
-
To activate the trigger, click Start on the trigger details page.
You now have a working trigger for the generator.