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

 找回密码
 立即注册
楼主: raspberry0316

[进阶教程] 小爱同学控制HA设备-免费-无需破解-有手就行

  [复制链接]

3

主题

35

帖子

327

积分

中级会员

Rank: 3Rank: 3

积分
327
金钱
292
HASS币
10
发表于 2021-7-20 18:57:18 | 显示全部楼层
先赞收藏备用了
回复

使用道具 举报

11

主题

80

帖子

599

积分

高级会员

Rank: 4

积分
599
金钱
519
HASS币
0
发表于 2021-7-22 22:03:56 | 显示全部楼层
小白有点奇怪 我是docker安装的nodered 如何让nodered和hass通讯呢
回复

使用道具 举报

46

主题

641

帖子

4411

积分

元老级技术达人

积分
4411
金钱
3740
HASS币
120
发表于 2021-8-1 23:09:27 | 显示全部楼层
感谢楼主分享好玩的用法
回复

使用道具 举报

1

主题

67

帖子

731

积分

高级会员

Rank: 4

积分
731
金钱
664
HASS币
0
发表于 2021-8-2 22:25:45 | 显示全部楼层
巴法支持IPV6公网吗?
回复

使用道具 举报

7

主题

188

帖子

2344

积分

金牌会员

Rank: 6Rank: 6

积分
2344
金钱
2156
HASS币
0
发表于 2021-8-3 15:59:33 | 显示全部楼层
//定义变量,serv代表service,entid代表entity_id
var serv,entid,dom
//定义开关S1-S8
//1车库2太阳能3水管4大窗帘5小窗帘6落地灯
var s1 = "switch.cheku_switch";
var s2 = "switch.heater_upwater";
var s3 = "switch.konke_minik_2";
var s4 = "cover.ting_cover";
var s5 = "cover.han_cover";
var s6 = "switch.ztc1_1_d0bae4640fce";
var s7 = "switch.mops2_switch";
var s8 = "switch.mops2_switch";

switch (msg.topic) {
    case "bemfa1001" :
        dom = "switch";
        entid = s1;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    case "bemfa2001" :
        dom = "switch";
        entid = s2;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    case "bemfa3001" :
        dom = "switch";
        entid = s3;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    case "bemfa4001" :
        dom = "cover";
        entid = s4;
        if(msg.payload == "on") { serv="open_cover" }
        else {serv="close_cover" }
        break;
    case "bemfa5001" :
        dom = "cover";
        entid = s5;
        if(msg.payload == "on") { serv="open_cover" }
        else {serv="close_cover" }
        break;
    case "bemfa6001" :
        dom = "switch";
        entid = s6;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    case "bemfa7001" :
        dom = "switch";
        entid = s7;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    case "bemfa8001" :
        dom = "switch";
        entid = s8;
        if(msg.payload == "on") { serv="turn_on" }
        else {serv="turn_off" }
        break;
    default : 
        console.log("null");
}
msg.payload = {
  "domain": `${dom}`,
  "service": `${serv}` ,
  "data": {
    "entity_id": `${entid}`
  }
}
return msg;
360截图-26566003.jpg
回复

使用道具 举报

7

主题

188

帖子

2344

积分

金牌会员

Rank: 6Rank: 6

积分
2344
金钱
2156
HASS币
0
发表于 2021-8-3 16:02:22 | 显示全部楼层
前面加mqtt in节点,中间加function节点,后面加call service节点
根据topic不同,确定不同设备,再根据payload是on还是off,确定相应操作
回复

使用道具 举报

20

主题

216

帖子

1458

积分

论坛DIY达人

积分
1458
金钱
1217
HASS币
100
发表于 2021-8-14 20:56:48 | 显示全部楼层
yuyuye 发表于 2021-5-22 17:58
另外再请教下,我每一个插座都做了一个mqtt in + switch + 两个call service,
然后看上去一点不优雅,有啥 ...

实测好用,感谢楼主,和公网不公网没关系。
另外优雅的方法来了。

创建好之后保存成子流程,新增设备只需要拖一个子流程就行了,把相应ID添加进去。

PIC20210814205235001.jpg
PIC20210814205246001.jpg
PIC20210814205443001.jpg

子流程属性里面填入HA的ID,和MQTT的topic就好了。
输入输出不用加东西,我加的 on off 和debug只是测试用的。
[
    {
        "id": "623357d2e876ab06",
        "type": "subflow",
        "name": "巴法连HA",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 120,
                "y": 20,
                "wires": [
                    {
                        "id": "1a6a667787f70d88"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 720,
                "y": 40,
                "wires": [
                    {
                        "id": "ff32644de77c30c0",
                        "port": 0
                    },
                    {
                        "id": "ff32644de77c30c0",
                        "port": 1
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "entity_id",
                "type": "str",
                "value": "entity001"
            },
            {
                "name": "mqtttopic",
                "type": "str",
                "value": "test001"
            }
        ],
        "meta": {},
        "color": "#C7E9C0",
        "icon": "node-red-contrib-home-assistant-websocket/ha-webhook.svg"
    },
    {
        "id": "dffd385bea936724",
        "type": "mqtt in",
        "z": "623357d2e876ab06",
        "name": "",
        "topic": "${mqtttopic}",
        "qos": "2",
        "datatype": "auto",
        "broker": "51136551f880e3a0",
        "nl": false,
        "rap": true,
        "rh": 0,
        "x": 390,
        "y": 140,
        "wires": [
            [
                "ff32644de77c30c0"
            ]
        ]
    },
    {
        "id": "1a6a667787f70d88",
        "type": "mqtt out",
        "z": "623357d2e876ab06",
        "name": "",
        "topic": "${mqtttopic}",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "51136551f880e3a0",
        "x": 390,
        "y": 80,
        "wires": []
    },
    {
        "id": "de3535e203dd44dd",
        "type": "server-state-changed",
        "z": "623357d2e876ab06",
        "name": "次卧主灯",
        "server": "3df55419.f637dc",
        "version": 3,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "${entity_id}",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "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": 200,
        "y": 80,
        "wires": [
            [
                "1a6a667787f70d88"
            ]
        ]
    },
    {
        "id": "25a18cb6126e4b46",
        "type": "api-call-service",
        "z": "623357d2e876ab06",
        "name": "",
        "server": "3df55419.f637dc",
        "version": 3,
        "debugenabled": false,
        "service_domain": "switch",
        "service": "turn_on",
        "entityId": "${entity_id}",
        "data": "",
        "dataType": "jsonata",
        "mergecontext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 770,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "ff32644de77c30c0",
        "type": "switch",
        "z": "623357d2e876ab06",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 570,
        "y": 140,
        "wires": [
            [
                "25a18cb6126e4b46"
            ],
            [
                "d487c7df9f5b9f5c"
            ]
        ]
    },
    {
        "id": "d487c7df9f5b9f5c",
        "type": "api-call-service",
        "z": "623357d2e876ab06",
        "name": "",
        "server": "3df55419.f637dc",
        "version": 3,
        "debugenabled": false,
        "service_domain": "switch",
        "service": "turn_off",
        "entityId": "${entity_id}",
        "data": "",
        "dataType": "jsonata",
        "mergecontext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 770,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "51136551f880e3a0",
        "type": "mqtt-broker",
        "name": "巴法云",
        "broker": "bemfa.com",
        "port": "9501",
        "clientid": "",
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    },
    {
        "id": "3df55419.f637dc",
        "type": "server",
        "name": "Home Assistant",
        "version": 1,
        "legacy": false,
        "addon": true,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": false
    },
    {
        "id": "ac6aa2263f7c09d2",
        "type": "subflow:623357d2e876ab06",
        "z": "9f7bc6d2.1403b8",
        "name": "次卧主灯",
        "env": [
            {
                "name": "entity_id",
                "value": "switch.0x001",
                "type": "str"
            },
            {
                "name": "mqtttopic",
                "value": "ciwozhudeng001",
                "type": "str"
            }
        ],
        "x": 480,
        "y": 500,
        "wires": [
            [
                "f242529bdbe3b3ca"
            ]
        ]
    },
    {
        "id": "25535accd358b15d",
        "type": "inject",
        "z": "9f7bc6d2.1403b8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "on",
        "payloadType": "str",
        "x": 240,
        "y": 480,
        "wires": [
            [
                "ac6aa2263f7c09d2"
            ]
        ]
    },
    {
        "id": "aff08f1e5531f3ec",
        "type": "inject",
        "z": "9f7bc6d2.1403b8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "off",
        "payloadType": "str",
        "x": 250,
        "y": 520,
        "wires": [
            [
                "ac6aa2263f7c09d2"
            ]
        ]
    },
    {
        "id": "f242529bdbe3b3ca",
        "type": "debug",
        "z": "9f7bc6d2.1403b8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 500,
        "wires": []
    }
]
bafa.json (4.46 KB, 下载次数: 13)

回复

使用道具 举报

20

主题

216

帖子

1458

积分

论坛DIY达人

积分
1458
金钱
1217
HASS币
100
发表于 2021-8-14 22:38:54 | 显示全部楼层

你这个函数好,为啥我调用的时候他提示错误呢?
能否把前后的节点也复制出来?"Call-service error. extra keys not allowed @ data['domain']"

call serv的节点怎么设的?
回复

使用道具 举报

7

主题

349

帖子

3227

积分

论坛元老

Rank: 8Rank: 8

积分
3227
金钱
2878
HASS币
0
发表于 2021-8-15 17:41:04 | 显示全部楼层
raspberry0316 发表于 2021-7-6 13:18
我自己就是这么用的呀,你再试试检查检查

大佬,用#订阅真不行。。你怎么做到的。
回复

使用道具 举报

7

主题

349

帖子

3227

积分

论坛元老

Rank: 8Rank: 8

积分
3227
金钱
2878
HASS币
0
发表于 2021-8-15 17:42:35 | 显示全部楼层

非常感谢大佬分享~
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 06:03 , Processed in 0.133797 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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