本帖最后由 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
以上两个方案都是判断离家和在家场景,两个方案都有欠缺,人也是一样人无完人。我现在两个方案同时运行下一步看看把这两个方案做一起,其中一个出错通过另一个纠正。
|