本帖最后由 cjnt007 于 2024-6-21 15:22 编辑
首先,把【提取令牌】节点里的代码修改如下(要按自己实际情况对应修改):
//global.set('ac_list', ['ac_00', 'ac_01', 'ac_02', 'ac_03', 'ac_04', 'ac_05', 'ac_06']);
let ac_list_array = {
ac_sn: ['ac_00', 'ac_01', 'ac_02', 'ac_03', 'ac_04', 'ac_05', 'ac_06'],
ac_name: ['储藏室空调','次卧空调','书房空调','客房空调','餐厅空调','客厅空调','主卧空调'],
ac_id: ['chu_cang_shi_kong_diao', 'ci_wo_kong_diao', 'shu_fan_kong_diao', 'ke_fang_kong_diao', 'can_ting_kong_diao', 'ke_ting_kong_diao', 'zhu_wo_kong_diao']
}
global.set('ac_list_array', ac_list_array);
注意:原先相关function节点里读取global.get('ac_list')的要更改为global.get('ac_list_array').ac_sn
生成空调实体【配置MQTT Climate】里的代码如下:
let ac_list_array = global.get('ac_list_array');
let ac_count = ac_list_array.ac_sn.length; //内机数
//生成x台空调内机的MQTT Climae配置信息,依次发送到HA
for (let index = 0; index < ac_count; index++) {
let ac_name = ac_list_array.ac_name[index];
let ac_sn = ac_list_array.ac_sn[index];
let ac_id = ac_list_array.ac_id[index];
msg.payload = {
name: ac_name,
unique_id: ac_id,
modes: ['off','cool','dry','fan_only','heat'],
fan_modes: ['low','medium','high'],
max_temp: 32,
min_temp: 16,
power_command_topic: `climate/${ac_sn}/power`,
mode_command_topic: `climate/${ac_sn}/mode`,
temperature_command_topic: `climate/${ac_sn}/temp`,
fan_mode_command_topic: `climate/${ac_sn}/fan`,
current_temperature_topic: `climate/${ac_sn}/tempnow`,
fan_mode_state_topic: `climate/${ac_sn}/fannow`,
mode_state_topic: `climate/${ac_sn}/state`,
temperature_state_topic: `climate/${ac_sn}/tempstate`
}
msg.retain = true;
msg.qos = 0;
msg.topic = `homeassistant/climate/${ac_id}/config`;
node.send(msg);
}
return;
【预处理】节点的代码因topic调整,substr函数截取字符参数作如下修改:
/*
topic形如以下:
climate/ac_00/fan
climate/ac_00/temp
climate/ac_00/mode
climate/ac_00/power
*/
let ctrl_value = msg.payload; //读取控制值
let ctrl_code = msg.topic.substr(14); //读取控制码
let ac_name = msg.topic.substr(8,5); //读取内机名称
msg.ac_sn = msg.topic.substr(11,2); //读取内机序号
其他参看14楼里的内容
|