# Advanced Rule Engine E-Mail and Webhook Templates

## Introduction

The Rule Engine in our Datacake IoT platform is a powerful feature that enables you to automate actions and send notifications based on data from your IoT devices. As of July 4, 2023, our Rule Engine supports the inclusion of measurement timestamps in notifications. This document guides you through the utilization of the Django Template Language within the Rule Engine for crafting highly informative and customized notification content.

### **Why Use Template Language?**

When sending notifications, whether it's an email, SMS, or webhook, it’s often necessary to include data from sensors, and timestamps, and sometimes perform some conditional logic. This might include formatting dates, displaying values, or sending different messages based on sensor readings. The Django Template Language allows for dynamic inclusion and manipulation of content, based on the data and logic you specify.

### **What Can You Do with Template Language in Notifications?**

* **Dynamic Content**: Include real-time sensor data and timestamps in your notifications.
* **Formatting**: Customize the formatting of timestamps to make them more readable or to conform to specific standards.
* **Conditional Logic**: Use if-else statements to send different messages depending on the data.
* **Loops**: Loop through lists of data, such as multiple sensor readings.
* **Combining Information**: Craft comprehensive messages by combining multiple pieces of information.
* **Adding Hyperlinks**: Include links to dashboards or additional resources.

### **Getting Started**

This document contains examples and syntax that will help you get started with using the Django Template Language in Rule Engine notifications. From simply displaying sensor data to more advanced conditional logic, these examples will guide you through creating informative and dynamic notifications.

Please proceed through the examples to understand the various features and possibilities. Keep in mind that while this documentation provides a strong foundation, the Django Template Language is very versatile and you may find even more creative ways to utilize it in your notifications.

## **Including Sensor Data**

Include the value of a sensor measurement in your notification. When writing templates for webhooks or emails, it’s important to use the correct path to access your device data. Some ways of referencing the measured values of your devices is:

```html
Temperature: {{ triggering_device["values"]["TEMPERATURE"] }}°C
```

But it could also be:

```
Temperature: {{ triggering_device["measurements"]["TEMPERATURE"] }}°C
```

{% hint style="success" %}
**💡 Tip: Use `debug_keys` to Inspect Your Data**

To figure out the correct paths for your template, add a temporary key to your payload that prints the entire `triggering_device` object or a subset like `measurements`.\
Add `"debug_keys": "{{ triggering_device }}"` to your webhook to include the full device object in your outgoing webhook or email, so you can visually confirm the structure and keys available at runtime.

Once you’ve identified the correct paths, you can safely remove the debug line.
{% endhint %}

## **Including Measurement Timestamp in UTC**

Include the timestamp of the measurement in UTC ISO format.

```html
Measurement Time (UTC): {{ triggering_device["timestamps"]["TEMPERATURE"] }}
```

## **Including Measurement Timestamp in Local Timezone**

Include the timestamp of the measurement in the rule's timezone using the `datetime` filter.

```html
Measurement Time (Local): {{ triggering_device["timestamps"]["TEMPERATURE"] | datetime }}
```

{% hint style="warning" %}
Timestamp is provided in the time zone you selected on your rule! Make sure to select the correct one.
{% endhint %}

## **Custom Date Formatting**

Customize the date format using strftime syntax.

```html
Measurement Time (Formatted): {{ triggering_device["timestamps"]["TEMPERATURE"] | datetime("%A, %B %d, %Y %I:%M %p") }}
```

## **Combining Information**

Combine multiple pieces of information into a single message.

```html
Temperature is {{ triggering_device["values"]["TEMPERATURE"] }}°C, measured at {{ triggering_device["timestamps"]["TEMPERATURE"] | datetime }} ({{ triggering_device['timestamps']['TEMPERATURE']|date:"c"|timesince }} ago).
```

## **Including Hyperlinks**

Include a hyperlink, possibly to a dashboard or additional resources.

```html
For more details, visit the <a href="https://example.com/dashboard">dashboard</a>.
```

Note: You can combine these examples and syntax to create complex and informative notifications according to your requirements.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datacake.de/portal/rule-engine/new-rule-engine/advanced-rule-engine-e-mail-and-webhook-templates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
