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:

  1. Navigate to Device > Observations.
  2. Click Add Observation.
  3. Click the Observed resource drop down and select /redSensor/light/value.
  4. Enter an Observation name.
  5. Set Send events to Edge Action.
  6. Click Save.
  7. Navigate to Device > Edge Actions.
  8. Click Add Edge Action.
  9. Click Source Observation and select the edge action configured in the previous steps.
  10. Click Edge Action name and enter a descriptive name.
  11. 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.