# Rule Engine

### What is the Rule Engine? <a href="#what-is-thingsboard-rule-engine" id="what-is-thingsboard-rule-engine"></a>

Rule Engine is an easy to use framework for building event-based workflows. There are 3 main components:

* **Message** - any incoming event. It can be an incoming data from devices, device life-cycle event, REST API event, RPC request, etc.
* **Rule Node** - a function that is executed on an incoming message. There are many different Node types that can filter, transform or execute some action on incoming Message.
* **Rule Chain** - nodes are connected with each other with relations, so the outbound message from rule node is sent to next connected rule nodes.

### Typical Use Cases <a href="#typical-use-cases" id="typical-use-cases"></a>

Tesenso IoT Cloud Rule Engine is a highly customizable framework for complex event processing. Here are some common use cases that one can configure via Tesenso IoT Cloud Rule Chains:

* Data validation and modification for incoming telemetry or attributes before saving to the database.
* Copy telemetry or attributes from devices to related assets so you can aggregate telemetry. For example data from multiple devices can be aggregated in related Asset.
* Create/Update/Clear alarms based on defined conditions.
* Trigger actions based on device life-cycle events. For example, create alerts if Device is Online/Offline.
* Load additional data required for processing. For example, load temperature threshold value for a device that is defined in Device’s Customer or Tenant attribute.
* Trigger REST API calls to external systems.
* Send emails when complex event occurs and use attributes of other entities inside Email Template.
* Take into account User preferences during event processing.
* Make RPC calls based on defined condition.
* Integrate with external pipelines like Kafka, Spark, AWS services, etc.

### Hello-World Example <a href="#hello-world-example" id="hello-world-example"></a>

Let’s assume your device is using DHT22 sensor to collect and push temperature to the Tesenso IoT Cloud. DHT22 sensor can measure temperature from -40°C to +80°C.

In this tutorial we will configure Tesenso IoT Cloud Rule Engine to store all temperature within -40 to 80°C range and log all other readings to the system log.

**Adding temperature validation node**

In Tesenso IoT Cloud UI go to **Rule Chains** section and open **Root Rule Chain**.

Drag and Drop [**Script Filter**](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/filter-rule-nodes/script-filter) rule node to the chain. Node configuration window will be opened. We will use this script for data validation:

```
return typeof msg.temperature === 'undefined'         || (msg.temperature >= -40 && msg.temperature <= 80);
```

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FeVg1NHeD8KCPE9xEtEFX%2Fimage.png?alt=media\&token=5e5e21f7-7a6d-4878-8a6d-143e08370ee6)

If temperature property not defined or temperature is valid - script will return **True**, otherwise it will return **False**. If script returns **True** incoming message will be routed to the next nodes that are connected with **True** relation.

Now we want that all **telemetry requests** pass through this validation script. We need to remove the existing **Post Telemetry** relation between **Message Type Switch** node and **Save Telemetry** node:

![image](https://thingsboard.io/images/user-guide/rule-engine-2-0/tutorials/getting-started/remove-relation.png)

And connect [**Message Type Switch**](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/filter-rule-nodes/message-type-switch) node with [**Script Filter**](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/filter-rule-nodes/script-filter) node using **Post Telemetry** relation:

<div align="left"><img src="https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FTMQ9L0iGdniVaHmOjvqP%2Fimage.png?alt=media&#x26;token=9fa3f80b-f85b-48b1-9ad5-dbf38d145c90" alt=""></div>

![image](https://thingsboard.io/images/user-guide/rule-engine-2-0/tutorials/getting-started/connect-script.png)

Next, we need to connect **Script Filter** node with **Save Telemetry** node using **True** relation. So all valid telemetry will be saved:

<div align="left"><img src="https://thingsboard.io/images/user-guide/rule-engine-2-0/tutorials/getting-started/script-to-save.png" alt="image"></div>

Also, we will connect **Script Filter** node with **Log Other** node using **False** relation. So that all not valid telemetry will be logged in the system log:

![image](https://thingsboard.io/images/user-guide/rule-engine-2-0/tutorials/getting-started/false-log.png)

Press Save button to apply changes.

**Validate results**

For validating results we will need to create Device and submit telemetry to the Tesenso IoT Cloud. So go to **Devices** section and create new Device:

<div align="left"><img src="https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FHwJdsOzVxgpAdJJohxdf%2Fimage.png?alt=media&#x26;token=defa45af-9043-402c-aac4-3e569237ed57" alt=""></div>

For posting device telemetry we will use Rest API. To do this this we will need to copy device access token from the device **Thermostat Home**.

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FENCW7R0oIlrabaZb3UNJ%2Fimage.png?alt=media\&token=2f89a60c-16d0-44e1-8fad-590be6761dd9)

Lets post temperature = 99. We will see that telemetry **was not** added in Device **Latest Telemetry** section:

```
curl -v -X POST -d '{"temperature":99}' http://tesenso.io:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"
```

**\*you need to replace $ACCESS\_TOKEN with actual device token**

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FXz3m5DrAuMHfUe8HxVUw%2Fimage.png?alt=media\&token=9a4d2dad-dc60-4011-bfa9-8a58bf255b83)

Lets post temperature = 24. We will see that telemetry was saved successfully.

```
curl -v -X POST -d '{"temperature":24}' http://tesenso.io:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"
```

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FVWP6bIYj96YNpfBZ0Dgl%2Fimage.png?alt=media\&token=97432d7b-0834-49f2-a390-86c2d1dacc40)

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2Ft7SzE2kful3ibdHWyWOh%2Fimage.png?alt=media\&token=ac5b3456-f0d9-41bc-861e-6dd8e797aabc)
