Lemline documentation Help

IoT Workflow Examples

This document provides practical examples of workflows for Internet of Things (IoT) applications using Lemline. They manage the sensors of the tutorial shop's warehouse: device provisioning, telemetry processing, firmware updates, predictive maintenance and energy optimization.

Run the Examples

Complete the SQLite setup and run the commands from the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. Start the tutorial shop from the root of the Lemline source checkout. Its /devices routes hold three warehouse sensors; the shop page lists them under Devices.

docker compose -f examples/tutorial-services/compose.yaml up -d

Devices answer through events, which the tutorial publisher sends for you. The telemetry workflow is started by events, so it must be registered before the worker starts. Stop the setup worker with Ctrl+C, save the publisher below as tutorial-publisher.yaml and the telemetry workflow as device-telemetry.yaml, register both and start the worker again:

document: dsl: '1.0.3' namespace: examples name: tutorial-publisher version: '1.0.0' do: - publish: emit: event: with: type: ${ .type } source: https://events.example.com/tutorial data: ${ .data }
bin/lemline definition post -f tutorial-publisher.yaml bin/lemline definition post -f device-telemetry.yaml bin/lemline listen --debug

Run the other commands from a second terminal in the setup directory. Publish each device event only after the instance that waits for it has started waiting: an event only resumes the instances already listening.

Device Provisioning Workflow

This example provisions a new warehouse sensor: it registers it if the shop does not know it, obtains credentials, stores its configuration, asks the device to activate and waits for its confirmation.

document: dsl: '1.0.3' namespace: examples name: device-provisioning version: '1.0.0' input: schema: format: json document: type: object required: - deviceId - deviceType - location properties: deviceId: type: string deviceType: type: string enum: - sensor - gateway - controller location: type: object required: - siteId - zone do: - validateDevice: try: - checkDeviceExists: call: http with: method: get endpoint: ${ "http://127.0.0.1:8787/devices/\(.deviceId)" } catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/communication status: 404 as: notFoundError do: - createDeviceRecord: call: http with: method: post endpoint: http://127.0.0.1:8787/devices body: deviceId: ${ .deviceId } deviceType: ${ .deviceType } location: ${ .location } status: provisioning export: as: '${ $context + { deviceRegistry: . } }' - generateCredentials: try: - createCredentials: call: http with: method: post endpoint: ${ "http://127.0.0.1:8787/devices/\($workflow.input.deviceId)/credentials" } export: as: '${ $context + { credentials: . } }' catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/communication retry: delay: seconds: 5 limit: attempt: count: 3 - configureDevice: call: http with: method: patch endpoint: ${ "http://127.0.0.1:8787/devices/\($workflow.input.deviceId)" } body: config: reportingIntervalSeconds: 60 zone: ${ $workflow.input.location.zone } export: as: '${ $context + { deviceConfig: .config } }' - activateDevice: emit: event: with: type: com.example.iot.device.activation.requested source: https://events.example.com/warehouse data: deviceId: ${ $workflow.input.deviceId } config: ${ $context.deviceConfig } - waitForDeviceActivation: listen: to: one: with: type: com.example.iot.device.activated correlate: deviceId: from: ${ .deviceId } expect: ${ $workflow.input.deviceId } timeout: after: minutes: 15 - finalizeProvisioning: call: http with: method: patch endpoint: ${ "http://127.0.0.1:8787/devices/\($workflow.input.deviceId)" } body: status: active lastPing: ${ .[0].timestamp }

Save it as device-provisioning-workflow.yaml:

bin/lemline definition post -f device-provisioning-workflow.yaml bin/lemline instance start examples device-provisioning --input '{"deviceId":"sensor-4","deviceType":"sensor","location":{"siteId":"warehouse-1","zone":"office"}}'

The shop answers HTTP 404 for the unknown sensor-4, so the catch registers it. The instance then waits for the activation. Send it as the device would:

bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.iot.device.activated","data":{"deviceId":"sensor-4","timestamp":"2026-11-25T10:00:00Z"}}'

The instance completes, and the shop shows sensor-4 with status active, its configuration and the lastPing of the event.

Telemetry Processing Workflow

This workflow starts on each telemetry event received from a device and emits an alert event when the reported temperature is above 80.

document: dsl: '1.0.3' namespace: examples name: device-telemetry version: '1.0.0' schedule: on: one: with: type: com.example.device.telemetry.received input: from: ${ .[0] } do: - inspectReading: switch: - highTemperature: when: ${ .temperature > 80 } then: raiseAlert - other: then: end - raiseAlert: emit: event: with: type: com.example.device.temperature.high source: https://events.example.com/telemetry subject: ${ .deviceId } data: '${ { deviceId, temperature } }'

You registered it before starting the worker. Publish a normal and a high reading:

bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.device.telemetry.received","data":{"deviceId":"sensor-2","temperature":19}}' bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.device.telemetry.received","data":{"deviceId":"sensor-2","temperature":85}}'

Each reading starts a device-telemetry instance about 10 seconds later; only the second emits com.example.device.temperature.high.

Firmware Update Workflow

This workflow requests a firmware update for each eligible device of a zone, those whose firmware is older than the target version. For each one it waits for a status event matching the campaign and the device, then records the installed version. If no status arrives within 60 seconds, it reads the device's current state from the shop instead. A real campaign would wait hours.

document: dsl: '1.0.3' namespace: examples name: firmware-update version: '1.0.0' input: schema: document: type: object required: [firmwareVersion, campaignId, zone] properties: firmwareVersion: type: string campaignId: type: string zone: type: string do: - findEligibleDevices: call: http with: method: get endpoint: http://127.0.0.1:8787/devices query: zone: ${ .zone } firmwareBelow: ${ .firmwareVersion } - updateDevices: for: in: ${ .items } each: device do: - rememberDevice: set: deviceId: ${ $device.deviceId } export: as: '${ $context + { deviceId: .deviceId } }' - dispatchUpdate: emit: event: with: type: com.example.iot.firmware.requested source: https://events.example.com/warehouse data: campaignId: ${ $workflow.input.campaignId } deviceId: ${ $device.deviceId } firmwareVersion: ${ $workflow.input.firmwareVersion } - awaitStatus: try: - waitForStatus: listen: to: one: with: type: com.example.iot.firmware.status correlate: campaignId: from: ${ .campaignId } expect: ${ $workflow.input.campaignId } deviceId: from: ${ .deviceId } expect: ${ $context.deviceId } timeout: after: seconds: 60 output: as: ${ .[0] } catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/timeout do: - readDurableStatus: call: http with: method: get endpoint: '${ "http://127.0.0.1:8787/devices/\($context.deviceId | @uri)" }' - recordStatus: call: http with: method: patch endpoint: '${ "http://127.0.0.1:8787/devices/\($context.deviceId | @uri)" }' body: firmware: ${ .version // .firmware }

Save it as firmware-update-workflow.yaml:

bin/lemline definition post -f firmware-update-workflow.yaml bin/lemline instance start examples firmware-update --input '{"firmwareVersion":"1.1.0","campaignId":"CMP-1","zone":"dock"}'

In the dock, only sensor-2 runs a firmware older than 1.1.0. Once the instance waits for it, report the installation:

bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.iot.firmware.status","data":{"campaignId":"CMP-1","deviceId":"sensor-2","status":"installed","version":"1.1.0"}}'

The shop now shows sensor-2 with firmware 1.1.0. To see the fallback, start a campaign for the cold room and send no status:

bin/lemline instance start examples firmware-update --input '{"firmwareVersion":"1.1.0","campaignId":"CMP-2","zone":"cold-room"}'

After 60 to 70 seconds the listener times out, the workflow reads sensor-1 from the shop, and its firmware stays 1.0.0.

Predictive Maintenance Workflow

This workflow runs every 24 hours. For each device it asks the shop for a failure prediction, then opens a maintenance ticket above 0.7, warns the maintenance team above 0.4, and does nothing otherwise. schedule.every runs it once when it is started, then at each interval; schedule.cron would wait for the next matching time instead.

document: dsl: '1.0.3' namespace: examples name: predictive-maintenance version: '1.0.0' schedule: every: hours: 24 do: - retrieveDevices: call: http with: method: get endpoint: http://127.0.0.1:8787/devices - reviewDevices: for: in: ${ .items } each: device do: - predictMaintenance: call: http with: method: post endpoint: ${ "http://127.0.0.1:8787/devices/\($device.deviceId)/predictions" } - routePrediction: switch: - maintenanceNeeded: when: ${ .probabilityOfFailure > 0.7 } then: createTicket - warning: when: ${ .probabilityOfFailure > 0.4 } then: notifyMaintenance - healthy: then: exit - createTicket: call: http with: method: post endpoint: http://127.0.0.1:8787/documents body: reference: ${ $device.deviceId } title: Maintenance ticket content: '${ "Failure probability \(.probabilityOfFailure)" }' then: exit - notifyMaintenance: call: http with: method: post endpoint: http://127.0.0.1:8787/notifications body: reference: ${ $device.deviceId } recipient: maintenance message: '${ "Failure probability \(.probabilityOfFailure)" }'

Save it as predictive-maintenance-workflow.yaml:

bin/lemline definition post -f predictive-maintenance-workflow.yaml bin/lemline instance start examples predictive-maintenance

The predictions are 0.13 for sensor-1, 0.78 for sensor-2 and 0.42 for sensor-3: the run opens a Maintenance ticket document for sensor-2 and leaves a Failure probability 0.42 message for maintenance about sensor-3. The schedule repeats every 24 hours while a worker runs.

Energy Optimization Workflow

This workflow runs every 15 minutes. It asks the shop for a plan based on the zone's sensor readings, requests the HVAC and lighting changes with two events, and waits up to five minutes for both devices to confirm the same request. Without both confirmations it records the optimization with none.

document: dsl: '1.0.3' namespace: examples name: energy-optimization version: '1.0.0' schedule: every: minutes: 15 input: from: '${ { zone: "dock" } + (. // {}) }' do: - rememberZone: set: zone: ${ .zone } export: as: '${ $context + { zone: .zone } }' - calculateSettings: call: http with: method: post endpoint: http://127.0.0.1:8787/energy-plans body: zone: ${ .zone } export: as: '${ $context + { settings: . } }' - updateHvac: emit: event: with: type: com.example.iot.settings.requested source: https://events.example.com/energy data: requestId: ${ $workflow.input.requestId } zone: ${ $context.zone } deviceType: hvac setpoint: ${ $context.settings.hvacSetpoint } - updateLighting: emit: event: with: type: com.example.iot.settings.requested source: https://events.example.com/energy data: requestId: ${ $workflow.input.requestId } zone: ${ $context.zone } deviceType: lighting level: ${ $context.settings.lightingLevel } - getConfirmations: try: - waitForConfirmations: listen: to: all: - with: type: com.example.iot.settings.applied data: ${ .deviceType == "hvac" } correlate: requestId: from: ${ .requestId } expect: ${ $workflow.input.requestId } - with: type: com.example.iot.settings.applied data: ${ .deviceType == "lighting" } correlate: requestId: from: ${ .requestId } expect: ${ $workflow.input.requestId } timeout: after: PT5M catch: errors: with: type: https://open-workflow-specification.org/spec/1.0.0/errors/timeout do: - markUnconfirmed: set: confirmations: [] - recordOptimization: call: http with: method: post endpoint: http://127.0.0.1:8787/audit body: reference: ${ $context.zone } message: '${ "Optimization applied: hvac \($context.settings.hvacSetpoint), lighting \($context.settings.lightingLevel), \(if type == "array" then length else 0 end) confirmations" }'

Save it as energy-optimization-workflow.yaml:

bin/lemline definition post -f energy-optimization-workflow.yaml bin/lemline instance start examples energy-optimization --input '{"zone":"dock","requestId":"REQ-1"}'

The dock's two sensors average 20 °C, so the plan sets the HVAC to 19 and the lighting to 70. Confirm both changes:

bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.iot.settings.applied","data":{"requestId":"REQ-1","deviceType":"hvac"}}' bin/lemline instance start examples tutorial-publisher --input '{"type":"com.example.iot.settings.applied","data":{"requestId":"REQ-1","deviceType":"lighting"}}'

The instance completes and the shop's audit list shows Optimization applied: hvac 19, lighting 70, 2 confirmations for the dock. The schedule repeats every 15 minutes while a worker runs; stop the worker when you are done. Stop the shop too:

docker compose -f examples/tutorial-services/compose.yaml down

Best Practices for IoT Workflows

When implementing IoT workflows with Lemline, consider these best practices:

  1. Device Identity Management: Implement secure device identity and credential management

  2. Batch Processing: Process telemetry data in batches for efficiency

  3. Error Handling: Use robust error handling and retry mechanisms for device communications

  4. Correlation: Properly correlate events from the same device across different workflows

  5. Timeouts: Set appropriate timeouts for device communication, which may be unstable (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)

  6. Stateful Workflows: Use workflow context for maintaining state about devices

  7. Digital Twins: Update digital twin representations with the latest device state

  8. Progressive Deployment: Implement batched or phased deployments for firmware updates

  9. Monitoring: Include appropriate logging and monitoring for device interactions

  10. Data Validation: Always validate incoming telemetry data before processing

Conclusion

These examples demonstrate how to implement common IoT patterns as workflows using Lemline. The workflows cover the entire IoT lifecycle, from device provisioning to telemetry processing, firmware updates, predictive maintenance, and energy optimization.

By leveraging Lemline's workflow capabilities, you can build robust, scalable IoT solutions that handle complex device management and data processing requirements while maintaining reliability and performance.

For more examples of specific integrations, see the Integration Examples document.

Run the warehouse exercise to deliver a real event to the manager’s inbox.

25 November 2026