> 在 Home Assistant 和许多其他智能家居控制器里,您的家庭状态是在家或者离开,没有中间的状态。
所以,你已经开始了家庭自动化,现在是时候添加一些很酷的自动化功能,比如离开时关闭所有的东西,或者在回家的时候播放音乐。 这些使用存在检测(Presence Detection)实现起来就很容易,但有时你需要更多。
在我的 Home Assistant 设置中,我正在使用蓝牙存在检测。 我的 Synology NAS 运行的 Home Assistant 每 10 秒钟运行一次,检查一下它能“看到”的蓝牙设备。 如果它检测到我的手机,那么它知道我在家。
当我回到家里,并且在一段时间之后,我的房子将通过我的 Sonos 扬声器开始在 Spotify 上播放我的 Chillout 播放列表。只这样做,如果我一个人回家,或者我的未婚妻不在家。当每个人离开时,家庭助理将关闭所有的东西——包括音乐,飞利浦色调灯,电视,加热/冷却等。
我在一个两间卧室的公寓,蓝牙存在检测工作在整个公寓里。当我用手机在我的桌子上时,似乎有个黑点。我会开始工作,写下我的下一个令人敬畏的自动化或代码段,只有 Home Assistant 才能切断电脑显示器的电源并暂停我的音乐。一个巨大的第一个世界问题需要解决。
我可以增加超时,来减少发生这样的事,(我已经完成的部分)甚至使用 WiFi 和蓝牙等多种状态检测选项。 然而,这并不能解决我家做事太快的问题。
我会把手机叫醒,把它放在桌子上,等待家庭助理再次检测到。但问题来了。 在等待我的电话再次被发现的一段时间之后,一切都开始了,好像我离开了几个小时。我的音乐再次变成了我的 Chillout 播放列表(或者从头开始),我不需要的灯可能会重新打开,我得到的信息是洗碗机已经完成等等。此刻,Home Assistant 并不知道 我一分钟前就在家,但也许有一种方法,我们不需要每次都从头开始。
也有些时候,我们可能会离家出走。也许是在乡下的一个周末,或者外出打工。房子应该能够告诉我们,我们不仅仅是一天的时间,所以也许不应该向我们发出电池需要更换的警报。或者,如果我离开很久,但是我的未婚妻在家,也许把这些警报发给她。
在我家里,我想跟踪一个人可能会出现的下列状态。
JUST ARRIVED | 当某人在离开后刚到家时 |
---|---|
HOME | 典型的在家状态 |
JUST LEFT | 当有人刚离开家 |
AWAY | 典型的离开或者不在家 |
EXTENDED AWAY | 如果有人离开超过 24 小时 |
我使用 Home Assistant 输入选择组件,来跟踪我的洗碗机和洗衣机的状态。在这里,我可以重复使用相同的逻辑:
input_select:
phil_status_dropdown:
name: Phil
options:
- Home
- Just Arrived
- Just Left
- Away
- Extended Away
initial: Home
就像我为洗衣机和洗碗机所做的那样,我将使用模板传感器来读取下拉菜单的值。然后,通过将我们的照片添加到传感器,我可以使它看起来像一个正常的设备跟踪器。
configuration.yaml 文件:
sensor:
- platform: template
sensors:
phil_status:
value_template: '{{ states.input_select.phil_status_dropdown.state }}'
friendly_name: 'Phil'
entity_id: input_select.phil_status_dropdown
helen_status:
value_template: '{{ states.input_select.helen_status_dropdown.state }}'
friendly_name: 'Helen'
entity_id: input_select.helen_status_dropdown
groups.yaml
people_status:
name: People Status
entities:
- sensor.phil_status
- sensor.helen_status
customize.yaml
sensor.phil_status:
entity_picture: #URL to picture
sensor.helen_status:
entity_picture: #URL to picture
在 Home Assistant 中,我们可以看到什么时候有人在家或者离开。但是我们现在有一些更复杂的状态。就像我为洗衣机所做的一样,我将使用 for:
条件。 所以一旦有人被标记为远离设备跟踪器,他们将被设置为 “Just Left”。如果他们在 ““Just Left” 状态上五分钟,他们将被标记为 “Away”。 回家时正好相反。
但是我提到的一个问题是,音乐跳回到我的 Chillout 播放列表。让我们确保如果有人被标记为“Just Left”,然后他们的设备跟踪器再次 “see” 他们,而不是将其标记为“Just Arrived”,请将其标记为“Home”。 这将停止任何自动化工作,即那些你第一次回家的时候操作。
这里有一些代码可以做到这一点。
- alias: Mark Phil as just arrived
trigger:
- platform: state
entity_id: device_tracker.phil
from: 'not_home'
to: 'home'
action:
- service: input_select.select_option
data:
entity_id: input_select.phil_status_dropdown
option: Just Arrived
- alias: Mark Phil as home
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Just Arrived'
for:
minutes: 10
- platform: state
entity_id: input_select.phil_status_dropdown
from: 'Just Left'
to: 'Just Arrived'
condition:
action:
- service: input_select.select_option
data:
entity_id: input_select.phil_status_dropdown
option: Home
- alias: Mark Phil as just left
trigger:
- platform: state
entity_id: device_tracker.phil
from: 'home'
to: 'not_home'
action:
- service: input_select.select_option
data:
entity_id: input_select.phil_status_dropdown
option: Just Left
- alias: Mark Phil as away
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Just Left'
for:
minutes: 10
action:
- service: input_select.select_option
data:
entity_id: input_select.phil_status_dropdown
option: Away
我们还想添加特殊的盒子 “Extended Away”。这是有用的,如果有人要离开房子超过一整天,像一个假期。
- alias: Mark Phil as extended away
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Away'
for:
hours: 24
action:
- service: input_select.select_option
data_template:
entity_id: input_select.phil_status_dropdown
option: Extended Away
通过上面的代码块,只是通过这些状态就有五个自动化。如果你家有两个人呢? 您将需要维护的十个自动化。
让我们使用 Home Assistant 模板来使自动化更加动态。我们可以使用触发器变量,来查看哪个设备导致状态改变,然后更新正确人员的下拉列表。 使用模板,我们今天的整个自动化代码可以变成:
- alias: Mark person as just arrived
trigger:
- platform: state
entity_id: device_tracker.phil
from: 'not_home'
to: 'home'
- platform: state
entity_id: group.helen
from: 'not_home'
to: 'home'
action:
- service: input_select.select_option
data_template:
entity_id: >
{% if trigger.entity_id == 'device_tracker.phil' %}
input_select.phil_status_dropdown
{% else %}
input_select.helen_status_dropdown
{% endif %}
option: Just Arrived
- alias: Mark person as home
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Just Arrived'
for:
minutes: 10
- platform: state
entity_id: input_select.helen_status_dropdown
to: 'Just Arrived'
for:
minutes: 10
- platform: state
entity_id: input_select.phil_status_dropdown
from: 'Just Left'
to: 'Just Arrived'
- platform: state
entity_id: input_select.helen_status_dropdown
from: 'Just Left'
to: 'Just Arrived'
action:
- service: input_select.select_option
data_template:
entity_id: >
{% if trigger.entity_id == 'input_select.phil_status_dropdown' %}
input_select.phil_status_dropdown
{% else %}
input_select.helen_status_dropdown
{% endif %}
option: Home
- alias: Mark person as just left
trigger:
- platform: state
entity_id: device_tracker.phil
from: 'home'
to: 'not_home'
- platform: state
entity_id: group.helen
from: 'home'
to: 'not_home'
action:
- service: input_select.select_option
data_template:
entity_id: >
{% if trigger.entity_id == 'device_tracker.phil' %}
input_select.phil_status_dropdown
{% else %}
input_select.helen_status_dropdown
{% endif %}
option: Just Left
- alias: Mark person as away
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Just Left'
for:
minutes: 10
- platform: state
entity_id: input_select.helen_status_dropdown
to: 'Just Left'
for:
minutes: 10
action:
- service: input_select.select_option
data_template:
entity_id: >
{% if trigger.entity_id == 'input_select.phil_status_dropdown' %}
input_select.phil_status_dropdown
{% else %}
input_select.helen_status_dropdown
{% endif %}
option: Away
- alias: Mark person as extended away
trigger:
- platform: state
entity_id: input_select.phil_status_dropdown
to: 'Away'
for:
hours: 24
- platform: state
entity_id: input_select.helen_status_dropdown
to: 'Away'
for:
hours: 24
action:
- service: input_select.select_option
data_template:
entity_id: >
{% if trigger.entity_id == 'input_select.phil_status_dropdown' %}
input_select.phil_status_dropdown
{% else %}
input_select.helen_status_dropdown
{% endif %}
option: Extended Away
注意:我正在为我的未婚妻的 iPhone 使用蓝牙和 WiFi 进行存在检测,这是我在 Home Assistant 组中使用的。 当在蓝牙或WiFi上检测到她的 iPhone 时,Helen 将被标记为 “Home” 状态。
现在我们已经有了新的状态设置,下面是我们可以做的一些自动化的例子:
目前,这五个状态对我来说工作很好,但我可能想在未来改变他们。请记住,他们可能不是最适合你的,但你可以量身定做,以适应自己的家。
我不使用此功能,但 Home Assistant 可以与GPS一起使用来识别 “zones”,例如工作或学校。Home Assistant使用这些区域而不是“home”或“not home”。
你也可以做一些类似的下拉菜单。一个例子是添加一个 work 选项到下拉菜单。 如果您的 Home Assistant 面向全世界,您可以使用 Android上 的 Tasker 来更改下拉菜单的状态,以便在连接到您工作的 WiFi 时使用。如果你有一个 Eight Sleep tracker 睡眠追踪器的东西,你也可以标记某人躺在床上时 “asleep”。
通过添加额外的状态来显示,您的家庭获得更多有关正在发生的事情的信息。这意味着你的智能家居,可以告诉你什么时候从假期到家,何时从商店里拿起牛奶。
希望这能激发你在控制器软件的二元约束之外思考,帮助你构建一些真正的智能自动化。
原文链接:https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/
观光\评论区