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:

  1. Publish the data in the action Data field into a channel.
  2. 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.

  1. In the Test1 Thing, we create a new Threshold trigger called Pub Data.
  1. The channel name is building.
  2. We will use a Data field called temperature as the data source for the trigger.
  3. Rule Type is Threholds.
  4. The Threshold value is 40.
  1. 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. Pub message
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. Expand into channels
3. Save the rule, then save the trigger and save the device. The resulting rule and trigger are as follows: Pub Data RulePub Data Trigger
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. Pub DataChannel list1
5. Publish {“temperature”:50,“humidity”:58,“CO2”:550}. to cause the trigger to fire execute. Pub Data 2
6. Open the channel list under Data and verify that the channels pub_message and new_expanded_channel are created. Channel List1
7. Verify that the channel pub_message has 1 data record indicating the values that were published in channel building Pub Data channel 1Pub Data channel 2
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. Expanded channel 1Expanded channel 2
Still need help? Get in touch!
Last updated on 6th May 2021