Sonoff TRVZB与Google Home脚本问题:基于温度判断加热状态
eWeLink脚本实现TRVZB加热状态判定与设备控制
核心解决方案脚本
针对你需要通过环境温度与设定温度对比来判定加热状态的需求,以下是符合eWeLink脚本语法的实现代码:
// 触发条件:TRVZB环境温度/设定温度变化时执行 if (device.temperature.ambient < device.temperature.setpoint) { // 环境温度低于设定值,判定为加热状态,打开目标设备 controlDevice("toSwitch", "turnOn"); } else { // 环境温度≥设定值,停止加热,关闭目标设备 controlDevice("toSwitch", "turnOff"); }
关键说明
参数路径确认
eWeLink脚本中TRVZB的温度参数路径需准确对应:- 环境温度:
device.temperature.ambient - 设定温度:
device.temperature.setpoint
可在脚本编辑页面的「设备状态变量」中查看当前设备的所有可用字段,避免因参数名错误导致语法报错。
- 环境温度:
触发条件设置
必须将脚本的触发条件设置为TRVZB的thermostatTemperatureAmbient或thermostatTemperatureSetpoint状态变化,确保温度变动时自动执行脚本逻辑。之前写法的问题分析
- 用
activeThermostatMode判定:该字段对应TRVZB的手动/自动模式,和阀门实际是否加热无直接关联,因此会出现始终返回true的无效判定。 - 温度对比语法错误:大概率是参数路径拼写错误(如误写字段名),或使用了eWeLink脚本不支持的语法格式(eWeLink脚本基于JavaScript子集,需遵循其语法规范)。
- 用
进阶优化方案
1. 添加调试日志
方便排查脚本执行逻辑,可在脚本中加入日志输出:
console.log(`当前环境温度: ${device.temperature.ambient}°C,设定温度: ${device.temperature.setpoint}°C`); if (device.temperature.ambient < device.temperature.setpoint) { console.log("判定为加热状态,打开toSwitch"); controlDevice("toSwitch", "turnOn"); } else { console.log("判定为停止加热,关闭toSwitch"); controlDevice("toSwitch", "turnOff"); }
2. 温度波动阈值
为避免温度小幅波动导致设备频繁开关,可设置温度阈值:
const threshold = 0.5; // 可自行调整阈值大小 if (device.temperature.setpoint - device.temperature.ambient > threshold) { controlDevice("toSwitch", "turnOn"); } else if (device.temperature.ambient - device.temperature.setpoint >= threshold) { controlDevice("toSwitch", "turnOff"); }
内容的提问来源于stack exchange,提问作者pie Techn
相关产品推荐
相关产品推荐

