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

标题: 经典的传感器方式和蓝牙tracker两种方案判断在家离家场景 [打印本页]

作者: qilihai    时间: 2020-9-16 22:09
标题: 经典的传感器方式和蓝牙tracker两种方案判断在家离家场景
本帖最后由 qilihai 于 2020-9-16 22:09 编辑

第一次一(胡)番(说)正(八)经(道)的发帖子。如何准确的判断离家和在家一直是个难点,我写了这两种方案每个都可以单独运行,因为每个人的情况不一样,单一方案很难适合所有人,仅做参考,涉及的知识点全部可以在HA的官方文档里找到。
第一种方案:传感器判断离家在家,原理就是一个判断在家一个判断离家的自动化,当大门关闭之后延迟两分钟,然后检查所有的人体传感器在15分钟内是否监测到人体移动,如果无人移动就算离家。这里有个bug就是当睡觉时候,我家习惯关门睡觉,所以condition中有个例外,就是卧室门关闭就代表有人在家睡觉。
知识点:group,input_select,choose,wait_template
automation.yaml

  1. 判断离家的自动化
  2. - alias: "if leave home"
  3.   description: "detect movement while door closed in 15minutes"
  4.   initial_state: true
  5.   mode: restart
  6.   trigger:
  7.     platform: state
  8.     entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
  9.     from: 'on'
  10.     to: 'off'
  11.   condition:
  12.     condition: and
  13.     conditions:
  14.       - condition: state
  15.         entity_id: input_select.scenes
  16.         state: '在家'
  17.       - condition: state #防止卧室睡觉时清扫
  18.         entity_id: binary_sensor.door_window_sensor_158d0000e2e2c2 #卧室门传感器
  19.         state: 'on'
  20.   action:
  21.     - delay:
  22.         minutes: 2   
  23.     - wait_template: "{{ is_state('group.movement', 'on') }}"
  24.       timeout: '00:15:00'
  25.       continue_on_timeout: true
  26.     - choose:
  27.         - conditions:
  28.             - condition: state
  29.               entity_id: group.movement
  30.               state: 'off'
  31.           sequence:
  32.             - service: input_select.select_option
  33.               data:
  34.                 entity_id: input_select.scenes
  35.                 option: '离开'
  36.       default:
  37.         - service: input_select.select_option
  38.           data:
  39.             entity_id: input_select.scenes
  40.             option: '在家'
  41. #判断回家的自动化
  42. - alias: "if back home"
  43.   description: "someone arrive home"
  44.   initial_state: true
  45.   mode: single
  46.   trigger:
  47.     platform: event
  48.     event_type: xiaomi_aqara.motion
  49.     event_data:
  50.       entity_id: binary_sensor.motion_sensor_158d0002b4937f #玄关人体传感器
  51.   condition:
  52.     condition: and
  53.     conditions:
  54.       - condition: state
  55.         entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
  56.         state: 'on'
  57.       - condition: state
  58.         entity_id: input_select.scenes
  59.         state: '离开'
  60.   action:
  61.     - service: input_select.select_option
  62.       data:
  63.         entity_id: input_select.scenes
  64.         option: '在家'
复制代码
groups.yaml
  1. Movement:
  2.   name: 是否有人员移动
  3.   icon: mdi:run
  4.   entities:
  5.     - binary_sensor.motion_sensor_158d0002456d41 #卧室人体
  6.     - binary_sensor.motion_sensor_158d0002c6476d #客厅人体
  7.     - binary_sensor.motion_sensor_158d0003f4c983 #客厅人体2
  8.     - binary_sensor.motion_sensor_158d000447fdae #卫生间人体
  9.     - binary_sensor.motion_sensor_158d0004494186 #厨房人体
  10.     - binary_sensor.motion_sensor_158d0002f2a2d2 #次卧人体
  11.     - binary_sensor.motion_sensor_158d0002b4937f #玄关人体传感器
复制代码
input_select
  1. input_select:
  2.   scenes: #在家、离开场景选择
  3.     name: '场景'
  4.     options:
  5.       - 在家
  6.       - 离开
  7.     initial: '在家'
  8.     icon: mdi:home-assistant
复制代码
别忘了重启HA时候的初始化
  1. - alias: 'Select_the_Scene'
  2.   initial_state: true
  3.   trigger:
  4.     - platform: homeassistant
  5.       event: start
  6.   action:
  7.     - wait_template: "{{ is_state('group.family', 'home') }}" #重启HA后5分钟之内如手机接入则为在家
  8.       timeout: '00:05:00'
  9.       continue_on_timeout: true
  10.     - service: input_select.select_option
  11.       data_template:
  12.         entity_id: input_select.scenes
  13.         option: >
  14.           {% if is_state('group.family','home') %}
  15.             在家              
  16.           {% else %}
  17.             离开
  18.           {% endif %}
复制代码
第二种方案:device_tracker通过检测手机蓝牙信号判断是否在家,我用树莓派4B安装的Hassos,树莓派4B自带蓝牙5.0只要手机开了蓝牙就能检测到手机是否在家。蓝牙的tracker会比wifi的tracker好用一些,wifi有一个缺点是需要唤醒才能连上路由,树莓派的蓝牙连接很快,1分钟之内就能连上。缺点就是只能检测手机在不在家,如果认为不带手机出门不算离家(因为不会走远会很快回家)那也是可以接受的。我在自动化里特意规避了房屋角落蓝牙信号弱或者关机导致蓝牙断开的情况,用了一个input_boolean记录device_tracker的状态,当device_tracker状态变化时检查入户大门的情况,如果蓝牙状态改变就检查5分钟之前大门有没有动作,没动作就不记录保持input_boolean上一个状态。我的树莓派放置在客厅必经之处,一个树莓派信号能覆盖八九十平米中小户型,用起来还算OK
知识点:device_tracker,input_boolean
先configuration.yaml启用device_tracker,启用后会生成known_devices.yaml,里面找到自己的手机,最后修改track_new_devices为false不希望tracking隔壁老王的手机
  1. device_tracker: #启用蓝牙tracker
  2.   - platform: bluetooth_tracker
  3.     interval_seconds: 12
  4. #    consider_home: 180
  5.     new_device_defaults:
  6.       track_new_devices: false
复制代码

input_boolean:
  1. input_boolean:
  2.   m9_presence:
  3.     name: if m9 at home
  4.     initial: false
  5.   mate20pro_presence:
  6.     name: if mate20pro at home
  7.     initial: false
复制代码



automation.yaml
  1. - alias: "mate20pro presence tracking"
  2.   description: "tracking mate20pro BLE signal"
  3.   initial_state: true
  4.   mode: single
  5.   trigger:
  6.     platform: state
  7.     entity_id: device_tracker.huawei_mate_20_pro
  8.   condition:
  9.     condition: not
  10.     conditions:   
  11.       - condition: state
  12.         entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
  13.         state: 'off'
  14.         for: '00:05:00'
  15.   action:
  16.     - service_template: "input_boolean.turn_{% if is_state('device_tracker.huawei_mate_20_pro','home')%}on{% else %}off{% endif %}"
  17.       data:
  18.         entity_id: input_boolean.mate20pro_presence   
复制代码


以上两个方案都是判断离家和在家场景,两个方案都有欠缺,人也是一样人无完人。我现在两个方案同时运行下一步看看把这两个方案做一起,其中一个出错通过另一个纠正。






作者: XCray    时间: 2020-9-17 07:26
楼主的方案显然经过不少思考,但仍然有明显的适用性限制。比如家里有宠物的,比如睡觉不关门的。另外在安防方面也无能为力(判断主人离家回家)。

不过你这么喜欢琢磨,不如看看我最为推荐的方案——monitor,是国外一牛人写的。

我研究过几乎所有的离家回家方案,也试过大部分,最终只有monitor值得长期使用和推荐。
https://bbs.hassbian.com/thread-10096-1-1.html
作者: hgy327    时间: 2020-9-18 15:30
不错,写得很详细,复制学习下。




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5