NodeRed解决跨生态的开关/灯具联动
在我们做全屋智能调试的时候,经常会遇到智能开关控制智能灯的情况。
大家都知道,智能开关控制智能灯,往往需要把智能开关转换为无线开关模式。
同时还需要在对应的生态中设置自动化,这样才能够让智能开关和智能灯同时在线,否则当智能开关处于关闭的状态,智能灯就离线了。
但在这里必须要有一个前提, 智能开关和智能灯都必须要接入同一个生态,
比如米家的开关,自动化就必须要在米家里面写, aqara的开关也需要在aqara home里面配置.
今天我这里有一个典型的例子,开关是aqara的,但灯具的调光调色,只能在homekit里面控制.一旦aqara开关关闭了,智能灯具就离线了.
在我的调试方案中, 需要把他们同时都接入到Home Assistant里面统一管理.
那么怎样做到既可以单独控制灯具,又不能丢掉开关的功能呢?
我的方法是用NodeRed来写一个简单的自动化,即可完美解决.
效果是这样的,当我点击开关,不管是在APP上切换开关,还是直接去点按物理按钮,效果都是一样的.
都可以让智能灯具切换开关状态.
基本的思路是这样的:
1. 必须要保持开关的常通电状态
2. 监控开关的状态,断电后切换灯具的状态.
所以在NodeRed中,增加一个状态监控. 当开关被关闭,触发开关再次打开.开关再次打开的时候,去切换灯具的状态即可.

[
{
"id": "85211e94963c7b21",
"type": "server-state-changed",
"z": "ae76f6bbf821abec",
"name": "客厅主灯-开关",
"server": "219af92c.afc8e6",
"version": 6,
"outputs": 2,
"exposeAsEntityConfig": "",
"entities": {
"entity": [
"switch.wall_switch_switch2_6"
],
"substring": [],
"regex": []
},
"outputInitially": true,
"stateType": "str",
"ifState": "off",
"ifStateType": "str",
"ifStateOperator": "is",
"outputOnlyOnStateChange": true,
"for": "0.5",
"forType": "num",
"forUnits": "seconds",
"ignorePrevStateNull": false,
"ignorePrevStateUnknown": false,
"ignorePrevStateUnavailable": false,
"ignoreCurrentStateUnknown": false,
"ignoreCurrentStateUnavailable": false,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
}
],
"x": 110,
"y": 120,
"wires": [
[
"6cd00db484afd3f1",
"91626f3b1389ba64"
],
[]
]
},
{
"id": "6cd00db484afd3f1",
"type": "api-call-service",
"z": "ae76f6bbf821abec",
"name": "",
"server": "219af92c.afc8e6",
"version": 7,
"debugenabled": false,
"action": "switch.turn_on",
"floorId": [],
"areaId": [],
"deviceId": [],
"entityId": [
"switch.wall_switch_switch2_6"
],
"labelId": [],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"blockInputOverrides": true,
"domain": "switch",
"service": "turn_on",
"x": 340,
"y": 40,
"wires": [
[]
]
},
{
"id": "91626f3b1389ba64",
"type": "ha-wait-until",
"z": "ae76f6bbf821abec",
"name": "",
"server": "219af92c.afc8e6",
"version": 3,
"outputs": 2,
"entities": {
"entity": [
"switch.wall_switch_switch2_6"
],
"substring": [],
"regex": []
},
"property": "state",
"comparator": "is",
"value": "on",
"valueType": "str",
"timeout": "1",
"timeoutType": "num",
"timeoutUnits": "seconds",
"checkCurrentState": true,
"blockInputOverrides": true,
"outputProperties": [],
"x": 380,
"y": 120,
"wires": [
[
"343ff0e8470ed931"
],
[]
]
},
{
"id": "343ff0e8470ed931",
"type": "api-call-service",
"z": "ae76f6bbf821abec",
"name": "",
"server": "219af92c.afc8e6",
"version": 7,
"debugenabled": false,
"action": "light.toggle",
"floorId": [],
"areaId": [],
"deviceId": [],
"entityId": [
"light.pc000apcct_light_bulb_6"
],
"labelId": [],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"blockInputOverrides": true,
"domain": "light",
"service": "toggle",
"x": 630,
"y": 80,
"wires": [
[]
]
},
{
"id": "219af92c.afc8e6",
"type": "server",
"name": "Home Assistant",
"addon": true
}
]