bestsort 发表于 2024-2-19 15:38:46

判断是否节假日并持久化

简单来说流程如下1. 调用api
2. 写入HA的辅助元素


1. https://www.tianapi.com/apiview/139 注册个开发者账号申请下api 获取token key(100次调用/天,够用)
2. ha中新增个虚拟开关



3.导入流程
[
    {
      "id": "c47395601e1189fd",
      "type": "inject",
      "z": "18f3d6ef71710367",
      "name": "每天早上00:01",
      "props": [
            {
                "p": "token",
                "v": "YOUR_TOKEN_KEY",
                "vt": "str"
            }
      ],
      "repeat": "",
      "crontab": "01 00 * * *",
      "once": false,
      "onceDelay": 0.1,
      "topic": "",
      "x": 180,
      "y": 400,
      "wires": [
            [
                "8974b6d77d75f52a"
            ]
      ]
    },
    {
      "id": "8974b6d77d75f52a",
      "type": "function",
      "z": "18f3d6ef71710367",
      "name": "获取当前日期",
      "func": "// Function节点中的代码\nlet token = msg.token;\nlet now = new Date();\nlet year = now.getFullYear();\nlet month = (\"0\" + (now.getMonth() + 1)).slice(-2); // 加1因为月份是从0开始的\nlet day = (\"0\" + now.getDate()).slice(-2);\nlet formattedDate = year + \"-\" + month + \"-\" + day;\n\nmsg.url = `https://apis.tianapi.com/jiejiari/index?key=${token}&type=0&date=${formattedDate}`;\n\nmsg.payload = formattedDate;\nreturn msg;\n",
      "outputs": 1,
      "timeout": 0,
      "noerr": 0,
      "initialize": "",
      "finalize": "",
      "libs": [],
      "x": 420,
      "y": 420,
      "wires": [
            [
                "337cbee97c05d3e9"
            ]
      ]
    },
    {
      "id": "337cbee97c05d3e9",
      "type": "http request",
      "z": "18f3d6ef71710367",
      "name": "今天是否是节假日",
      "method": "GET",
      "ret": "obj",
      "paytoqs": "ignore",
      "url": "",
      "tls": "",
      "persist": false,
      "proxy": "",
      "insecureHTTPParser": false,
      "authType": "",
      "senderr": false,
      "headers": [],
      "x": 630,
      "y": 440,
      "wires": [
            [
                "2f90222899418958"
            ]
      ]
    },
    {
      "id": "2f90222899418958",
      "type": "switch",
      "z": "18f3d6ef71710367",
      "name": "需要上班",
      "property": "payload.result.list.isnotwork",
      "propertyType": "msg",
      "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
      ],
      "checkall": "true",
      "repair": false,
      "outputs": 2,
      "x": 860,
      "y": 460,
      "wires": [
            [
                "ea836bd9da07fa1e"
            ],
            [
                "6485aea7e97c015c"
            ]
      ]
    },
    {
      "id": "ea836bd9da07fa1e",
      "type": "api-call-service",
      "z": "18f3d6ef71710367",
      "name": "需要上班",
      "server": "69621d87.7af904",
      "version": 5,
      "debugenabled": false,
      "domain": "input_boolean",
      "service": "turn_on",
      "areaId": [],
      "deviceId": [],
      "entityId": [
            "input_boolean.ying_gai_shang_ban"
      ],
      "data": "",
      "dataType": "jsonata",
      "mergeContext": "",
      "mustacheAltTags": false,
      "outputProperties": [],
      "queue": "none",
      "x": 1040,
      "y": 460,
      "wires": [
            []
      ]
    },
    {
      "id": "6485aea7e97c015c",
      "type": "api-call-service",
      "z": "18f3d6ef71710367",
      "name": "不需要上班",
      "server": "69621d87.7af904",
      "version": 5,
      "debugenabled": false,
      "domain": "input_boolean",
      "service": "turn_on",
      "areaId": [],
      "deviceId": [],
      "entityId": [
            "input_boolean.ying_gai_shang_ban"
      ],
      "data": "",
      "dataType": "jsonata",
      "mergeContext": "",
      "mustacheAltTags": false,
      "outputProperties": [],
      "queue": "none",
      "x": 1050,
      "y": 520,
      "wires": [
            []
      ]
    },
    {
      "id": "69621d87.7af904",
      "type": "server",
      "name": "HA",
      "addon": false,
      "rejectUnauthorizedCerts": false,
      "ha_boolean": "y|yes|true|on|home|open|工作日",
      "connectionDelay": true,
      "cacheJson": false,
      "heartbeat": true,
      "heartbeatInterval": "30",
      "areaSelector": "id",
      "deviceSelector": "id",
      "entitySelector": "id",
      "statusSeparator": "",
      "statusYear": "2-digit",
      "statusMonth": "2-digit",
      "statusDay": "2-digit",
      "statusHourCycle": "h23",
      "statusTimeFormat": "h:m:s",
      "enableGlobalContextStore": false
    }
]4. 修改第一步中的TOKEN为你自己的, 修改HA配置。搞定

5. 要用时直接获取entity状态判断是否需要上班




mitu 发表于 2024-2-19 18:06:32

直接用sensor里rest会更好一些
https://www.home-assistant.io/integrations/sensor.rest/

bestsort 发表于 2024-2-20 20:15:20

mitu 发表于 2024-2-19 18:06
直接用sensor里rest会更好一些
https://www.home-assistant.io/integrations/sensor.rest/

还不是很习惯用ha,所以流程都丢nodered里的
页: [1]
查看完整版本: 判断是否节假日并持久化