Run Function
This node is used to input your own code to control the execution of the flow.
When this node is triggered, its containing Javascript code will be executed
The code it supports is ES6 server side javascript (eg. no window or DOM object).
It can be used to get the output of the previous node and manipulate it in someway, and send data on to the following node. For every node you place in the flow editor, the output of that node is accessible in the 'input' object in the following node.
The output of the previous node is accessed via the 'input' object. The output of the current node can be set by manipulating the 'output' object.
A good way to identify the data / values attached to the node is by changing to test mode, running a test, and clicking 'view output' on the node.

Once the output is on screen, you will see the objects that are available to you in the code. In this example, the 'input' object contains data pertaining to the Event that was created.

The children are :
- id - specifies the id of the Event that was created (eg. var id = input.body.id; )
- name - specifies the name of the Event that was created (eg. var name = input.body.name; )
- data - the data payload of the Event that was created (eg. var data = input.body.data; )
- file - if a file was part of the Event's payload, it will be included here (eg. var file = input.body.file; )
So the following are a few scenarios you might have to use a run function
Scenario 1:
- You have 2 events and you want to distinguish between them
- Check which one has actually executed
- Only execute the one event
- Do two separate things based on which event has executed

So our run function node is going to be set up as follows :
var event = input.body.name;
if (event == "toggle-away" ) {
output.body = "slack.com/api/users.setPresence?token=xoxp-16811800865-542295231655-683636284785-e48d49a53d1a8813265ee53448eecb45&presence=away&pretty=1";
} else {
output.body = "slack.com/api/users.setPresence?token=xoxp-16811800865-542295231655-683636284785-e48d49a53d1a8813265ee53448eecb45&presence=auto&pretty=1";
}
So essentially this node retrieves the name of the event that's just been executed, checks it against a condition, and sets output.body accordingly.
Then our configuration for the update state node is as such :

Then depending on the url the output.body variable object is set to , two different responses are triggered.
Scenario 2:
- Timer preconditioned to trigger at a certain time / every specified number of minutes
- HTTP Service node to make api requests
- Run Function node to parse the resulting response from the previous node
- Update State node to update the state to the value needed from the api call

So as you can see from looking at the output of the HTTP Service node in the test flow panel, the response is returned as a JSON String which will need to parsed in order to extract the data.

For this reason, we need a run function to parse input.body variable and access the value we need / want through the use of dot notation.
So our code in the run function will be set up as follows :
var response = JSON.parse(input.body);
var precipitation = response.data[0].precip;
var deviceState = "";
deviceState = precipitation;
output.body = deviceState;
This code also passes the resulting value to the output.body variable object for use in the following node.
This is the case in this configuration for the following update state node

Scenario 3:
Another scenario you might have is when you want to set up a counter on the device's state and add 1 to it every time an event is triggered (eg. visitor counter at a stand from when button is pressed).
In this case, you can access the 'device' object to get the Device's state.
- You would have an event
- Run function to get the device's state and add 1 to it
- Update that state again

The code in the run function would look like this :
var deviceState = device.state.eventNum;
deviceState += 1;
output.body = deviceState;
As you can see from the code , the method / object we use to get the state's value is housed inside in the device.state object variable and from this then to get a specific state of the device we specify this by using dot notation and adding the key of the state that we want to obtain.
Note: Test flow's panel uses a simulated device and therefore not a real one so you won't be able to visibly see the device's state in the device.state variable object in the output of the node when running a test.
From here , the update state node is configured as above with the output.body variable object forming the input.body variable object for the following node (eg. Update State )
