スマホの Ping(ICMP) の binary_sensor では、スマホがスリープ状態になったりするタイミングがあって、単純な ping では接続/切断が発生して、在宅/外出が正しく検出できない。
Ping(ICMP)の設定には consider_home で『デバイスが応答しなくなってから「切断」と判定するまでの待ち時間』を設定することができるが、エンティティとして扱える binary_sensor には consider_home は効き目がない。調べる中で device_tracker を有効にする必要があることが分かったけど、有効設定の画面にたどり着くのにひと手間かかった。
ようやく、device_tracker が使えるようになり、在宅/外出の判定がうまくできるようになり、オートメーションの設定。
Gemini が automation.yaml で設定する方法を解説してくれたけど、自分が在宅しているかどうかで色々と通知を切り分けたかったので、オートメーションの UI での登録はちょい悩んだけど…
オートメーションの例
UIで設定した結果を、YAML形式で表示
# shell_command.sh_mail_notify_message - メールで送る shell_command
# shell_command.sh_google_notify_message - Google Home で話させる shell_command
alias: とーるの帰宅/外出を通知
description: ""
triggers:
- trigger: state
entity_id:
- device_tracker.iphone_xxxx
conditions: []
actions:
- action: shell_command.sh_mail_notify_message
metadata: {}
data:
to: t-saitoh
message: >-
とーるが
{% if states('device_tracker.iphone_xxxx') == 'home' %}
帰宅/{{ now().strftime('%H:%M') }}
{% else %}
外出{{ (now() - timedelta(seconds=450)).strftime('%H:%M') }}
{% endif %}
- if:
- condition: state
entity_id: device_tracker.iphone_yyyy
state:
- home
then:
- action: shell_command.sh_google_notify_message
metadata: {}
data:
message: >-
とーるが
{% if states('device_tracker.iphone_xxxx') == 'home' %}
帰宅
{% else %}
外出
{% endif %}
しました
mode: single