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

 找回密码
 立即注册
查看: 36776|回复: 59

[流程系列] 好习惯赞一个[持久化变量应用][link][子流程]

  [复制链接]

219

主题

1287

帖子

7945

积分

超级版主

Rank: 8Rank: 8

积分
7945
金钱
6628
HASS币
86

教程狂人论坛风云人物突出贡献

发表于 2018-12-27 20:02:08 | 显示全部楼层 |阅读模式
本帖最后由 lidicn 于 2018-12-27 20:04 编辑

前置需求

  1. 持久化存储node-red变量
  2. 小爱TTS

实现场景及用途

  1. 当幼儿在家表现良好时,小爱TTS表扬,并在计数器内+1
  2. 指定时间,开台灯写作业,赞一个
  3. 指定时间,把换洗衣服放入洗衣机内,赞一个
  4. 指定时间,睡觉,获得赞一个
  5. 为防止作弊,15分钟内只触发一次
  6. 指定时间,播报今天总共获得多少个赞,并提醒睡觉.
  7. 凌晨重置计数器

TIM截图20181227195211.jpg

  1. 本例从天气流程里link out,连接到好习惯流程里面的link in
  2. 实现了跨流程的调用节点,减少重复节点
    TIM截图20181227195416.jpg

子流程

  1. 子流程,可将一些常用的重复使用的节点,简化成节点,方便重复调用
    TIM截图20181227195640.jpg
  2. 子流程会在左边面板出现,跟其他节点一样,双击即可编辑子流程
    TIM截图20181227195904.jpg
[{"id":"3f6343d0.30c6ac","type":"subflow","name":"判断时间段","info":"## 判断时间段\n1. 判断当前时间段,早上/下午/晚上\n2. 输出相应问候语到msg.timerange","category":"","in":[{"x":140,"y":920,"wires":[{"id":"71f3064d.b0f058"}]}],"out":[{"x":400,"y":920,"wires":[{"id":"71f3064d.b0f058","port":0}]}]},{"id":"71f3064d.b0f058","type":"function","z":"3f6343d0.30c6ac","name":"判断时间段","func":"\nvar before_dawn = ['你见过凌晨四点的洛杉矶吗?','夜深了,早点休息'];\nvar morning = ['早上好','早安','早啊','早','Good morning','早上好,送你清新的问候'];\nvar noon = ['中午好','午安','good afternoon'];\nvar afternoon = ['下午好','午安','Good afternoon'];\nvar night = ['晚上好','宁静的夜晚,美丽的天空,丝丝的微风','万家灯火,是夜晚最美的风景'];\nfunction rnd(start, end){\n    return Math.floor(Math.random() * (end - start) + start);\n}\nvar now = new Date();\nvar hour = now.getHours();\nif (hour >= 0 && hour < 5){\n    msg.timerange = before_dawn[rnd(0,before_dawn.length)];\n}\nelse if (hour >= 5 && hour < 11){\n    msg.timerange = morning[rnd(0,morning.length)];\n}\nelse if (hour >=11 && hour < 13){\n    msg.timerange = noon[rnd(0,noon.length)];\n}\nelse if (hour >= 13 && hour < 18){\n    msg.timerange = afternoon[rnd(0,afternoon.length)];\n}\nelse if (hour >= 18 && hour < 24){\n    msg.timerange = night[rnd(0,night.length)];\n}\nelse {\n    msg.timerange = '你好';\n}\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":920,"wires":[[]]},{"id":"9bedb441.78cf58","type":"api-call-service","z":"5085a041.8fbbb","name":"小爱TTS","server":"94f0c0a9.629b6","service_domain":"hello_miai","service":"add2msgqueue","data":"{\"message\":\"好习惯,赞一个!\",\"miai_num\":0}","render_data":false,"mergecontext":"","x":1720,"y":200,"wires":[["52c86c5.0b1a994"]]},{"id":"94601313.6cacb","type":"time-range-switch","z":"5085a041.8fbbb","name":"","lat":"","lon":"","startTime":"16:35","endTime":"20:00","startOffset":0,"endOffset":0,"x":1170,"y":160,"wires":[["704295d.00f426c","ce8252ab.1a0a7"],[]]},{"id":"4a879732.8cec68","type":"server-state-changed","z":"5085a041.8fbbb","name":"台灯状态","server":"94f0c0a9.629b6","entityidfilter":"light.xiaomi_philips_eyecare_smart_lamp_2","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is","x":540,"y":120,"wires":[["7a77e513.27ae7c"]]},{"id":"7a77e513.27ae7c","type":"rbe","z":"5085a041.8fbbb","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":790,"y":160,"wires":[["350b2ee3.06ff72"]]},{"id":"350b2ee3.06ff72","type":"switch","z":"5085a041.8fbbb","name":"门新状态为开启","property":"data.new_state.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":940,"y":160,"wires":[["94601313.6cacb","704295d.00f426c"]]},{"id":"704295d.00f426c","type":"debug","z":"5085a041.8fbbb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1150,"y":60,"wires":[]},{"id":"58f8e136.87d8a","type":"server-state-changed","z":"5085a041.8fbbb","name":"监测洗衣机门磁","server":"94f0c0a9.629b6","entityidfilter":"binary_sensor.door_window_sensor_158d0002b7c289","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is","x":560,"y":220,"wires":[["7a77e513.27ae7c"]]},{"id":"7cb39b17.5b1be4","type":"inject","z":"5085a041.8fbbb","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"50 20 * * *","once":false,"onceDelay":0.1,"x":510,"y":480,"wires":[["c214a507.55fb48"]]},{"id":"eeced851.12b328","type":"function","z":"5085a041.8fbbb","name":"推送内容","func":"\nmsg.payload = {};\nmsg.payload.data = {\"message\":`${msg.timerange},很晚了呀,小朋友你该睡觉了,你今天获得了${flow.get('good_habits','store')}个赞,明天继续努力哦`,\"miai_num\":\"0\"};\nreturn msg;","outputs":1,"noerr":0,"x":860,"y":480,"wires":[["a70381bd.115bb"]]},{"id":"a70381bd.115bb","type":"api-call-service","z":"5085a041.8fbbb","name":"小爱TTS","server":"94f0c0a9.629b6","service_domain":"hello_miai","service":"add2msgqueue","data":"","render_data":false,"mergecontext":"","x":1040,"y":480,"wires":[[]]},{"id":"c214a507.55fb48","type":"subflow:3f6343d0.30c6ac","z":"5085a041.8fbbb","name":"","x":690,"y":480,"wires":[["eeced851.12b328"]]},{"id":"97145b4c.cb1758","type":"inject","z":"5085a041.8fbbb","name":"","topic":"凌晨","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":570,"y":40,"wires":[["bf623101.4d80a"]]},{"id":"bf623101.4d80a","type":"function","z":"5085a041.8fbbb","name":"重置好习惯计数器","func":"\nflow.set('good_habits',0,'store');\n\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":40,"wires":[[]]},{"id":"52c86c5.0b1a994","type":"function","z":"5085a041.8fbbb","name":"好习惯计数器+1","func":"\nflow.set('good_habits',flow.get('good_habits','store')+1,'store');\n\nreturn msg;","outputs":1,"noerr":0,"x":1900,"y":200,"wires":[[]]},{"id":"af76712a.afc79","type":"inject","z":"5085a041.8fbbb","name":"","topic":"晚上九点半","payload":"","payloadType":"date","repeat":"","crontab":"10 23 * * *","once":false,"onceDelay":0.1,"x":550,"y":340,"wires":[["29ecc8da.c68ba8","1663b4e8.6831fb","95a79ad1.a8d0f8","17a4f45d.1e60bc"]]},{"id":"29ecc8da.c68ba8","type":"api-current-state","z":"5085a041.8fbbb","name":"客厅左键大灯关闭","server":"94f0c0a9.629b6","halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"switch.wall_switch_left_158d00014cf47a","state_type":"str","x":850,"y":260,"wires":[["7ac3e57d.11a56c"]]},{"id":"1663b4e8.6831fb","type":"api-current-state","z":"5085a041.8fbbb","name":"客厅右键大灯关闭","server":"94f0c0a9.629b6","halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"switch.wall_switch_right_158d00014cf47a","state_type":"str","x":850,"y":300,"wires":[["7ac3e57d.11a56c"]]},{"id":"95a79ad1.a8d0f8","type":"api-current-state","z":"5085a041.8fbbb","name":"书房射灯","server":"94f0c0a9.629b6","halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"switch.wall_switch_ln_left_158d000239c546","state_type":"str","x":820,"y":340,"wires":[["7ac3e57d.11a56c"]]},{"id":"17a4f45d.1e60bc","type":"api-current-state","z":"5085a041.8fbbb","name":"副厅灯关闭","server":"94f0c0a9.629b6","halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"switch.wall_switch_ln_158d0001fd43d1","state_type":"str","x":830,"y":380,"wires":[["7ac3e57d.11a56c"]]},{"id":"7ac3e57d.11a56c","type":"join","z":"5085a041.8fbbb","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1130,"y":320,"wires":[["532fa3db.d42edc"]]},{"id":"e1725b27.7c6b18","type":"link in","z":"5085a041.8fbbb","name":"好习惯in","links":["a6dd579b.e5a088"],"x":1375,"y":40,"wires":[["e8bddeb2.bbaa5"]]},{"id":"532fa3db.d42edc","type":"trigger","z":"5085a041.8fbbb","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"15","extend":false,"units":"min","reset":"","bytopic":"all","name":"","x":1450,"y":260,"wires":[["9bedb441.78cf58"]]},{"id":"e8bddeb2.bbaa5","type":"trigger","z":"5085a041.8fbbb","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"15","extend":false,"units":"min","reset":"","bytopic":"all","name":"","x":1450,"y":120,"wires":[["9bedb441.78cf58"]]},{"id":"ce8252ab.1a0a7","type":"trigger","z":"5085a041.8fbbb","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"15","extend":false,"units":"min","reset":"","bytopic":"all","name":"","x":1450,"y":200,"wires":[["9bedb441.78cf58"]]},{"id":"94f0c0a9.629b6","type":"server","z":"","name":"Home Assistant","legacy":true,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]


游客,如果您要查看本帖隐藏内容请回复

评分

参与人数 1金钱 +20 收起 理由
+ 20 纳尼,还有这种操作?

查看全部评分

回复

使用道具 举报

8

主题

2073

帖子

6053

积分

论坛元老

流水无味

Rank: 8Rank: 8

积分
6053
金钱
3980
HASS币
145

灌水之王

发表于 2018-12-27 20:55:15 | 显示全部楼层
这个还要跟小的讲好集赞到1000可以买大型玩具
回复

使用道具 举报

219

主题

1287

帖子

7945

积分

超级版主

Rank: 8Rank: 8

积分
7945
金钱
6628
HASS币
86

教程狂人论坛风云人物突出贡献

 楼主| 发表于 2018-12-27 20:57:47 | 显示全部楼层
xieahui 发表于 2018-12-27 20:55
这个还要跟小的讲好集赞到1000可以买大型玩具

每天清0 的,看来要搞一个长期存的
回复

使用道具 举报

8

主题

870

帖子

5218

积分

论坛元老

Rank: 8Rank: 8

积分
5218
金钱
4348
HASS币
0
发表于 2018-12-27 22:06:18 | 显示全部楼层
谢谢分享,66666
回复

使用道具 举报

0

主题

163

帖子

1017

积分

金牌会员

Rank: 6Rank: 6

积分
1017
金钱
854
HASS币
0
发表于 2018-12-28 00:03:34 | 显示全部楼层
本帖最后由 chen1206 于 2018-12-28 00:04 编辑

谢谢分享,这个子流程一直在用,简化美观
回复

使用道具 举报

27

主题

2143

帖子

8928

积分

论坛元老

Rank: 8Rank: 8

积分
8928
金钱
6780
HASS币
30

论坛元老

发表于 2018-12-28 08:45:32 | 显示全部楼层
围观一下
回复

使用道具 举报

6

主题

230

帖子

2128

积分

金牌会员

Rank: 6Rank: 6

积分
2128
金钱
1893
HASS币
20
发表于 2018-12-28 09:14:49 | 显示全部楼层
谢谢分享,66666
回复

使用道具 举报

10

主题

276

帖子

1190

积分

金牌会员

Rank: 6Rank: 6

积分
1190
金钱
914
HASS币
10
发表于 2018-12-28 11:37:37 | 显示全部楼层
这个好!
回复

使用道具 举报

2

主题

230

帖子

1901

积分

金牌会员

Rank: 6Rank: 6

积分
1901
金钱
1671
HASS币
0
发表于 2018-12-28 14:08:18 | 显示全部楼层
感謝分享
回复

使用道具 举报

9

主题

534

帖子

4530

积分

元老级技术达人

积分
4530
金钱
3986
HASS币
60
发表于 2018-12-28 20:21:12 | 显示全部楼层
就是怕大人误触发啊 啊,没办法区分小孩还是大人
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 11:54 , Processed in 0.069857 second(s), 37 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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