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
- Device(s) connected to Losant.
- Device(s) reporting state data at regular intervals.
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
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:
Create a workflow within your application.
From your blank workflow canvas, drag a Device: State Trigger onto the canvas.
In the “Devices” section, select the device that will report the data converted within this workflow.
In the Filter Attributes section of the node configuration, select “Any but the following” from the Allowed Attributes drop down menu.
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 thecelsius
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.
- Drag a Math Node onto the canvas and connect it to the Device: State Trigger. The Math Node will contain the formula to calculate the temperature in Celsius using the reported temperature in Fahrenheit.
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:
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((
-32) * (5/9))
More information on accessing payload data and available expression support can be found in the Accessing Payload Data Documentation.
- Complete the configuration of the Math Node by specifying a payload path of
data.celsius
to store the result of your expression.
- Drag a Device: State Output Node onto the canvas, and connect it to the Math Node.
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.
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
.
- 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, 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.
This attribute data can now be utilized throughout the Losant platform, including use in:
- Generating Alerts via SMS or Email.
- Event logging on the result of this transformed attribute.
- Data Visualization via Dashboards using a Time Series Block or a Gauge Block.
Was this page helpful?
Still looking for help? You can also search the Losant Forums or submit your question there.