Tutorial: IoT Alert Pipeline
Build an end-to-end pipeline that reads sensor data from an ESP32, sends it through Contact, analyzes it with AI in Strand, and delivers alerts to Slack.
ESP32 → Contact (MQTT) → Strand Workflow → AI Analysis → Slack Alert
Time: ~30 minutes Difficulty: Beginner You'll need: An ESP32 board, a Slack workspace, and an OpenAI or Anthropic API key
---
Step 1: Set Up Contact#
Create a Service#
In the Contact dashboard, go to Services and create a new service:
- Name:
sensor-monitoring - Validation Rules: (optional) Add a rule to check
temperatureis a number
Create an Entity#
Go to Entities → Create Entity:
- Name:
esp32-sensor - Service:
sensor-monitoring
Turn on MQTT for the entity. Its key is shown once, at creation; the next step puts it on the board for you, so you only need to copy it if you plan to send test messages with curl.
Create an API Key#
Go to Access Control → API Keys and create a key for esp32-sensor with the entity:WriteMessages permission. You'll use this in Strand to link the two platforms.
---
Step 2: Program the ESP32#
Open the entity in the Contact dashboard, switch to the Device tab, plug the board into USB and click Connect, then Provision. That installs the Tendrl client and writes the entity's key, your Wi-Fi and the server address to the board, so your code contains no credentials. See Provisioning.
Then save this as main.py on the board (the Device tab has a file editor). It reads a temperature sensor and publishes to Contact every 30 seconds:
import time
import machine
from tendrl import Client
# ADC pin for temperature sensor (adjust for your hardware)
adc = machine.ADC(machine.Pin(34))
adc.atten(machine.ADC.ATTN_11DB)
def read_temperature():
"""Convert ADC reading to Celsius (adjust for your sensor)."""
raw = adc.read()
voltage = raw * 3.3 / 4095
return round(voltage * 100, 1) # LM35 formula
client = Client(debug=True) # reads the key and Wi-Fi written by provisioning
client.start() # connects, then returns
while True:
temp = read_temperature()
client.publish({"temperature": temp, "unit": "celsius", "device": "esp32-sensor"}, tags=["sensor"])
print(f"Sent: {temp}°C")
time.sleep(30)
The tags matter: a message with no tags is stored and starts nothing. Step 4 gives the workflow the same tag.
Don't have an ESP32? You can simulate messages with curl:
curl -X POST https://app.tendrl.com/api/entities/message \
-H "Authorization: Bearer YOUR_ENTITY_KEY" \
-H "Content-Type: application/json" \
-d '{
"msg_type": "publish",
"data": {
"temperature": 28.5,
"unit": "celsius",
"device": "esp32-sensor"
},
"context": {"tags": ["sensor"]}
}'
---
Step 3: Set Up Strand Connectors#
You need three connectors. Go to the Strand Connectors page and create each one:
Slack Connector#
| Field | Value |
|---|---|
| Type | Slack |
| Name | My Slack |
| Bot Token | Your Slack bot token (starts with xoxb-) |
| Default Channel | #sensor-alerts |
To get a Slack bot token: go to api.slack.com/apps, create an app, add chat:write and chat:write.public scopes under OAuth & Permissions, then install to your workspace. Copy the Bot User OAuth Token.
AI Connector (OpenAI or Anthropic)#
| Field | Value |
|---|---|
| Type | OpenAI (or Anthropic) |
| Name | My AI |
| API Key | Your OpenAI/Anthropic API key |
Contact Message Connector#
| Field | Value |
|---|---|
| Type | Contact Entity Message |
| Name | Contact Sensor |
| API Key | The entity API key from Step 1 |
---
Step 4: Create the Strand Workflow#
Go to Flows → New Workflow and name it Sensor Alert Pipeline.
4a. Add the Trigger#
Your workflow receives events from Contact by tag matching. In the workflow settings:
- Turn on Expose to Contact.
- Give the workflow one or more trigger tags — for example
sensorandtemperature.
A Contact message starts the workflow when every tag on the message is one of the workflow's tags. A message tagged sensor starts a workflow tagged sensor and temperature; a message tagged sensor and humidity does not. A message gets its tags either from the device, which sends them with each reading, or from a validation rule in Contact, which adds its action's tags when the rule fails. A message with no tags starts nothing. No per-workflow wiring.
To trigger the same workflow from your own code instead of a Contact message, call it directly over HTTP — see Trigger a workflow over HTTP.
4b. Add a Python Snippet Node#
Drag a Python Snippet node onto the canvas. This extracts the sensor data from Contact's message format:
# Sensor data from the Contact message
sensor_data = payload.get("data", {})
temperature = sensor_data.get("temperature", 0)
device = sensor_data.get("device", "unknown")
# Determine alert level
if temperature > 35:
alert_level = "critical"
elif temperature > 30:
alert_level = "warning"
else:
alert_level = "normal"
return {
"temperature": temperature,
"device": device,
"alert_level": alert_level,
"needs_analysis": alert_level != "normal"
}
4c. Add a Logic Node (Filter)#
Add a Logic node set to Filter mode. Connect it to the Python Snippet.
Filter code: return payload.get('needs_analysis', False)
Filter mode runs Python, not a Jinja expression, and the upstream node's output arrives as payload.
This ensures we only analyze and alert on abnormal readings, saving AI API costs.
4d. Add an AI Connector Node#
Drag a Connector node and select your AI connector. Connect it after the Logic node.
Operation: chat
System Prompt:
You are an IoT monitoring assistant. Analyze sensor readings and provide a brief assessment (2-3 sentences) including: what the reading means, potential causes, and recommended action.
User Message:
Device: {{ steps.python_snippet.output_payload.device }}
Temperature: {{ steps.python_snippet.output_payload.temperature }}°C
Alert Level: {{ steps.python_snippet.output_payload.alert_level }}
4e. Add a Slack Connector Node#
Drag another Connector node and select your Slack connector. Connect it after the AI node.
Operation: send_message
Message Text:
🌡️ *Sensor Alert: {{ steps.python_snippet.output_payload.alert_level | upper }}*
*Device:* {{ steps.python_snippet.output_payload.device }}
*Temperature:* {{ steps.python_snippet.output_payload.temperature }}°C
*AI Analysis:*
{{ steps.ai_node.output_payload.response }}
Final Workflow#
Your workflow should look like this:
[Contact Trigger] → [Python Snippet] → [Logic Filter] → [AI Analysis] → [Slack Alert]
Click Save. There is no separate deploy step: the next trigger uses the version you just saved.
---
Step 5: Test It#
Option A: Send a Test Message from Contact#
curl -X POST https://app.tendrl.com/api/entities/message \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"msg_type": "publish",
"data": {
"temperature": 36.2,
"unit": "celsius",
"device": "esp32-sensor"
},
"context": {"tags": ["sensor"]}
}'
Option B: Trigger Strand Directly#
Call the workflow by its ID (from the editor URL) — no Contact message needed. The inner data object matches the message shape the Python snippet reads with payload.get("data"):
curl -X POST https://app.tendrl.com/strand/api/workflows/YOUR_WORKFLOW_ID/run \
-H "Authorization: Bearer YOUR_STRAND_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"data": {
"data": {
"temperature": 36.2,
"unit": "celsius",
"device": "esp32-sensor"
}
}
}'
Check your #sensor-alerts Slack channel; you should see an alert with AI analysis within a few seconds.
---
What's Happening#
- ESP32 reads sensor data and publishes it to Contact
- Contact receives the message, validates it against the service rules, and starts the Strand workflow whose tags cover the message's tags
- Strand runs the workflow:
- Extracts temperature and determines alert level
- Filters out normal readings
- Sends abnormal readings to AI for analysis
- Posts the analysis to Slack
- Slack delivers the alert to your team
The entire pipeline runs in seconds with zero custom backend code.
---
Next Steps#
- Add more sensors: Create additional Contact entities and feed them into the same workflow
- Add email alerts: Use the Email connector for critical alerts
- Store readings: Use a Python Snippet to log data to a database
- Add thresholds: Use workflow variables to make temperature thresholds configurable
- Dashboard: Build a simple web app using Contact's REST API to display historical readings
Tendrl