Automation
Create triggers that automatically execute actions when device conditions are met.
备注
The userInstance refers to ESPRMUser from User Sign in.
What This Module Does ?
Automations link events (conditions) to actions (parameter changes on nodes). When a trigger fires—motion, brightness, sunrise, weather—the SDK runs the configured actions.
| Type | API |
|---|---|
| Node-level (device params) | node.addAutomation() |
| Daylight (sunrise/sunset) | userInstance.addDaylightBasedAutomation() |
| Weather | userInstance.addWeatherBasedAutomation() |
Manage existing automations in Automation Management.
Expected outcome: A new automation is created and runs when its conditions match.
Common Workflows
Motion triggers light
const automation = await node.addAutomation({
name: "Motion → Light",
events: [{ params: { Motion: { Motion: true } }, check: "==" }],
eventOperator: "and",
actions: [
{
node_id: "target_node_id",
params: { Light: { Power: true, Brightness: 80 } },
},
],
retrigger: false,
});
Multiple events (AND)
await node.addAutomation({
name: "Low light + motion",
events: [
{ params: { Light: { Brightness: 30 } }, check: "<=" },
{ params: { Motion: { Motion: true } }, check: "==" },
],
eventOperator: "and",
actions: [{ node_id: "node_id", params: { Light: { Power: true } } }],
retrigger: false,
});
List automations on a node
const automations = await node.getAutomations();
Daylight automation (sunrise)
await userInstance.setGeoCoordinates({
latitude: 37.7749,
longitude: -122.4194,
});
await userInstance.addDaylightBasedAutomation({
name: "Sunrise lights",
eventType: "sunrise",
offsetMinutes: 0,
actions: [
{ nodeId: "node_id", deviceName: "Light", params: { Power: true } },
],
});
Weather automation (rain)
await userInstance.addWeatherBasedAutomation({
name: "Close blinds when raining",
eventType: "rain",
actions: [
{ nodeId: "node_id", deviceName: "Blinds", params: { Power: false } },
],
});
Error Handling
try {
await node.addAutomation(automationDetails);
} catch (error) {
console.error("Failed to create automation:", error);
}
Match event parameter names to node config and ensure action node_id values exist.
Advanced Concepts
Mental model
When X happens → do Y on one or more nodes.
Event and action structure
- Events watch parameter values on the source node; actions can target other nodes
eventOperatoris"and"or"or"for multiple eventsretriggercontrols whether the automation can fire again while active- Daylight and weather automations need
setGeoCoordinates()first
Best Practices
- Set geo coordinates before daylight/weather automations
- Use clear automation names in management UI
- Start with
retrigger: false - Test one event before combining many
- Use Automation Management for edits after creation