『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 489|回复: 1

[流程系列] node-red代码向mqtt发送switch,homeassistant不能更改状态

[复制链接]

7

主题

15

帖子

82

积分

注册会员

Rank: 2

积分
82
金钱
67
HASS币
0
发表于 2023-12-23 15:00:18 | 显示全部楼层 |阅读模式
本帖最后由 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,
2.png
请问如何解决让按钮可以更改状态并样式为switch
flows .rar (1.46 KB, 下载次数: 2)



回复

使用道具 举报

9

主题

115

帖子

1165

积分

论坛技术达人

积分
1165
金钱
1045
HASS币
20
发表于 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";就行了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-2 00:26 , Processed in 0.257320 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表