hunl0613 发表于 2023-12-23 15:00:18

node-red代码向mqtt发送switch,homeassistant不能更改状态

本帖最后由 hunl0613 于 2023-12-23 15:22 编辑

msg.payload={
   "name":"38度开关",
   "command_topic": "homeassistant/switch/wanghe_switch_38/set",
   "state_topic":"homeassistant/switch/wanghe_switch_38/state",
   "unique_id":"wh101",
   "device":{
      "identifiers":[
         "wanghe101"
      ],
      "name":"热水器"
      
   },
   "payload_on": "ON",
   "payload_off": "OFF",
   "state_on": "ON",
   "state_off": "OFF",
   "qos": 0,
   "retain": true,
   "command_on_template": "ON",
   "command_off_template": "OFF",
   "state_on_template": "ON",
   "state_off_template": "OFF",
   "value_template": "{{ value_json.state }}"
}
msg.topic = "homeassistant/switch/wanghe_switch_38/config";
msg.retain = true; // 保留配置主题
return msg;请问上面node-red代码向mqtt发送switch,发送ON或OFF到home

assistant/switch/wanghe_switch_38/state主题可以更改状态,但在homeassistant中mqtt生成的switch中没法点击更改状态,添加代码   "optimistic": true后可以更改状态,但显示的开关样式为turn而不是switch,

请问如何解决让按钮可以更改状态并样式为switch




hunl1986 发表于 2023-12-23 21:53:03

msg.payload = {
   "name":"38度开关",
   "command_topic": "homeassistant/switch/wanghe_switch_38/set",
   "state_topic":"homeassistant/switch/wanghe_switch_38/set",
   "unique_id": "wh101",
   "device": {
      "identifiers": [
         "wanghe101"
      ],
      "name": "热水器"
   },
   "payload_on": "ON",
   "payload_off": "OFF",
   "state_on": "ON",
   "state_off": "OFF",
}
msg.topic = "homeassistant/switch/wanghe_switch_38/config";
msg.retain = true; // 保留配置主题
return msg;

更改状态发送到msg.topic = "homeassistant/switch/wanghe_switch_38/set";就行了
页: [1]
查看完整版本: node-red代码向mqtt发送switch,homeassistant不能更改状态