请选择 进入手机版 | 继续访问电脑版

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

 找回密码
 立即注册
查看: 6216|回复: 2

经典的传感器方式和蓝牙tracker两种方案判断在家离家场景

[复制链接]

6

主题

34

帖子

719

积分

高级会员

Rank: 4

积分
719
金钱
685
HASS币
10
发表于 2020-9-16 22:09:19 | 显示全部楼层 |阅读模式
本帖最后由 qilihai 于 2020-9-16 22:09 编辑

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

判断离家的自动化
- alias: "if leave home"
  description: "detect movement while door closed in 15minutes"
  initial_state: true
  mode: restart
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
    from: 'on'
    to: 'off'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_select.scenes
        state: '在家'
      - condition: state #防止卧室睡觉时清扫
        entity_id: binary_sensor.door_window_sensor_158d0000e2e2c2 #卧室门传感器
        state: 'on'
  action:
    - delay:
        minutes: 2    
    - wait_template: "{{ is_state('group.movement', 'on') }}"
      timeout: '00:15:00'
      continue_on_timeout: true
    - choose:
        - conditions:
            - condition: state
              entity_id: group.movement
              state: 'off'
          sequence:
            - service: input_select.select_option
              data:
                entity_id: input_select.scenes
                option: '离开'
      default:
        - service: input_select.select_option
          data:
            entity_id: input_select.scenes
            option: '在家'
#判断回家的自动化
- alias: "if back home"
  description: "someone arrive home"
  initial_state: true
  mode: single
  trigger:
    platform: event
    event_type: xiaomi_aqara.motion
    event_data:
      entity_id: binary_sensor.motion_sensor_158d0002b4937f #玄关人体传感器
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
        state: 'on'
      - condition: state
        entity_id: input_select.scenes
        state: '离开'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.scenes
        option: '在家'
groups.yaml
Movement:
  name: 是否有人员移动
  icon: mdi:run
  entities:
    - binary_sensor.motion_sensor_158d0002456d41 #卧室人体
    - binary_sensor.motion_sensor_158d0002c6476d #客厅人体
    - binary_sensor.motion_sensor_158d0003f4c983 #客厅人体2
    - binary_sensor.motion_sensor_158d000447fdae #卫生间人体
    - binary_sensor.motion_sensor_158d0004494186 #厨房人体
    - binary_sensor.motion_sensor_158d0002f2a2d2 #次卧人体
    - binary_sensor.motion_sensor_158d0002b4937f #玄关人体传感器
input_select
input_select:
  scenes: #在家、离开场景选择
    name: '场景'
    options:
      - 在家
      - 离开
    initial: '在家'
    icon: mdi:home-assistant
别忘了重启HA时候的初始化
- alias: 'Select_the_Scene'
  initial_state: true
  trigger:
    - platform: homeassistant
      event: start
  action:
    - wait_template: "{{ is_state('group.family', 'home') }}" #重启HA后5分钟之内如手机接入则为在家
      timeout: '00:05:00'
      continue_on_timeout: true
    - service: input_select.select_option
      data_template:
        entity_id: input_select.scenes
        option: >
          {% if is_state('group.family','home') %}
            在家              
          {% else %}
            离开
          {% 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隔壁老王的手机
device_tracker: #启用蓝牙tracker
  - platform: bluetooth_tracker
    interval_seconds: 12
#    consider_home: 180
    new_device_defaults:
      track_new_devices: false

input_boolean:
input_boolean:
  m9_presence:
    name: if m9 at home
    initial: false
  mate20pro_presence:
    name: if mate20pro at home
    initial: false



automation.yaml
- alias: "mate20pro presence tracking"
  description: "tracking mate20pro BLE signal"
  initial_state: true
  mode: single
  trigger:
    platform: state
    entity_id: device_tracker.huawei_mate_20_pro
  condition:
    condition: not
    conditions:    
      - condition: state
        entity_id: binary_sensor.door_window_sensor_158d00045234d8 #玄关大门传感器
        state: 'off'
        for: '00:05:00'
  action:
    - service_template: "input_boolean.turn_{% if is_state('device_tracker.huawei_mate_20_pro','home')%}on{% else %}off{% endif %}"
      data:
        entity_id: input_boolean.mate20pro_presence   


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





评分

参与人数 1金钱 +20 HASS币 +10 收起 理由
+ 20 + 10 感谢楼主分享!

查看全部评分

回复

使用道具 举报

98

主题

2866

帖子

1万

积分

超级版主

智能家居&单板滑雪痴迷爱好者

Rank: 8Rank: 8

积分
11435
金钱
8504
HASS币
460

教程狂人突出贡献

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

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

我研究过几乎所有的离家回家方案,也试过大部分,最终只有monitor值得长期使用和推荐。
https://bbs.hassbian.com/thread-10096-1-1.html
回复

使用道具 举报

8

主题

43

帖子

620

积分

高级会员

Rank: 4

积分
620
金钱
577
HASS币
0
发表于 2020-9-18 15:30:42 | 显示全部楼层
不错,写得很详细,复制学习下。
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-17 00:43 , Processed in 0.487677 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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