跳到主要内容

自动化

通过自动化为节点创建触发条件和操作,响应特定的条件或事件。

概览

在 RainMaker 中,自动化可以让你:

  • 为节点创建自动化触发器。
  • 获取并管理自动化任务。
  • 基于设备状态设置条件操作。

添加自动化

使用 addAutomation 方法,为节点添加新的自动化触发器。

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 接口

addAutomation 方法需要 ESPAutomationDetails 对象,包含以下属性:

属性类型必需说明
name*字符串自动化任务的名称。
events*ESPAutomationEvent[]触发自动化任务的事件数组。
eventOperator*ESPAutomationEventOperator组合多个事件的逻辑运算符(andor)。
actions*ESPAutomationAction[]事件触发时要执行的操作数组。
retrigger*布尔值在自动化任务已激活时是否允许再次触发。
metadata任意类型与该自动化任务关联的可选元数据。

示例:使用 AND 运算符的多个事件

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);
}

示例:使用 OR 运算符的多个事件

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);
}

获取自动化任务

使用 getAutomations 方法获取某个节点的自动化任务列表,支持可选的分页。

try {
const automations = await node.getAutomations();
console.log("Node automations:", automations);
} catch (error) {
console.error("Error fetching automations:", error);
}

ESPPaginatedAutomationsResponse 接口

getAutomations 方法会返回 ESPPaginatedAutomationsResponse 对象,包含以下属性:

属性类型说明
automations*ESPAutomation[]当前页中的自动化任务列表。
hasNext*布尔值是否还有更多页可用。
fetchNext() => Promise<ESPPaginatedAutomationsResponse>获取下一页自动化任务的可选函数。

示例:处理分页

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);
}

示例:处理自动化任务

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);
}

前提条件:设置地理坐标

在使用基于位置的自动化任务(如日照和天气自动化任务)之前,必须先设置用户的地理坐标,因为日照和天气自动化任务都依赖用户的位置来判定触发条件。

设置地理坐标

使用 setGeoCoordinates 方法保存用户的纬度和经度。

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);
}

获取地理坐标

使用 getGeoCoordinates 方法获取已保存的地理坐标。

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);
}

注意: 如果在未先设置地理坐标的情况下尝试创建日照或天气自动化任务,SDK 会自动获取已保存的坐标。若未找到坐标,则会报错。

添加基于日照的自动化任务

使用 addDaylightBasedAutomation 方法创建基于日照事件(如日出与日落)的自动化任务。

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 接口

addDaylightBasedAutomation 方法需要 ESPDaylightAutomationDetails 对象,包含以下属性:

属性类型必需说明
name*字符串自动化任务的名称。
events*ESPDaylightEvent[]日照事件数组(sunrisesunset)。
eventOperator*ESPAutomationEventOperator组合多个事件的逻辑运算符(andor)。
actions*ESPAutomationAction[]事件触发时要执行的操作数组。
retrigger*布尔值在自动化任务已激活时是否允许再次触发。
locationESPGeoCoordinates可选的位置覆写配置。若未提供,则使用已保存的地理坐标。
metadata任意类型与该自动化任务关联的可选元数据。

示例:在日出时开灯

try {
const daylightAutomationDetails = {
name: "Morning lights",
events: ["sunrise"],
eventOperator: "or",
actions: [
{
nodeId: "bedroom_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "bedroom_light_node_id",
deviceName: "Light",
param: "Brightness",
value: 50,
},
],
retrigger: true,
metadata: {
description: "Gradually turn on bedroom lights at sunrise",
},
};

const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Sunrise automation created:", automation.automationId);
} catch (error) {
console.error("Error creating sunrise automation:", error);
}

示例:在日落时执行多个操作

try {
const daylightAutomationDetails = {
name: "Evening routine",
events: ["sunset"],
eventOperator: "or",
actions: [
{
nodeId: "living_room_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "outdoor_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "thermostat_node_id",
deviceName: "Thermostat",
param: "Temperature",
value: 22,
},
],
retrigger: true,
};

const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Evening automation created successfully");
} catch (error) {
console.error("Error creating evening automation:", error);
}

示例:使用自定义位置

try {
// 使用自定义的位置而不是已保存的地理坐标
const customLocation = {
latitude: "40.7128", // 纽约
longitude: "-74.0060",
};

const daylightAutomationDetails = {
name: "NYC sunset lights",
events: ["sunset"],
eventOperator: "or",
actions: [
{
nodeId: "light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
],
retrigger: true,
location: customLocation,
};

const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Custom location automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}

添加基于天气的自动化任务

使用 addWeatherBasedAutomation 方法创建基于天气条件触发的自动化任务。

try {
const weatherAutomationDetails = {
name: "Turn on heater when cold",
events: [
{
param: "temperature",
value: 15,
check: "<=",
},
],
eventOperator: "or",
actions: [
{
nodeId: "heater_node_id",
deviceName: "Heater",
param: "Power",
value: true,
},
{
nodeId: "heater_node_id",
deviceName: "Heater",
param: "Temperature",
value: 22,
},
],
retrigger: true,
metadata: {
description: "Turn on heater when temperature drops below 15°C",
},
};

const automation = await user.addWeatherBasedAutomation(
weatherAutomationDetails
);
console.log("Weather automation created:", automation);
} catch (error) {
console.error("Error creating weather automation:", error);
}

ESPWeatherAutomationDetails 接口

addWeatherBasedAutomation 方法需要 ESPWeatherAutomationDetails 对象,包含以下属性:

属性类型必需说明
name*字符串自动化任务的名称。
events*ESPWeatherEvent[] | ESPWeatherCondition天气事件或条件的数组。
eventOperator*ESPAutomationEventOperator组合多个事件的逻辑运算符(andor)。
actions*ESPAutomationAction[]事件触发时要执行的操作数组。
retrigger*布尔值在自动化任务已激活时是否允许再次触发。
locationESPGeoCoordinates可选的位置覆写配置。若未提供,则使用已保存的地理坐标。
metadata任意类型与该自动化任务关联的可选元数据。

天气事件类型

天气事件可以通过两种方式指定:

  1. 天气条件字符串:使用预定义的天气条件字符串,例如 rainsnowclear 等。
  2. 天气参数对象:通过数值和比较运算符来指定某个天气参数:
    • param: 天气参数(例如,temperaturehumiditywind_speed)。
    • value: 阈值。
    • check: 比较运算符(==!=<><=>=)。

示例:天气条件触发

try {
const weatherAutomationDetails = {
name: "Close windows when raining",
events: ["rain"], // 使用天气条件字符串
eventOperator: "or",
actions: [
{
nodeId: "window_controller_node_id",
deviceName: "Window",
param: "Position",
value: 0, // 完全关闭
},
],
retrigger: false,
};

const automation = await user.addWeatherBasedAutomation(
weatherAutomationDetails
);
console.log("Rain automation created:", automation.automationId);
} catch (error) {
console.error("Error creating rain automation:", error);
}

示例:多个天气参数

try {
const weatherAutomationDetails = {
name: "Climate control",
events: [
{
param: "temperature",
value: 30,
check: ">=",
},
{
param: "humidity",
value: 70,
check: ">=",
},
],
eventOperator: "and", // 必须同时满足两个条件
actions: [
{
nodeId: "ac_node_id",
deviceName: "AC",
param: "Power",
value: true,
},
{
nodeId: "ac_node_id",
deviceName: "AC",
param: "Temperature",
value: 24,
},
{
nodeId: "dehumidifier_node_id",
deviceName: "Dehumidifier",
param: "Power",
value: true,
},
],
retrigger: true,
metadata: {
description: "Turn on AC and dehumidifier when hot and humid",
},
};

const automation = await user.addWeatherBasedAutomation(
weatherAutomationDetails
);
console.log("Climate automation created successfully");
} catch (error) {
console.error("Error creating climate automation:", error);
}

示例:风速监控

try {
const weatherAutomationDetails = {
name: "High wind alert",
events: [
{
param: "wind_speed",
value: 50, // km/h
check: ">=",
},
],
eventOperator: "or",
actions: [
{
nodeId: "outdoor_blind_node_id",
deviceName: "Blind",
param: "Position",
value: 0, // 收起百叶窗
},
{
nodeId: "notification_node_id",
deviceName: "Alert",
param: "Status",
value: true,
},
],
retrigger: false,
};

const automation = await user.addWeatherBasedAutomation(
weatherAutomationDetails
);
console.log("Wind speed automation created:", automation);
} catch (error) {
console.error("Error creating wind automation:", error);
}

示例:结合天气条件与参数

try {
const weatherAutomationDetails = {
name: "Smart weather response",
events: [
"snow", // 天气条件字符串
{
param: "temperature",
value: 0,
check: "<=",
},
],
eventOperator: "or", // 下雪或气温低于 0°C 时触发
actions: [
{
nodeId: "heating_node_id",
deviceName: "Heating",
param: "Power",
value: true,
},
{
nodeId: "heating_node_id",
deviceName: "Heating",
param: "Mode",
value: "max",
},
],
retrigger: true,
metadata: {
description: "Maximize heating during snow or freezing conditions",
},
};

const automation = await user.addWeatherBasedAutomation(
weatherAutomationDetails
);
console.log("Winter weather automation created:", automation);
} catch (error) {
console.error("Error creating weather automation:", error);
}