9
46
184
注册会员
dandymin 发表于 2025-1-2 10:21 没看懂你写的是啥- -#。 逻辑就有问题吧。
使用道具 举报
0
21
94
if(msg.topic1 ==true && msg.topic2==true){ ###执行的操作 }
dandymin 发表于 2025-1-2 11:35 先获取到第一个设备的状态 赋值状态变量 msg.topic1 =true 然后获取第二个设备状态 赋值到变量msg.topic2= ...
// 获取当前消息的 payload var deviceStatus = msg.payload; // 创建一个新消息对象 var newMsg = {}; // 根据 topic 判断是哪个设备 if (msg.topic === 'device1/status') { context.set('device1', deviceStatus); } else if (msg.topic === 'device2/status') { context.set('device2', deviceStatus); } // 检查两个设备的状态 if (context.get('device1') !== undefined && context.get('device2') !== undefined) { var device1 = context.get('device1'); var device2 = context.get('device2'); // 如果两个设备都是打开状态 if (device1 === 'ON' && device2 === 'ON') { newMsg.payload = "Both devices are ON"; return [null, newMsg]; // 返回一个空消息和包含结果的消息 } else { return null; // 不输出任何消息 } } return null; // 不输出任何消息
qawsedfffrr 发表于 2025-1-2 11:46 这个是AI给的,请问要怎么改才能使用呢?
if(msg.payload1=="xxxx" && msg.payload2=='xxxx'){ ### }
12
263
2126
金牌会员
7
85
388
论坛分享达人
let device1status = context.get('device1status') || "off"; let device2status = context.get('device2status') || "off"; deviceStatus = msg.payload; if(msg.topic === "device1") { context.set('device1status', deviceStatus); device1status = deviceStatus; } else if(msg.topic === "device2") { context.set('device2status', deviceStatus); device2status = deviceStatus; } //下面的不用改 if 。。。。。
本版积分规则 发表回复 回帖后跳转到最后一页
Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )
GMT+8, 2025-5-22 16:33 , Processed in 0.243931 second(s), 22 queries .
Powered by Discuz! X3.5
© 2001-2025 Discuz! Team.