在这个节奏快速的时代,每个人都在追求更高效、更个性化的生活方式。智能家居作为现代生活的一个缩影,它的出现无疑给我们的日常带来了极大的便利。想象一下,清晨的第一缕阳光透过自动开启的窗帘轻轻唤醒你,而那温暖的灯光则是新的一天的开始信号。然而,当这份自动化的便捷在五一这种调休假的时候,按照法定节假日仍旧按部就班地执行时,我们不禁会想:“智能家居,你可不可以也懂得‘假期模式’?”
正因如此,本次教程将带您走进智能家居与节日调休的完美结合。本篇教程将引导您如何通过API查询,让您的智能家居系统能够识别日历上的中国法定工作日和调休时间,实现真正的个性化智能生活。让我们开始吧,让您的智能家居不仅仅是一个程序运行的机器,而是一个能够理解您生活节奏的贴心伴侣。
创建你的自动化流
在开始之前,请确保您已经安装了 Node-RED,并且它能够正常运行在您的设备上。
第一步:导入流程
[{"id":"1185b956a2a0f631","type":"group","z":"42a5241c2622761b","name":"","style":{"label":true},"nodes":["1ea5d9dd6e22283a","b4b4e1995bcf5830","c1e65db71885de08","6a48a8af61f623af","123491cf0da021b0","7bf8bc6e3cc087d1"],"x":114,"y":1159,"w":952,"h":182},{"id":"1ea5d9dd6e22283a","type":"debug","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"工作日判断","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":1240,"wires":[]},{"id":"b4b4e1995bcf5830","type":"function","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"设置日期","func":"let date = new Date();\nlet dateString = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);\nmsg.url = `http://timor.tech/api/holiday/batch?d=${dateString}&type=Y`;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":1300,"wires":[["c1e65db71885de08"]]},{"id":"c1e65db71885de08","type":"http request","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"查询节假日","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":570,"y":1300,"wires":[["7bf8bc6e3cc087d1"]]},{"id":"6a48a8af61f623af","type":"comment","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"中国节假日/调休查询(每日0点自动更新)","info":"","x":300,"y":1200,"wires":[]},{"id":"123491cf0da021b0","type":"inject","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"每天0点","props":[{"p":"payload"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":1300,"wires":[["b4b4e1995bcf5830"]]},{"id":"7bf8bc6e3cc087d1","type":"function","z":"42a5241c2622761b","g":"1185b956a2a0f631","name":"解析结果并存储","func":"let data = msg.payload;\nlet date = new Date();\nlet dateString = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);\n\nif (data.code === 0 && data.holiday[dateString]) {\n // 如果是节假日\n flow.set('isWorkday', false);\n msg.payload = \"今天是节假日\";\n} else {\n // 如果不是节假日\n flow.set('isWorkday', true);\n msg.payload = \"今天不是节假日\";\n}\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":1300,"wires":[["1ea5d9dd6e22283a"]]},{"id":"6141008fe355a91a","type":"group","z":"42a5241c2622761b","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["6a5b5e635422eb99","f4bc753a2cf82b0f","cae1b3036a808fe6","32c947c42711dcc6","7cfe57b7d4bbd715","c718fb559c90c505","d78f0da636e82e71"],"x":114,"y":1359,"w":1132,"h":222},{"id":"6a5b5e635422eb99","type":"inject","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"每天7点","props":[{"p":"payload"}],"repeat":"","crontab":"10 07 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":1460,"wires":[["f4bc753a2cf82b0f"]]},{"id":"f4bc753a2cf82b0f","type":"function","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"检查是否工作日","func":"let isWorkday = flow.get('isWorkday') || false;\nif (isWorkday) {\n // 如果是工作日,发送到第一个输出\n return [{ payload: true }, null];\n} else {\n // 如果是假期/调休,发送到第二个输出\n return [null, { payload: false }];\n}\n","outputs":2,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":1460,"wires":[["cae1b3036a808fe6","32c947c42711dcc6","c718fb559c90c505"],["7cfe57b7d4bbd715"]],"outputLabels":["工作日","假期/调休"]},{"id":"cae1b3036a808fe6","type":"api-call-service","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"设置卧室窗帘打开50%","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"cover","service":"set_cover_position","areaId":[],"deviceId":[],"entityId":["cover.dooya_c1_247e_curtain_2"],"data":"{\"position\": 50}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":1078.333267211914,"y":1484.9998693466187,"wires":[[]]},{"id":"32c947c42711dcc6","type":"api-call-service","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"小爱音响执行 “早上好”指令","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"text","service":"set_value","areaId":[],"deviceId":[],"entityId":["text.xiaomi_l7a_3fb0_execute_text_directive"],"data":"{\"value\":\"早上好!\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1100,"y":1540,"wires":[[]]},{"id":"7cfe57b7d4bbd715","type":"stoptimer","z":"42a5241c2622761b","g":"6141008fe355a91a","duration":"1","units":"Hour","payloadtype":"num","payloadval":"0","name":"如果是休息日,延迟1小时执行","x":670,"y":1540,"wires":[["cae1b3036a808fe6","32c947c42711dcc6","c718fb559c90c505"],[]]},{"id":"c718fb559c90c505","type":"api-call-service","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"打开卧室床头灯","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"homeassistant","service":"turn_on","areaId":[],"deviceId":[],"entityId":["event.unknown1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":1060,"y":1440,"wires":[[]]},{"id":"d78f0da636e82e71","type":"comment","z":"42a5241c2622761b","g":"6141008fe355a91a","name":"工作日每天7点执行起床命令,休息日推迟一个小时(每天8点)执行","info":"","x":400,"y":1400,"wires":[]},{"id":"d7601960.d6be38","type":"server","name":"Home Assistant","addon":true}]
请将上述 JSON 代码复制到您的剪贴板。在 Node-RED 的界面中,点击右上角的菜单按钮(三条横线),选择“导入”。在弹出的窗口中,粘贴您的代码,并点击“导入”。
第二步:检查/理解流程
导入之后,您会看到两个主要的组(group),它们将分别执行以下操作:
中国节假日/调休查询(每日0点自动更新):
每天0点
节点:每天0点触发,请求今天的日期是否为工作日。设置日期
函数节点:构造请求 URL,调用 Timor API 查询。查询节假日
HTTP 请求节点:向 Timor API 发送请求,获取节假日数据。解析结果并存储
函数节点:解析 API 返回的数据,并将结果存储在流程变量中。
工作日每天7点执行起床命令,休息日推迟一个小时(每天8点)执行:
每天7点
节点:根据工作日或节假日状态触发不同的动作。- 检查
是否工作日
函数节点:检查流程变量,确定今天是否为工作日。 - 设置
卧室窗帘打开50%
、小爱音响执行 “早上好”指令
和打开卧室床头灯
节点:这些节点根据工作日状态控制智能设备。 如果是休息日,延迟1小时执行
延时节点:如果今天是休息日,则将执行动作推迟1小时(请按照你休息日的时间调整,也可以直接关闭)。
第三步:调整流程
然后你需要根据你的习惯来调整第二组流
- 更改
每天7点
节点中的时间,来调整工作日命令执行时间 - 更改
如果是休息日,延迟1小时执行
延时节点,在基于工作日时间后的多少分钟/小时执行休息日命令执行时间。 - 更改
call service
节点,或者连接到更多的节点,来执行不同的指令。(我的休息日和节假日是一样的指令,只是时间推迟了,所以精简了下。如果你的工作日和节假日需要执行不同的指令,可以像下图这样修改,连接不同的节点,流程已经放在图片下方了) - 最后别忘了点击
部署
,然后检查并调试整个流程。
[{"id":"a96f8890426da0ca","type":"group","z":"42a5241c2622761b","name":"","style":{"label":true},"nodes":["6682e0306a3dd68b","eef72d9c31e4bcd2","8b2a7a01599f0191","0dc2d2ba637bdd15","4b7e31e0bcc0483c","d4a0cee9d661f122"],"x":114,"y":1779,"w":952,"h":182},{"id":"6682e0306a3dd68b","type":"debug","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"工作日判断","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":1860,"wires":[]},{"id":"eef72d9c31e4bcd2","type":"function","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"设置日期","func":"let date = new Date();\nlet dateString = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);\nmsg.url = `http://timor.tech/api/holiday/batch?d=${dateString}&type=Y`;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":1920,"wires":[["8b2a7a01599f0191"]]},{"id":"8b2a7a01599f0191","type":"http request","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"查询节假日","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":570,"y":1920,"wires":[["d4a0cee9d661f122"]]},{"id":"0dc2d2ba637bdd15","type":"comment","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"中国节假日/调休查询(每日0点自动更新)","info":"","x":300,"y":1820,"wires":[]},{"id":"4b7e31e0bcc0483c","type":"inject","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"每天0点","props":[{"p":"payload"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":1920,"wires":[["eef72d9c31e4bcd2"]]},{"id":"d4a0cee9d661f122","type":"function","z":"42a5241c2622761b","g":"a96f8890426da0ca","name":"解析结果并存储","func":"let data = msg.payload;\nlet date = new Date();\nlet dateString = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);\n\nif (data.code === 0 && data.holiday[dateString]) {\n // 如果是节假日\n flow.set('isWorkday', false);\n msg.payload = \"今天是节假日\";\n} else {\n // 如果不是节假日\n flow.set('isWorkday', true);\n msg.payload = \"今天不是节假日\";\n}\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":1920,"wires":[["6682e0306a3dd68b"]]},{"id":"b710b447c75a02f5","type":"group","z":"42a5241c2622761b","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["5e5e036069a07937","45dd2f65908f2247","367943c52b2ee78b","ecc964890f5894ba","074cf9ff90f99a9d","08041615afe85193"],"x":114,"y":2013,"w":1018,"h":474},{"id":"5e5e036069a07937","type":"inject","z":"42a5241c2622761b","g":"b710b447c75a02f5","name":"每天7点","props":[{"p":"payload"}],"repeat":"","crontab":"10 07 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":2140,"wires":[["45dd2f65908f2247"]]},{"id":"45dd2f65908f2247","type":"function","z":"42a5241c2622761b","g":"b710b447c75a02f5","name":"检查是否工作日","func":"let isWorkday = flow.get('isWorkday') || false;\nif (isWorkday) {\n // 如果是工作日,发送到第一个输出\n return [{ payload: true }, null];\n} else {\n // 如果是假期/调休,发送到第二个输出\n return [null, { payload: false }];\n}\n","outputs":2,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":2140,"wires":[["6faea3feda7f4261","3bc694bf4c6052a0","2c0932823dc0300c"],["367943c52b2ee78b"]],"outputLabels":["工作日","假期/调休"]},{"id":"367943c52b2ee78b","type":"stoptimer","z":"42a5241c2622761b","g":"b710b447c75a02f5","duration":"1","units":"Hour","payloadtype":"num","payloadval":"0","name":"如果是休息日,延迟1小时执行","x":610,"y":2360,"wires":[["16c2c95c6cbe9661"],[]]},{"id":"ecc964890f5894ba","type":"comment","z":"42a5241c2622761b","g":"b710b447c75a02f5","name":"工作日每天7点执行起床命令,休息日推迟一个小时(每天8点)执行","info":"","x":400,"y":2080,"wires":[]},{"id":"074cf9ff90f99a9d","type":"group","z":"42a5241c2622761b","g":"b710b447c75a02f5","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["3bc694bf4c6052a0","6faea3feda7f4261","2c0932823dc0300c","d91212aa2248996e"],"x":812.3332672119141,"y":2039,"w":293.66673278808594,"h":242},{"id":"3bc694bf4c6052a0","type":"api-call-service","z":"42a5241c2622761b","g":"074cf9ff90f99a9d","name":"小爱音响执行 “早上好”指令","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"text","service":"set_value","areaId":[],"deviceId":[],"entityId":["text.xiaomi_l7a_3fb0_execute_text_directive"],"data":"{\"value\":\"早上好!\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":960,"y":2240,"wires":[[]]},{"id":"6faea3feda7f4261","type":"api-call-service","z":"42a5241c2622761b","g":"074cf9ff90f99a9d","name":"设置卧室窗帘打开50%","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"cover","service":"set_cover_position","areaId":[],"deviceId":[],"entityId":["cover.dooya_c1_247e_curtain_2"],"data":"{\"position\": 50}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":938.3332672119141,"y":2184.9998693466187,"wires":[[]]},{"id":"2c0932823dc0300c","type":"api-call-service","z":"42a5241c2622761b","g":"074cf9ff90f99a9d","name":"打开卧室床头灯","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"homeassistant","service":"turn_on","areaId":[],"deviceId":[],"entityId":["event.unknown1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":920,"y":2140,"wires":[[]]},{"id":"d91212aa2248996e","type":"comment","z":"42a5241c2622761b","g":"074cf9ff90f99a9d","name":"工作日","info":"","x":890,"y":2080,"wires":[]},{"id":"d7601960.d6be38","type":"server","name":"Home Assistant","addon":true},{"id":"08041615afe85193","type":"group","z":"42a5241c2622761b","g":"b710b447c75a02f5","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["16c2c95c6cbe9661","9867df89aa8475c3"],"x":814,"y":2319,"w":252,"h":142},{"id":"16c2c95c6cbe9661","type":"api-call-service","z":"42a5241c2622761b","g":"08041615afe85193","name":"设置卧室窗帘打开50%","server":"d7601960.d6be38","version":5,"debugenabled":false,"domain":"cover","service":"set_cover_position","areaId":[],"deviceId":[],"entityId":["cover.dooya_c1_247e_curtain_2"],"data":"{\"position\": 50}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":940,"y":2420,"wires":[[]]},{"id":"9867df89aa8475c3","type":"comment","z":"42a5241c2622761b","g":"08041615afe85193","name":"休息日","info":"","x":890,"y":2360,"wires":[]}]
结尾
现在,您的智能家居系统已经可以根据工作日和节假日自动调整设备状态了。不管是节假日的慵懒早晨,还是工作日的精神饱满,您的家都会提前为您做好准备。
通过本教程,您不仅学会了如何让智能家居系统适应您的生活节奏,还能够根据实际需要调整和扩展这个流程,让智能生活更加符合您的个性化需求。
最后感谢来自“提莫的神秘小站“提供的免费节假日 API