利用指定灯光[亮度/色温属性] 识别/触发跨生态联动自动化

101

我们知道跨生态联动,需要不同生态同时可监视控制同一个设备/实体,根据该实体不同的状态/属性的变化来触发不同生态中的自动化.

今天用可变色温/亮度的灯具实体完成跨生态联动操作.

实例:

设备:米家智能灯

系统:Home Assistant

要素:xiaomi home集成

门锁:小米智能门锁 (aqara以及其他智能门锁同理)

实现效果:不同人员回家,执行对应的自动化流程 (门锁可以使用指纹/人脸识别不同人员回家自动化)

思路:

不同人员开门时,可在原生态APP中配置不同人员回家自动化,打开指定某一盏灯,同时修改灯具亮度/色温,该配置尽量在中枢网关配置极客版自动化流程,触发动作完成后可延时关闭指定灯光.

用NodeRed监控该灯具的属性动态变化,导引属性检查判断,符合条件执行不同流程.

function节点判断: (此处配置三种亮度值,分别触发三个不同自动化流程.)

// 灯亮度变化监控function - 简化版
const newBrightness = msg.data.attributes.brightness;
const lightState = msg.data.state;
const friendlyName = msg.data.attributes.friendly_name;

// 目标亮度值
const TARGET1_BRIGHTNESS = 102;
const TARGET2_BRIGHTNESS = 128;
const TARGET3_BRIGHTNESS = 153;
// 构建消息
const message = {
    entity_id: msg.data.entity_id,
    friendly_name: friendlyName,
    brightness: newBrightness,
    light_state: lightState,
    color_temp_kelvin: msg.data.attributes.color_temp_kelvin,
    timestamp: msg.data.last_updated
};

// 条件判断:亮度达到102且灯开启
if (newBrightness === TARGET1_BRIGHTNESS && lightState === "on") {
    node.warn(`灯亮度达到目标值 - ${friendlyName}: ${newBrightness}`);
    return [msg, null, null];
}
if (newBrightness === TARGET2_BRIGHTNESS && lightState === "on") {
    node.warn(`灯亮度达到目标值 - ${friendlyName}: ${newBrightness}`);
    return [null, msg, null];
} 
if (newBrightness === TARGET3_BRIGHTNESS && lightState === "on") {
    node.warn(`灯亮度达到目标值 - ${friendlyName}: ${newBrightness}`);
    return [null, null, msg];
} else {
    return [null, null, null]; // 不满足条件,不输出
}

以上可以在属性中取亮度brightness, 色温color_temp_kelvin,作为判断依据.

完整的配置属性参考:

// 灯亮度变化监控function - 详细版
const oldState = msg.data.old_state;
const newState = msg.data.new_state;

// 获取新旧亮度值
const oldBrightness = oldState.attributes.brightness;
const newBrightness = newState.attributes.brightness;

// 获取其他有用信息
const lightState = newState.state;
const friendlyName = newState.attributes.friendly_name;
const oldLightState = oldState.state;

// 构建消息对象
const message = {
    entity_id: newState.entity_id,
    friendly_name: friendlyName,
    light_state: lightState,
    old_light_state: oldLightState,
    old_brightness: oldBrightness,
    new_brightness: newBrightness,
    brightness_changed: oldBrightness !== newBrightness,
    state_changed: oldLightState !== lightState,
    color_temp: newState.attributes.color_temp,
    color_temp_kelvin: newState.attributes.color_temp_kelvin,
    effect: newState.attributes.effect,
    color_mode: newState.attributes.color_mode,
    rgb_color: newState.attributes.rgb_color,
    hs_color: newState.attributes.hs_color,
    timestamp: newState.last_updated,
    timeSinceChangedMs: msg.timeSinceChangedMs,
    // 添加变化详情
    changes: {
        brightness: oldBrightness !== newBrightness ? {
            from: oldBrightness,
            to: newBrightness
        } : null,
        state: oldLightState !== lightState ? {
            from: oldLightState,
            to: lightState
        } : null
    }
};

// 目标亮度值
const TARGET_BRIGHTNESS = 102;

// 调试信息
node.log(`灯状态变化检测 - ${friendlyName}:`);
node.log(`  状态: ${oldLightState} → ${lightState}`);
node.log(`  亮度: ${oldBrightness} → ${newBrightness}`);
node.log(`  色温: ${newState.attributes.color_temp_kelvin}K`);
node.log(`  效果: ${newState.attributes.effect}`);

// 条件判断
if (newBrightness === TARGET_BRIGHTNESS && lightState === "on") {
    // 亮度达到目标值且灯是开启状态
    node.warn(`✅ 触发条件满足 - ${friendlyName}: 亮度${newBrightness}, 色温${newState.attributes.color_temp_kelvin}K`);
    return message; // 输出到第一个端口
} else if (newBrightness !== oldBrightness && lightState === "on") {
    // 亮度发生变化但不是目标值,且灯是开启状态
    node.warn(`🔄 亮度变化 - ${friendlyName}: ${oldBrightness} → ${newBrightness} (目标: ${TARGET_BRIGHTNESS})`);
    return null; // 不输出
} else if (lightState !== oldLightState) {
    // 灯开关状态变化
    node.warn(`🔌 开关状态变化 - ${friendlyName}: ${oldLightState} → ${lightState}`);
    return null; // 不输出
} else {
    // 其他情况
    node.warn(`ℹ️ 其他变化 - ${friendlyName}: 状态${lightState}, 亮度${newBrightness}`);
    return null; // 不输出
}