How To Manipulate and Convert Device Data Using a Workflow

Once you have device state reporting to Losant, the next step for any IoT application is to do something with that data—visualize, process, analyze, convert, or manipulate it. Losant Dashboards have the ability to display raw device data in a variety of ways, but with Losant Workflows you can also process the data to garner deeper insights.

This guide will walk through an example of how you can manipulate data using the Losant Visual Workflow Engine and report state for further use throughout the Losant platform.

Prerequisites

The following steps explain how to build a workflow to convert a temperature reading from degrees Fahrenheit to degrees Celsius.

Example Device

In this example, we have a device that has been configured with two number type attributes:

  • fahrenheit
  • celsius

Device Attributes

Tip: You can use the Device Simulator to simulate a device and data. For example, you can use the simulator to simulate fahrenheit data.

Building a Temperature Conversion Workflow

In this example, you have a device reporting fahrenheit. Using a workflow, you can convert the data and report the corresponding celsius value.

To build the conversion workflow, follow these steps:

  1. Create a workflow within your application.

  2. From your blank workflow canvas, drag a Device: State Trigger Node onto the canvas.

Device State Trigger

  1. In the “Devices” section, select the device that will report the data converted within this workflow.

  2. In the Filter Attributes section of the node configuration, select “Any but the following” from the Allowed Attributes drop down menu.

  3. Select celsius from the Select Attributes drop-down menu. When the device selected reports state to Losant, it will trigger the workflow when any attribute but the celsius attribute is reported. Using this filter allows the workflow to not invoke an infinite loop error by only triggering if certain attributes are included or not included.

Device State Configuration

  1. Drag a Math Node onto the canvas and connect it to the Device: State Trigger Node. The Math Node will contain the formula to calculate the temperature in Celsius using the reported temperature in Fahrenheit.

Math Node Configuration

The formula to convert Fahrenheit to Celsius is: (Temperature (F°) - 32) * (5/9)

When you report state, the Device State Node will be triggered and will place the data on the payload at data.fahrenheit. Now that it’s within the workflow engines payload:

Debug Node

Accessing values on the payload is done through the use of Handlebars templates. The value for the temperature in Fahrenheit in this example is found on the payload at the path data.fahrenheit, and thus the conversion formula to place into the expression area of the Math Node configuration is:

round(({{data.fahrenheit}}-32) * (5/9))

More information on accessing payload data and available expression support can be found in the Accessing Payload Data Documentation.

  1. Complete the configuration of the Math Node by specifying a payload path of data.celsius to store the result of your expression.

Math Node Expression

  1. Drag a Device: State Output Node onto the canvas, and connect it to the Math Node.

Device State Output Node

  1. In the ‘Device’ section of the Device: State Output Node configuration, specify the device that will have its state updated. This can be done be selecting the Device ID from the drop-down menu after selecting the “Select a specific device” radio button or by providing the payload path to the Device ID after selecting the “Use a Device ID specified on the current payload” radio button.

  2. Under the “State” section of the configuration window, set the value of the attribute celsius to the payload path where the stored result of the expression is located, data.celsius.

Device State Output Node Configuration

  1. Drag a Debug Node on to the canvas and connect it to the Device: State Output Node.

Now, your workflow is complete!

Avoiding Infinite Loops

There is a common pitfall to be aware of while building workflows. This workflow is triggered each time the device reports state. When the calculation is completed to convert Fahrenheit to Celsius, the workflow places the calculated value on the payload by using the Math Node and reports state using the Device State Output Node.

The use of the Device State is the same as the device again reporting state, which would trigger the workflow to run again, and for the conversion calculation to reoccur, resulting in an infinite loop error.

Through the use of the Filter Attributes configuration in the initial Device: State Trigger Node, the workflow checks whether a celsius value is reported. If the filter is satisfied, and there is no celsius value reported on the payload, the workflow is triggered and executes its conversion. If a celsius value is already reported on the payload, no further action is needed, and the workflow correctly stops, avoiding the infinite loop error.

What’s Next?

Upon completion of this workflow, the device within this application now has two temperature values stored within Losant’s time series database. Because these values exist on the payload and stored as state, both the Celsius and Fahrenheit temperature values can now be used throughout the Losant platform.

Losant Workflow Debug Panel

This attribute data can now be utilized throughout the Losant platform, including use in:

Was this page helpful?


Still looking for help? You can also search the Losant Forums or submit your question there.