How to Parse Scripts Using the Payload Parser

The payload parser tool allows users to parse data sent to their devices and format raw packages into JSON. It has many applications, including the ability to trigger actions. Before starting you will need to create a device. This tutorial uses a custom https device because, unlike most devices available, custom https devices are not initialized with their own parser.

Initializing the Temperature Variable

To initialize a variable for your device, navigate to the the device emulator and send the default emulation. This will add the variable “temperature” to the device’s bucket, which will be used later.

Creating the Script

The example used here is based off the Payload Parser example that can be accessed in the documentation. First, change the emulator variable from “temperature” to “toastValues” and change the value to have 3 number separated by spaces as shown below.

Next, open the Payload Parser. Delete the default parser script and add the following script.

const rawValues = payload.find(item => item.variable === 'toastValues');
if (rawValues) {
  const valuesString = rawValues.value;
  const splitValues = valuesString.split(' ');
  const successCode = splitValues[0];
  const tempF = splitValues[1];
  const errorCode = splitValues[2];
  payload.push({ "variable": "successCode", "value": successCode });
  payload.push({ "variable": "temperature", "value": tempF, "unit": "F" });
  if (errorCode !== '00') {
    payload.push({ "variable": "error", "value": errorCode });

Note: The same buttons will be visible to all users that access your application. You can’t set buttons to be visualized only for certain users.
Note: In this script, rawValues stores whatever data is sent to the device. The if statement is true whenever rawValues has data. In other words, this parser only runs when data is sent to the device. The constant variable valuesString is assigned to whatever string was sent under the name “value”, which in this case is “01 350 00”, and the variable splitValues seperates the valuesString into an array of substrings that can then be referenced individually. successCode is thus assigned to the first index of the array, tempF to the second, and errorCode to the third.

Save the parser script and return to the device emulator. Send the package with toastValues as described earlier. To see more about the package your device receives, turn on the Live Inspector and resend the package.

Debugging the Parser Script

One tool that can be used to debug parser scripts is console.log, which will print to the Live Inspector. In this example we can double check that tempF is referencing the intended value. It should be assigned to the value of 350 based on the package we sent from the emulator.

Note: Make sure that the buttons you have added to the sidebar are accessible to all users, by creating Policies in the Access Management section. Note: to see the message displayed by cosole.log resend the package with the live inspector turned on

Trigger an Action From the Payload Parser

Go to Actions and click “Add Action”. Name your action and select “Variable” as the trigger type. Choose the type of notification that you want to receive, fill in the necessary inputs, and click “Create My Action.”

Next, select your device, the variable that will trigger the action (temperature), and the value of the variable at which the action should be triggered.

Now that the action has been created, we need to find a way to trigger it. Currently, toastValues is the only variable being sent in the emulator and temperature was initialized with a value of 75, which is not high enough to trigger the action. We can use the parser script to trigger the action if we assign the temperature variable to tempF, which is 350.

Note: Make sure that the buttons you have added to the sidebar are accessible to all users, by creating Policies in the Access Management section. Note: make sure to assign the temperature to tempF after tempF has been initialized, otherwise you will receive a parsing error because you would be referencing a variable that has yet to been created.

Now, if a package is sent to the device with a value higher than 80 for tempF, the action will be triggered. Resend your package and check that you receive the given notification.

Tip: Watch the video for how to do this here