Publish Data Action
One of the actions that can be programmed to be executed by a trigger is to publish data. This is a powerful action that can be used to send a data or a command to one or more devices by publishing the data or command into a channel. This trigger enables the implementation of nested triggers, since data published by a trigger could be used to create an event that causes another trigger to execute.
The platform can be programmed to publish data:
- when a threshold is crossed.
- when a device enters, exists, or remains inside or outside of a location.
- when a device changes state, or remains in a particular state the platform.
- when a device changes its online status
The data published can be used to initiate another action; therefore, allowing the programming of nested triggers.
A Publish Data action can have one of two consequences:
- Publish the data in the action Data field into a channel.
- Use the Data Fields of a structured JSON to create sub-channels of a selected channel, and then publish the Data Field Values into those sub-channels.
Example
Here is an example that shows the effects of both actions on a single threshold trigger.
- In the Test1 Thing, we create a new Threshold trigger called Pub Data.
- The channel name is building.
- We will use a Data field called temperature as the data source for the trigger.
- Rule Type is Threholds.
- The Threshold value is 40.
- We create two actions when the value of the data published exceeds the threshold as shown below:
1. The first action is called Pub msg, and it simply publishes a JSON string into a channel called pub_message. Notice that \n is used to create a new line in JSON. | |
2. The second action is called Expand into channels and, if the data causing the trigger to be fired is a structured JSON, it will create sub-channels with the Data Field names into a channel called new_expanded_channel. Moreover, it will publish the values of the Data Fields into those sub-channels. | |
3. Save the rule, then save the trigger and save the device. The resulting rule and trigger are as follows: | |
4. Publish data to setup the trigger to execute on the next data publication. We publish a JSON with three data fields (temperature, humidity and CO2) in the channel building such that the temperature data field has a value below the threshold 40: {“temperature”:30,“humidity”:55,“CO2”:450}. Since the trigger is fired only if the threshold is exceeded, no action will be executed. There should be no pub_message and new_expanded_channel channels yet. | |
5. Publish {“temperature”:50,“humidity”:58,“CO2”:550}. to cause the trigger to fire execute. | |
6. Open the channel list under Data and verify that the channels pub_message and new_expanded_channel are created. | |
7. Verify that the channel pub_message has 1 data record indicating the values that were published in channel building | |
8. Verify that the channel new_expanded_channel has three sub-channels: temperature, humidity, C02 and that each contains its respective published data record. Only the last record is listed since it is the one that executed the expansion and creation of the new sub-channels. |