> For the complete documentation index, see [llms.txt](https://docs.akenza.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akenza.io/device-management/rule-engine.md).

# Rules

How rules apply business logic to incoming device data to send alerts, call external systems, and send downlinks.

**Rules** apply business logic to incoming device data. When a condition is met, a rule triggers an action, such as sending an alert, calling an external system, or sending a downlink to a device.

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2FSTQYKST0W2G56XAtf3XA%2Fscreely-1670229335843.png?alt=media&amp;token=f386ccf2-45d1-4682-835e-25208656330d" alt="A rule with a device input, a comparison logic block, and two actions"><figcaption><p>A rule linking an input, a logic block, and two actions</p></figcaption></figure>

## Common use cases

### Logic

<table><thead><tr><th width="230">Use case</th><th>Example</th></tr></thead><tbody><tr><td><strong>No-code business logic</strong></td><td>Compare a value to a threshold or to the last sample of another device with a <a href="/device-management/rule-engine/logic-blocks/rule-logic.md">Comparison</a> logic block, without writing code.</td></tr><tr><td><strong>Combine several inputs</strong></td><td>Sum the people counts of several occupancy sensors with a <a href="/device-management/rule-engine/logic-blocks/custom-logic.md">Custom Logic</a> block, and store the total on a separate device with the <a href="/device-management/rule-engine/rule-actions.md#akenza-db-action">akenza DB action</a>.</td></tr><tr><td><strong>Scheduled checks</strong></td><td>Run a rule every few minutes, every few hours, or at a fixed time with a <a href="/device-management/rule-engine/timed-rules.md">timer</a>, instead of on every new sample.</td></tr><tr><td><strong>Geofencing</strong></td><td>Trigger an action when a device enters or leaves a defined area with the Geofence logic block.</td></tr></tbody></table>

### Actions

<table><thead><tr><th width="230">Use case</th><th>Example</th></tr></thead><tbody><tr><td><strong>Alerts</strong></td><td>Notify users by <a href="/device-management/your-data-flow/connectors/notifications/email.md">email</a>, <a href="/device-management/your-data-flow/connectors/notifications/sms.md">SMS</a>, <a href="/device-management/your-data-flow/connectors/notifications/microsoft-teams.md">Microsoft Teams</a>, or <a href="/device-management/your-data-flow/connectors/notifications/slack.md">Slack</a> when a value crosses a threshold.</td></tr><tr><td><strong>Device commands</strong></td><td>Configure or control a device with a <a href="/device-management/rule-engine/rule-actions/downlink.md">downlink</a> when a condition is met.</td></tr><tr><td><strong>External systems</strong></td><td>Call a REST API with a <a href="/device-management/your-data-flow/connectors/streaming/webhook.md">webhook</a>.</td></tr><tr><td><strong>Data management</strong></td><td>Store or forward data only when it matches certain business logic.</td></tr></tbody></table>

## How a rule works

A rule consists of three parts, which you link to each other in the rule editor:

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><i class="fa-microchip">:microchip:</i></td><td><strong>Input</strong></td><td>The devices, tags, or tag combinations whose data samples trigger the rule.</td><td><a href="/device-management/rule-engine/rule-input.md">Input</a></td></tr><tr><td><i class="fa-code-branch">:code-branch:</i></td><td><strong>Logic</strong></td><td>The condition applied to the data: a comparison, or a custom script.</td><td><a href="/device-management/rule-engine/logic-blocks.md">Logic blocks</a></td></tr><tr><td><i class="fa-bolt">:bolt:</i></td><td><strong>Action</strong></td><td>What happens when the condition is true.</td><td><a href="/device-management/rule-engine/rule-actions.md">Rule Actions</a></td></tr></tbody></table>

To link two parts, click the dot on one block. The possible destinations are shown, and a blue line connects the blocks once they are linked.

### Input

An input is a **device** that has sent a data sample to akenza. Each new sample triggers the logic the input is linked to. A rule can have several inputs.

You can also select a [**tag**](/device-management/managing-a-workspace/tags.md) as input. The rule then runs for every device with that tag. A **tag combination** selects the devices that match a logic expression of several tags.

### Logic

The logic defines the condition that is evaluated when the rule is triggered.

* The [**Comparison**](/device-management/rule-engine/logic-blocks/rule-logic.md) logic block compares device data to a constant value, to the last sample of the same device, or to the last sample of another device. You can add more data sources and conditions to extend it.
* A [**Custom Logic**](/device-management/rule-engine/logic-blocks/custom-logic.md) block runs a JavaScript script for logic that a comparison cannot express. Custom logic blocks are managed at organization level and are available in all workspaces.

### Action

An action runs when the logic evaluates to true. Rule actions use the same [output connectors](/device-management/your-data-flow/connectors.md) as data flows, plus [downlinks](/device-management/rule-engine/rule-actions/downlink.md). Once a rule is saved, its actions can be reused in other rules.

{% hint style="warning" %}
A **deactivated** rule is not triggered by new data.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akenza.io/device-management/rule-engine.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
