|
发表于 2021-11-24 13:45:57
|
显示全部楼层
我的判断比较复杂, 贴个package文件片段, 每个家庭成员的状态(到家,在家,短暂离开,离开, 不在家), 在家人数统计, 家里有人, 家里所有人都离开, 各个状态适用不同的场景。 到家状态主要通过地理位置、开门、智能锁、wifi跟踪多手段确认,对于我基本够用了。
input_select:
tgm_status_dropdown:
options:
- Home
- Arrived
- Left
- Away
- Extended
initial: Home
sensor:
- platform: template
sensors:
tgm_status:
value_template: "{{ states.input_select.tgm_status_dropdown.state }}"
friendly_name: 'tgm'
# people_home_count: 在家人数
people_home_count:
friendly_name: '在家人数'
value_template: >
{% set count = 0 %}
{% if is_state("sensor.tgm_status", "Home") %}
{% set count = count + 1 %}
{% endif %}
{% if is_state("sensor.x01_status", "Home") %}
{% set count = count + 1 %}
{% endif %}
{% if is_state("sensor.x02_status", "Home") %}
{% set count = count + 1 %}
{% endif %}
{{count}}
# 在家人数>0 is_people_home:true 不然 false 确认是否有人在家用
is_people_home:
friendly_name: '在家'
value_template: >
{% set state = true %}
{% if is_state("sensor.people_home_count", "0") %}
{% set state = false %}
{% endif %}
{{state}}
# 家里是否无人,都已离开 true /false
is_people_away:
friendly_name: '离家'
value_template: >
{% set state = false %}
{%
if is_state("sensor.people_home_count", "0")
and (is_state("sensor.tgm_status", "Away") or is_state("sensor.tgm_status", "Extended"))
and (is_state("sensor.x01_status", "Away") or is_state("sensor.x01_status", "Extended"))
and (is_state("sensor.x02_status", "Away") or is_state("sensor.x02_status", "Extended"))
%}
{% set state = true %}
{% endif %}
{{state}}
# 确定person 是否在家,因person 可能状态为其它zone,故用此判断是否home,not_home
person_tgm_state:
value_template: >
{% set state = "not_home" %}
{% if is_state("person.tgm", "home") %}
{% set state = "home" %}
{% endif %}
{{state}}
automation:
######################################
# Arrived 到家
# Home 在家 (到家后600秒,或开门确认到家)
# Left 短暂离开(小于10分钟,可能的wifi暂时不在线,连不上,卫星漂移等情况)
# Away 离家 (离开10分钟后)
# Extended 不在家 (离家24hour)
#######################################
- alias: Mark tgm as just arrived
initial_state: true
trigger:
- platform: state
entity_id: sensor.person_tgm_state
from: not_home
to: home
condition:
condition: or
conditions:
- condition: state
entity_id: sensor.tgm_status
state: Away
- condition: state
entity_id: sensor.tgm_status
state: Extended
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Arrived
- alias: Mark tgm as home
initial_state: false
trigger:
- platform: state
entity_id: input_select.tgm_status_dropdown
to: Arrived
for:
seconds: 600
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Home
- alias: Mark tgm as home4
initial_state: true
trigger:
- platform: state
entity_id: sensor.lock_opener
to: tgm
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Home
- alias: Mark tgm as home3
initial_state: true
trigger:
- platform: state
entity_id: sensor.person_tgm_state
from: not_home
to: home
condition:
condition: state
entity_id: sensor.tgm_status
state: Left
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Home
- alias: Mark tgm as home2
initial_state: true
trigger:
- platform: state
entity_id: binary_sensor.door_window_sensor_158d000233169f
from: 'off'
to: 'on'
condition:
condition: state
entity_id: input_select.tgm_status_dropdown
state: Arrived
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Home
- alias: Mark tgm as just left
initial_state: true
trigger:
- platform: state
entity_id: sensor.person_tgm_state
from: home
to: not_home
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Left
- alias: Mark tgm as away
initial_state: true
trigger:
- platform: state
entity_id: input_select.tgm_status_dropdown
to: Left
for:
minutes: 10
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Away
- alias: Mark tgm as away2
initial_state: true
trigger:
- platform: homeassistant
event: start
condition:
condition: state
entity_id: sensor.person_tgm_state
state: not_home
action:
- service: input_select.select_option
data:
entity_id: input_select.tgm_status_dropdown
option: Away
- alias: Mark tgm as extended away
initial_state: true
trigger:
- platform: state
entity_id: input_select.tgm_status_dropdown
to: Away
for:
hours: 24
action:
- service: input_select.select_option
data_template:
entity_id: input_select.tgm_status_dropdown
option: Extended |
|