การเชื่อมต่อกับ Home Assistant - แจ้งเตือนบ้านอัจฉริยะ
เชื่อม Home Assistant เข้ากับ Echobell เพื่อรับการแจ้งเตือนบ้านอัจฉริยะทันที: ความปลอดภัย การเคลื่อนไหว อุณหภูมิ และอีเวนต์ระบบอัตโนมัติ เป็นพุชหรือสายโทรเข้า
Home Assistant เป็นแพลตฟอร์มระบบอัตโนมัติในบ้านแบบโอเพนซอร์สที่ทรงพลัง โดยให้ความสำคัญกับการควบคุมภายในเครือข่ายและความเป็นส่วนตัวเป็นอันดับแรก เมื่อเชื่อม Echobell เข้ากับ Home Assistant คุณจะได้รับการแจ้งเตือนทันทีหรือแม้แต่สายโทรเข้าเมื่อเกิดเหตุการณ์สำคัญในบ้านอัจฉริยะของคุณ ไม่ว่าจะเป็นการแจ้งเตือนด้านความปลอดภัย อุณหภูมิเกินเกณฑ์ หรือทริกเกอร์จากระบบอัตโนมัติ
คู่มือนี้จะพาคุณตั้งค่า Echobell ให้รับการแจ้งเตือนจากระบบอัตโนมัติของ Home Assistant ด้วย webhook หรือบริการ RESTful Notify
สิ่งที่ต้องเตรียม
ก่อนเริ่ม ตรวจสอบให้แน่ใจว่าคุณมี:
- ติดตั้ง แอป Echobell / Google Play บนอุปกรณ์มือถือของคุณแล้ว
- ช่อง Echobell ที่ใช้งานอยู่ (สร้างในแอปได้หากยังไม่มี)
- อินสแตนซ์ Home Assistant ที่ทำงานอยู่ (แนะนำเวอร์ชัน 2023.1 ขึ้นไป)
- สิทธิ์เข้าถึงไฟล์ตั้งค่าของ Home Assistant
วิธีที่ 1: ใช้ทริกเกอร์แบบ Webhook (แนะนำ)
วิธี webhook ยืดหยุ่นที่สุด เพราะช่วยให้คุณส่งข้อมูลแบบกำหนดเองและใช้ ตัวแปรในเทมเพลต ในการแจ้งเตือนได้
รับ Webhook URL ของ Echobell
- เปิดแอป Echobell แล้วไปที่ช่องของคุณ
- แตะที่ช่องนั้นเพื่อดูรายละเอียด
- หา Webhook URL ในส่วน Triggers
- คัดลอก URL ซึ่งจะมีหน้าตาแบบนี้:
https://hook.echobell.one/t/<channel-token>
สร้างระบบอัตโนมัติใน Home Assistant
ใน Home Assistant คุณทริกเกอร์การแจ้งเตือนของ Echobell ได้ด้วยบริการ rest_command ก่อนอื่นให้เพิ่มคำสั่ง REST ลงใน configuration.yaml ของคุณ:
rest_command:
echobell_notify:
url: "https://hook.echobell.one/t/<channel-token>"
method: POST
content_type: "application/json"
payload: '{"title": "{{ title }}", "message": "{{ message }}"}'แทนที่ YOUR_TOKEN ด้วยโทเคน webhook ของ Echobell ของคุณจริง ๆ
หลังเพิ่มเสร็จ ให้รีสตาร์ต Home Assistant เพื่อโหลดการตั้งค่าใหม่
ใช้งานในระบบอัตโนมัติ
ตอนนี้คุณเรียกใช้บริการนี้จากระบบอัตโนมัติใดก็ได้ นี่คือตัวอย่างการแจ้งเตือนเมื่อประตูถูกเปิด:
automation:
- alias: "Door Open Alert"
trigger:
- platform: state
entity_id: binary_sensor.front_door
to: "on"
action:
- service: rest_command.echobell_notify
data:
title: "Security Alert"
message: "Front door opened at {{ now().strftime('%H:%M') }}"ตั้งค่าเทมเพลตการแจ้งเตือน
ในหน้าตั้งค่าช่อง Echobell ของคุณ ให้ตั้งเทมเพลตการแจ้งเตือนเพื่อแสดงข้อมูลจาก webhook:
- เทมเพลตหัวข้อ:
{{title}} - เทมเพลตเนื้อหา:
{{message}}
ตอนนี้เมื่อระบบอัตโนมัติทำงาน คุณจะได้รับการแจ้งเตือนพร้อมหัวข้อและข้อความที่กำหนดเอง
วิธีที่ 2: ใช้แพลตฟอร์ม RESTful Notify
หากต้องการวิธีที่กลมกลืนกับระบบมากกว่า คุณตั้งค่า Echobell เป็นบริการ notify ใน Home Assistant ได้
การตั้งค่า
เพิ่มข้อความต่อไปนี้ลงใน configuration.yaml ของคุณ:
notify:
- name: echobell
platform: rest
resource: https://hook.echobell.one/t/<channel-token>
method: POST_JSON
data:
title: "{{ title }}"
message: "{{ message }}"
แทนที่ YOUR_TOKEN ด้วยโทเคน webhook ของ Echobell ของคุณ แล้วรีสตาร์ต Home Assistant
การใช้บริการ Notify
ตอนนี้คุณใช้ Echobell ได้เหมือนบริการ notify อื่น ๆ:
automation:
- alias: "Low Battery Alert"
trigger:
- platform: numeric_state
entity_id: sensor.phone_battery
below: 20
action:
- service: notify.echobell
data:
title: "Low Battery Warning"
message: "Phone battery is at {{ states('sensor.phone_battery') }}%"
กรณีใช้งานขั้นสูง
มอนิเตอร์อุณหภูมิพร้อมการแจ้งเตือนขั้นวิกฤต
ใช้ ประเภทการแจ้งเตือน เพื่อส่งการแจ้งเตือนอุณหภูมิขั้นวิกฤตเป็นสายโทรเข้า:
rest_command:
echobell_critical:
url: "https://hook.echobell.one/t/<channel-token>"
method: POST
content_type: "application/json"
payload: '{"temperature": "{{ temperature }}", "location": "{{ location }}", "severity": "critical"}'
ในช่อง Echobell ของคุณ ให้ตั้ง ประเภทการแจ้งเตือน เป็น "โทรเข้า" แล้วใช้เทมเพลตนี้:
- หัวข้อ:
Critical Temperature Alert - เนื้อหา:
{{location}} temperature is {{temperature}}°C - Immediate attention required!
การเชื่อมต่อกับระบบรักษาความปลอดภัย
ตั้งค่าการมอนิเตอร์ความปลอดภัยแบบครบวงจร:
automation:
- alias: "Security Breach Detection"
trigger:
- platform: state
entity_id:
- binary_sensor.motion_detector_1
- binary_sensor.motion_detector_2
- binary_sensor.window_sensor
to: "on"
condition:
- condition: state
entity_id: alarm_control_panel.home_alarm
state: "armed_away"
action:
- service: rest_command.echobell_notify
data:
title: "🚨 Security Alert"
message: "{{ trigger.to_state.attributes.friendly_name }} detected activity while system armed"
sensor: "{{ trigger.entity_id }}"
timestamp: "{{ now().isoformat() }}"
การแจ้งเตือนจากเครื่องใช้ไฟฟ้าอัจฉริยะ
ติดตามรอบการทำงานของเครื่องใช้ไฟฟ้าและรับแจ้งเตือนเมื่อทำงานเสร็จ:
automation:
- alias: "Washing Machine Complete"
trigger:
- platform: state
entity_id: sensor.washing_machine_power
to: "0"
for:
minutes: 3
action:
- service: notify.echobell
data:
title: "Washing Machine Done"
message: "Your laundry is ready to be moved to the dryer"
การมอนิเตอร์สภาพแวดล้อม
ติดตามคุณภาพอากาศ ความชื้น หรือปัจจัยแวดล้อมอื่น ๆ:
automation:
- alias: "High CO2 Alert"
trigger:
- platform: numeric_state
entity_id: sensor.living_room_co2
above: 1000
action:
- service: rest_command.echobell_notify
data:
title: "Air Quality Warning"
message: "CO2 level in living room is {{ states('sensor.living_room_co2') }} ppm. Consider ventilating."
ใช้เงื่อนไขเพื่อกรองอย่างชาญฉลาด
คุณใช้ เงื่อนไขของ Echobell เพื่อกรองการแจ้งเตือนตามเกณฑ์ที่กำหนดได้ ตัวอย่างเช่น แจ้งเตือนเฉพาะบางช่วงเวลา หรือเฉพาะเมื่อค่าเกินเกณฑ์:
ตั้งเงื่อนไขในช่อง Echobell ของคุณ:
severity == "critical" || (hour >= 22 || hour <= 7)
เงื่อนไขนี้จะส่งการแจ้งเตือนก็ต่อเมื่อระดับความรุนแรงเป็น critical หรืออยู่ในช่วงกลางคืน (22:00 ถึง 07:00) เท่านั้น
ในระบบอัตโนมัติของ Home Assistant ให้ใส่ข้อมูลที่เกี่ยวข้องเข้าไปด้วย:
action:
- service: rest_command.echobell_notify
data:
title: "Temperature Alert"
message: "Freezer temperature: {{ states('sensor.freezer_temp') }}°C"
severity: "{% if states('sensor.freezer_temp') | float > -10 %}critical{% else %}normal{% endif %}"
hour: "{{ now().hour }}"
การส่งข้อมูลจากเซ็นเซอร์
คุณส่งข้อมูลเซ็นเซอร์ใดก็ได้ของ Home Assistant ไปยังการแจ้งเตือน Echobell ของคุณ:
rest_command:
echobell_sensor_update:
url: "https://hook.echobell.one/t/<channel-token>"
method: POST
content_type: "application/json"
payload: >
{
"sensor_name": "{{ sensor_name }}",
"current_value": "{{ current_value }}",
"unit": "{{ unit }}",
"state": "{{ state }}",
"timestamp": "{{ timestamp }}"
}
การจัดการเฮดเดอร์ HTTP
Echobell เข้าถึงเฮดเดอร์ HTTP จากคำขอ webhook ได้ ซึ่งมีประโยชน์สำหรับการยืนยันตัวตนหรือการติดตามที่มา:
rest_command:
echobell_with_headers:
url: "https://hook.echobell.one/t/<channel-token>"
method: POST
headers:
X-Home-Assistant-Instance: "{{ instance_name }}"
X-Automation-ID: "{{ automation_id }}"
content_type: "application/json"
payload: '{"message": "{{ message }}"}'
ในเทมเพลตของ Echobell ให้เข้าถึงเฮดเดอร์ด้วย:
{{header["x-home-assistant-instance"]}}
การดีบักและการทดสอบ
ทดสอบ Webhook ของคุณ
ใช้ Developer Tools → Services ของ Home Assistant เพื่อทดสอบการตั้งค่าของคุณ:
- เลือก
rest_command.echobell_notify - กรอกข้อมูลทดสอบ:
title: "Test Notification"
message: "This is a test from Home Assistant"
- คลิก "Call Service"
- ตรวจดูการแจ้งเตือนในแอป Echobell ของคุณ
ตรวจสอบบันทึกของ Home Assistant
หากการแจ้งเตือนไม่มาถึง ให้ตรวจสอบบันทึกของ Home Assistant:
# In Home Assistant, go to:
Settings → System → Logs
# Or check the log file:
config/home-assistant.log
มองหาข้อผิดพลาดที่เกี่ยวกับ rest_command หรือคำขอ HTTP ไปยัง hook.echobell.one
ตรวจสอบ Webhook URL
ตรวจสอบให้แน่ใจว่า webhook URL ของคุณถูกต้อง:
- ต้องขึ้นต้นด้วย
https://hook.echobell.one/t/ - ต้องมีโทเคนอยู่ด้วย
- ต้องไม่มีช่องว่างหรืออักขระเกินมา
ข้อควรพิจารณาด้านประสิทธิภาพ
การจำกัดอัตราการเรียก
webhook ของ Echobell มีการจำกัดอัตราการเรียกเพื่อป้องกันการใช้งานในทางที่ผิด สำหรับเซ็นเซอร์ที่ส่งข้อมูลถี่:
- หลีกเลี่ยงการทริกเกอร์ทุกครั้งที่เซ็นเซอร์อัปเดต
- ใช้
forในทริกเกอร์เพื่อหน่วงการทำงาน:
trigger:
- platform: numeric_state
entity_id: sensor.temperature
above: 30
for:
minutes: 5 # Only trigger after 5 minutes above threshold
การรวมการอัปเดตเป็นชุด
สำหรับหลายเหตุการณ์ที่เกี่ยวข้องกัน ลองรวมเข้าด้วยกันเป็นการแจ้งเตือนเดียว:
automation:
- alias: "Daily Home Summary"
trigger:
- platform: time
at: "09:00:00"
action:
- service: rest_command.echobell_notify
data:
title: "Morning Home Summary"
message: >
Temperature: {{ states('sensor.temperature') }}°C
Humidity: {{ states('sensor.humidity') }}%
Energy today: {{ states('sensor.daily_energy') }} kWh
Windows open: {{ expand(states.binary_sensor) | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'window') | list | count }}
แนวปฏิบัติที่ดีด้านความปลอดภัย
ปกป้อง Webhook URL ของคุณ
- อย่าคอมมิต webhook URL ลงใน repository สาธารณะ
- ใช้ระบบ secrets ของ Home Assistant สำหรับข้อมูลที่ละเอียดอ่อน
- เปลี่ยนโทเคน webhook เป็นระยะ
การใช้ secrets ใน configuration.yaml:
# secrets.yaml
echobell_webhook: "https://hook.echobell.one/t/<channel-token>"
# configuration.yaml
rest_command:
echobell_notify:
url: !secret echobell_webhook
method: POST
content_type: "application/json"
payload: '{"title": "{{ title }}", "message": "{{ message }}"}'
จำกัดข้อมูลที่ละเอียดอ่อน
ระมัดระวังเรื่องข้อมูลที่คุณใส่ลงในการแจ้งเตือน:
# Good - General information
message: "Motion detected in living room"
# Avoid - Sensitive details
message: "Security code: 1234, camera feed: http://..."
การแก้ปัญหาที่พบบ่อย
การแจ้งเตือนไม่มาถึง
- ตรวจสอบการเชื่อมต่อเครือข่าย: ตรวจสอบว่าอินสแตนซ์ Home Assistant ของคุณเข้าถึงอินเทอร์เน็ตได้
- ตรวจสอบ webhook URL: คัดลอกและวางอย่างระมัดระวัง รวมถึงส่วนโทเคน
- ตรวจสอบแอป Echobell: ตรวจสอบว่าคุณติดตามช่องนั้นอยู่
- ตรวจดูบันทึกของ Home Assistant: มองหาข้อผิดพลาด HTTP หรือการหมดเวลา
ข้อผิดพลาดของเทมเพลต
หากเทมเพลตแสดงผลไม่ถูกต้อง:
- ทดสอบเทมเพลตใน Developer Tools → Template
- ตรวจสอบว่าชื่อตัวแปรตรงกันระหว่าง Home Assistant กับ Echobell
- ตรวจหาการพิมพ์ผิดในไวยากรณ์เทมเพลต: ต้องเป็น
{{variable}}ไม่ใช่{{ variable}}
ระบบอัตโนมัติไม่ทำงาน
- ตรวจสอบเงื่อนไขของทริกเกอร์ใน Home Assistant
- ทดสอบระบบอัตโนมัติด้วยตนเองจาก Developer Tools
- ตรวจสอบว่าสถานะของเอนทิตีตรงกับการตั้งค่าทริกเกอร์ของคุณ
- ตรวจดู trace ของระบบอัตโนมัติเพื่อหาสาเหตุ
ตัวอย่าง: การตั้งค่าระบบรักษาความปลอดภัยแบบครบชุด
นี่คือตัวอย่างครบวงจรที่เชื่อมเซ็นเซอร์ความปลอดภัยหลายตัวเข้าด้วยกัน:
# configuration.yaml
rest_command:
echobell_security:
url: !secret echobell_security_webhook
method: POST
content_type: "application/json"
payload: >
{
"event_type": "{{ event_type }}",
"location": "{{ location }}",
"sensor": "{{ sensor }}",
"timestamp": "{{ timestamp }}",
"alarm_state": "{{ alarm_state }}"
}
# automations.yaml
- alias: "Security Event Handler"
trigger:
- platform: state
entity_id:
- binary_sensor.front_door
- binary_sensor.back_door
- binary_sensor.garage_door
- binary_sensor.motion_hallway
- binary_sensor.motion_living_room
to: "on"
action:
- service: rest_command.echobell_security
data:
event_type: "{{ 'door' if 'door' in trigger.entity_id else 'motion' }}"
location: "{{ trigger.to_state.attributes.friendly_name }}"
sensor: "{{ trigger.entity_id }}"
timestamp: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
alarm_state: "{{ states('alarm_control_panel.home_alarm') }}"
ใน Echobell ให้ตั้งเทมเพลตของคุณเป็น:
- หัวข้อ:
Security Alert: {{event_type | upper}} - เนื้อหา:
{{location}} activated at {{timestamp}} (Alarm: {{alarm_state}})
ขั้นตอนถัดไป
เมื่อคุณเชื่อม Echobell เข้ากับ Home Assistant เรียบร้อยแล้ว:
- สำรวจ ประเภทการแจ้งเตือน สำหรับระดับความสำคัญที่ต่างกัน
- เรียนรู้เรื่อง เงื่อนไข เพื่อกรองการแจ้งเตือนอย่างชาญฉลาด
- ดู ตัวแปรในเทมเพลต สำหรับการจัดรูปแบบข้อความขั้นสูง
- อ่าน แนวปฏิบัติที่ดีของ webhook เพื่อการเชื่อมต่อที่ดีที่สุด