Now that the data has been received, you can implement functionality to act on any outliers in the data.
In this how-to, you will learn how to use business logic to create alerts for when the cargo exceeds the temperature threshold.
This how-to will teach you how to do the following:
- Extend the OnMessage flow to create alerts
- Improve the alert mechanism
Before starting with this how-to, make sure you have completed the following prerequisite:
- Use the previous how-to in this series: How to Build an IoT App Step 4: Add Another Data Dimension
3 Creating Alerts (Step 5)
3.1 Extending the OnMessage Flow to Create Alerts (Step 5.1)
If the temperature threshold has been exceeded, an alert needs to be created. To extend the OnMessage microflow to create alerts, follow these steps:
- Return to the IVK_Subscribe microflow and double-click the Data - Subscribe to MQTT topic activity.
- Change the On message microflow to IVK_OnMessage_HandleData_CreateTemperatureAlert (located in the Step 5.1 folder)
- Open the IVK_OnMessage_HandleData_CreateTemperatureAlert microflow.
- Add a Retrieve activity and keep the Source as By association.
- To see what the threshold is for the shipment, the CargoType object needs to be retrieved. So, for the Association, select Shipment > Shipment_CargoType and click OK.
- Insert an Exclusive split to check that the object is available (as in, not empty). In the Expression editor, add
$CargoType != empty.
- Insert another Exclusive split that will check that the temperature threshold exists. In the Expression editor, add
- Set the condition value on this flow as true.
- Insert one more Exclusive split that will check if the temperature is higher than the threshold. In the Expression editor, add
$SensorData/AmbientTemp > $CargoType/TemperatureThreshold.
- Set the condition value on this flow as true.
- Add a Create object activity on this flow.
- Select Alert for the Entity and set Commit to Yes.
- Click New to add a new attribute. Set the Member to Message and set the Value as
'Temperature is too high'.
- Add another attribute. Set the Member to AlertType and click Generate to verify that the expression value is set to Constant > Temp.
- Add one more attribute. Set the Member to Tutorial_Workspace.Alert_Shipment. Set the Value as
- You still need to handle the outgoing sequence flows from the exlusive splits, so insert a Merge after the NewAlert entity.
- Drag the false flow from each exclusive split to the merge.
- Save the changes, click Run Locally, then click View App.
- On the shipment overview page of the app, select Container Bananas, then click Subscribe. The data will now come into the indicators.
- In order to generate alerts, you need to make sure that the temperature threshold (which you can configure on the CargoType edit page) is lower than the temperature data being pushed to the app. The temperature threshold was set to 23 degrees in this example, so when the temperature goes above 23, you will get an alert. Click the alerts icon in the upper-right side of the screen. This will open the Current alerts panel, where you will see the newest alert.
3.2 Improving the Alert Mechanism (Step 5.2)
You get an alert every time the temperature exceeds the threshold, which is unnecessary after the initial alert. You need an indicator on a shipment that an alert is active, so now you are going to build some logic that prevents these repetitive alert messages.
To improve the alert mechanism, follow these steps:
- In the Modeler, open the Domain Model and then open the Shipment entity.
- Click New to add a new attribute. Name this attribute IsAlertedForTemperature and set Type to Boolean.
- Open the IVK_OnMessage_HandleData_CreateTemperatureAlert microflow and insert an Exclusive split before the NewAlert entity.
- Set the Expression to
$Shipment/IsAlertedForTemperature. This means that if an alert has already been created, an extra alert won’t be created.
- Drag a flow from this exclusive split to the merge (located after the NewAlert object), and set the Condition value on this flow to true.
- Add a Change object activity between the NewAlert object and the merge, and set the input variable to Shipment.
- Click New to add a new change item, which will be IsAlertedForTemperature. Set the Value to
- To change the message, open the NewAlert object and click the Message attribute. Change the message to
'Temperature is too high: ' + $SensorData/AmbientTemp. Click OK.
- Save the changes, click Run Locally, and click View App.
- On the shipment overview page of the app, select Container Bananas, then click Subscribe.
- Open the Current alerts panel by clicking the exclamation mark icon in the top right of the page. You will see a message like this: Temperature is too high: 15.
Excellent! Now that you have configured the alert mechanism, you can move on to the final how-to in this tutorial: How to Build an IoT App Step 6: Expand the IoT App Tracking.
4 Related Content
- How to Build an IoT App
- How to Build an IoT App Step 1: Connect App Pages
- How to Build an IoT App Step 2: Configure the Sensor Data Subscription
- How to Build an IoT App Step 3: Handle the Sensor Data
- How to Build an IoT App Step 4: Add Another Data Dimension
- How to Build an IoT App Step 6: Expand the IoT App Tracking