Automation
Automation allows you to create triggers and actions for nodes. You can set up automations that respond to specific conditions or events.
Overview
Automation in RainMaker enables you to:
- Create automation triggers for nodes
- Retrieve and manage automations
- Set up conditional actions based on device states
Add Automation
Add a new automation trigger for a node using the addAutomation method.
try {
const automationDetails = {
name: "Turn on lights when motion detected",
events: [
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
],
eventOperator: "and",
actions: [
{
node_id: "target_node_id",
params: {
Light: {
Power: true,
Brightness: 80,
},
},
},
],
retrigger: false,
metadata: {
description: "Automation to turn on lights when motion is detected",
},
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
ESPAutomationDetails Interface
The addAutomation method requires an ESPAutomationDetails object with the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
| name* | string | Yes | The name of the automation. |
| events* | ESPAutomationEvent[] | Yes | Array of events that trigger the automation. |
| eventOperator* | ESPAutomationEventOperator | Yes | Operator to combine multiple events ("and" or "or"). |
| actions* | ESPAutomationAction[] | Yes | Array of actions to execute when events are triggered. |
| retrigger* | boolean | Yes | Whether the automation can be retriggered while already active. |
| metadata | any | No | Optional metadata associated with the automation. |
Example: Multiple Events with AND Operator
try {
const automationDetails = {
name: "Turn on lights when brightness is low AND motion detected",
events: [
{
params: {
Light: {
Brightness: 30,
},
},
check: "<=",
},
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
],
eventOperator: "and",
actions: [
{
node_id: "light_node_id",
params: {
Light: {
Power: true,
Brightness: 100,
},
},
},
],
retrigger: false,
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
Example: Multiple Events with OR Operator
try {
const automationDetails = {
name: "Turn on lights when motion detected OR time is after sunset",
events: [
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
{
params: {
Time: {
Time: "18:00",
},
},
check: ">=",
},
],
eventOperator: "or",
actions: [
{
node_id: "light_node_id",
params: {
Light: {
Power: true,
},
},
},
],
retrigger: true,
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
Get Automations
Retrieve a list of all automations for a node with optional pagination using the getAutomations method.
try {
const automations = await node.getAutomations();
console.log("Node automations:", automations);
} catch (error) {
console.error("Error fetching automations:", error);
}
ESPPaginatedAutomationsResponse Interface
The getAutomations method returns an ESPPaginatedAutomationsResponse object with the following properties:
| Property | Type | Description |
|---|---|---|
| automations* | ESPAutomation[] | List of automations in the current page. |
| hasNext* | boolean | Indicates if there are more pages available. |
| fetchNext | () => Promise<ESPPaginatedAutomationsResponse> | Optional function to fetch the next page of automations. |
Example: Handling Pagination
try {
let response = await node.getAutomations();
let allAutomations = [...response.automations];
// Fetch all pages if available
while (response.hasNext && response.fetchNext) {
response = await response.fetchNext();
allAutomations = [...allAutomations, ...response.automations];
}
console.log("All automations:", allAutomations);
console.log("Total count:", allAutomations.length);
} catch (error) {
console.error("Error fetching automations:", error);
}
Example: Processing Automations
try {
const response = await node.getAutomations();
response.automations.forEach((automation) => {
console.log(`Automation: ${automation.name}`);
console.log(`Events: ${automation.events.length}`);
console.log(`Actions: ${automation.actions.length}`);
console.log(`Retrigger: ${automation.retrigger}`);
});
if (response.hasNext) {
console.log(
"More automations available. Use fetchNext() to retrieve them."
);
}
} catch (error) {
console.error("Error fetching automations:", error);
}
Prerequisites: Setting Geo Coordinates
Before using location-based automations (Daylight and Weather), you must set the user's geographical coordinates. This is required because both daylight and weather automations rely on the user's location to determine trigger conditions.
Set Geo Coordinates
Use the setGeoCoordinates method to store the user's latitude and longitude.
try {
const geoCoordinates = {
latitude: "37.7749", // Example: San Francisco latitude
longitude: "-122.4194", // Example: San Francisco longitude
};
const response = await user.setGeoCoordinates(geoCoordinates);
console.log("Geo coordinates set successfully:", response);
} catch (error) {
console.error("Error setting geo coordinates:", error);
}
Get Geo Coordinates
Retrieve the stored geographical coordinates using the getGeoCoordinates method.
try {
const coordinates = await user.getGeoCoordinates();
console.log("Latitude:", coordinates.latitude);
console.log("Longitude:", coordinates.longitude);
} catch (error) {
console.error("Error retrieving geo coordinates:", error);
}
Note: If you attempt to create a daylight or weather automation without setting geo coordinates first, the SDK will automatically retrieve the stored coordinates. If no coordinates are found, an error will be thrown.
Add Daylight-Based Automation
Create automations that trigger based on daylight events such as sunrise and sunset using the addDaylightBasedAutomation method.
try {
const daylightAutomationDetails = {
name: "Turn on lights at sunset",
events: ["sunset"], // Can be "sunrise" or "sunset"
eventOperator: "or",
actions: [
{
nodeId: "light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
],
retrigger: true,
metadata: {
description: "Automatically turn on lights when the sun sets",
},
};
const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Daylight automation created:", automation);
} catch (error) {
console.error("Error creating daylight automation:", error);
}
ESPDaylightAutomationDetails Interface
The addDaylightBasedAutomation method requires an ESPDaylightAutomationDetails object with the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
| name* | string | Yes | The name of the automation. |
| events* | ESPDaylightEvent[] | Yes | Array of daylight events ("sunrise" or "sunset"). |
| eventOperator* | ESPAutomationEventOperator | Yes | Operator to combine multiple events ("and" or "or"). |
| actions* | ESPAutomationAction[] | Yes | Array of actions to execute when events are triggered. |
| retrigger* | boolean | Yes | Whether the automation can be retriggered while already active. |
| location | ESPGeoCoordinates | No | Optional location override. If not provided, uses stored geo coordinates. |
| metadata | any | No | Optional metadata associated with the automation. |