Setting a Value From an Edge Action
This topic describes how to set a value from an edge action.
The following steps display the value of the light sensor on the LCD screen included with the mangOH Red, to illustrate how to set a value using an Edge Action:
- Navigate to Device > Observations.
- Click Add Observation.
- Click the Observed resource drop down and select /redSensor/light/value.
- Enter an Observation name.
- Set Send events to Edge Action.
- Click Save.
- Navigate to Device > Edge Actions.
- Click Add Edge Action.
- Click Source Observation and select the edge action configured in the previous steps.
- Click Edge Action name and enter a descriptive name.
- Click the Code tab and paste the following code in:
function (event) {
var lightReading = event.value;
var lightDescription;
if (lightReading > 1500) {
lightDescription = "bright"
} else if (lightReading > 300) {
lightDescription = "a bit dim"
} else {
lightDescription = "dark"
}
var s = "It's " + lightDescription + " in here.";
return {
"dh://lcd/txt3": [s]
}
}
The code reads the light value, concatenates it with some text based describing the brightness, and then writes the string to the /lcd/txt3
resource (third line of LCD text) on the Data Hub which is indicated with dh:
.
12. Click Save.
Return syntax
The returned object can include several paths, so several resources can be updated from a single edge action.
Also, the action runner expects an array of objects for each path. As a consequence, even your script provides only one value, you must use the syntax "dh://resource_path": [ resource_value ].
Of course, the value you provide can itself be an array.
Updated almost 5 years ago