---
title: "การเชื่อมต่อกับ Home Assistant - แจ้งเตือนบ้านอัจฉริยะ"
sidebarTitle: Home Assistant
description: "เชื่อม Home Assistant เข้ากับ Echobell เพื่อรับการแจ้งเตือนบ้านอัจฉริยะทันที: ความปลอดภัย การเคลื่อนไหว อุณหภูมิ และอีเวนต์ระบบอัตโนมัติ เป็นพุชหรือสายโทรเข้า"
---

import { Step, Steps } from "fumadocs-ui/components/steps";

# การเชื่อมต่อกับ Home Assistant

[Home Assistant](https://www.home-assistant.io/) เป็นแพลตฟอร์มระบบอัตโนมัติในบ้านแบบโอเพนซอร์สที่ทรงพลัง โดยให้ความสำคัญกับการควบคุมภายในเครือข่ายและความเป็นส่วนตัวเป็นอันดับแรก เมื่อเชื่อม Echobell เข้ากับ Home Assistant คุณจะได้รับการแจ้งเตือนทันทีหรือแม้แต่สายโทรเข้าเมื่อเกิดเหตุการณ์สำคัญในบ้านอัจฉริยะของคุณ ไม่ว่าจะเป็นการแจ้งเตือนด้านความปลอดภัย อุณหภูมิเกินเกณฑ์ หรือทริกเกอร์จากระบบอัตโนมัติ

คู่มือนี้จะพาคุณตั้งค่า Echobell ให้รับการแจ้งเตือนจากระบบอัตโนมัติของ Home Assistant ด้วย webhook หรือบริการ RESTful Notify

## สิ่งที่ต้องเตรียม

ก่อนเริ่ม ตรวจสอบให้แน่ใจว่าคุณมี:

- ติดตั้ง [แอป Echobell](https://apps.apple.com/app/apple-store/id6743597198?pt=128151925&ct=docs-developer-home-assistant-th&mt=8) / [Google Play](https://play.google.com/store/apps/details?id=one.echobell.echobellandroid) บนอุปกรณ์มือถือของคุณแล้ว
- ช่อง Echobell ที่ใช้งานอยู่ (สร้างในแอปได้หากยังไม่มี)
- อินสแตนซ์ Home Assistant ที่ทำงานอยู่ (แนะนำเวอร์ชัน 2023.1 ขึ้นไป)
- สิทธิ์เข้าถึงไฟล์ตั้งค่าของ Home Assistant

## วิธีที่ 1: ใช้ทริกเกอร์แบบ Webhook (แนะนำ)

วิธี webhook ยืดหยุ่นที่สุด เพราะช่วยให้คุณส่งข้อมูลแบบกำหนดเองและใช้ [ตัวแปรในเทมเพลต](/docs/template) ในการแจ้งเตือนได้

<Steps>
<Step>
### รับ Webhook URL ของ Echobell

1. เปิดแอป Echobell แล้วไปที่ช่องของคุณ
2. แตะที่ช่องนั้นเพื่อดูรายละเอียด
3. หา **Webhook URL** ในส่วน Triggers
4. คัดลอก URL ซึ่งจะมีหน้าตาแบบนี้: `https://hook.echobell.one/t/<channel-token>`
</Step>
<Step>

### สร้างระบบอัตโนมัติใน Home Assistant

ใน Home Assistant คุณทริกเกอร์การแจ้งเตือนของ Echobell ได้ด้วยบริการ `rest_command` ก่อนอื่นให้เพิ่มคำสั่ง REST ลงใน `configuration.yaml` ของคุณ:

```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 เพื่อโหลดการตั้งค่าใหม่

</Step>
<Step>
### ใช้งานในระบบอัตโนมัติ

ตอนนี้คุณเรียกใช้บริการนี้จากระบบอัตโนมัติใดก็ได้ นี่คือตัวอย่างการแจ้งเตือนเมื่อประตูถูกเปิด:

```yaml
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') }}"
```

</Step>
<Step>
### ตั้งค่าเทมเพลตการแจ้งเตือน

ในหน้าตั้งค่าช่อง Echobell ของคุณ ให้ตั้งเทมเพลตการแจ้งเตือนเพื่อแสดงข้อมูลจาก webhook:

- **เทมเพลตหัวข้อ**: `{{title}}`
- **เทมเพลตเนื้อหา**: `{{message}}`

ตอนนี้เมื่อระบบอัตโนมัติทำงาน คุณจะได้รับการแจ้งเตือนพร้อมหัวข้อและข้อความที่กำหนดเอง

</Step>
</Steps>

## วิธีที่ 2: ใช้แพลตฟอร์ม RESTful Notify

หากต้องการวิธีที่กลมกลืนกับระบบมากกว่า คุณตั้งค่า Echobell เป็นบริการ notify ใน Home Assistant ได้

### การตั้งค่า

เพิ่มข้อความต่อไปนี้ลงใน `configuration.yaml` ของคุณ:

```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 อื่น ๆ:

```yaml
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') }}%"
```

## กรณีใช้งานขั้นสูง

### มอนิเตอร์อุณหภูมิพร้อมการแจ้งเตือนขั้นวิกฤต

ใช้ [ประเภทการแจ้งเตือน](/docs/notification) เพื่อส่งการแจ้งเตือนอุณหภูมิขั้นวิกฤตเป็นสายโทรเข้า:

```yaml
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 ของคุณ ให้ตั้ง [ประเภทการแจ้งเตือน](/docs/notification) เป็น "โทรเข้า" แล้วใช้เทมเพลตนี้:

- **หัวข้อ**: `Critical Temperature Alert`
- **เนื้อหา**: `{{location}} temperature is {{temperature}}°C - Immediate attention required!`

### การเชื่อมต่อกับระบบรักษาความปลอดภัย

ตั้งค่าการมอนิเตอร์ความปลอดภัยแบบครบวงจร:

```yaml
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() }}"
```

### การแจ้งเตือนจากเครื่องใช้ไฟฟ้าอัจฉริยะ

ติดตามรอบการทำงานของเครื่องใช้ไฟฟ้าและรับแจ้งเตือนเมื่อทำงานเสร็จ:

```yaml
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"
```

### การมอนิเตอร์สภาพแวดล้อม

ติดตามคุณภาพอากาศ ความชื้น หรือปัจจัยแวดล้อมอื่น ๆ:

```yaml
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](/docs/conditions) เพื่อกรองการแจ้งเตือนตามเกณฑ์ที่กำหนดได้ ตัวอย่างเช่น แจ้งเตือนเฉพาะบางช่วงเวลา หรือเฉพาะเมื่อค่าเกินเกณฑ์:

ตั้งเงื่อนไขในช่อง Echobell ของคุณ:

```
severity == "critical" || (hour >= 22 || hour <= 7)
```

เงื่อนไขนี้จะส่งการแจ้งเตือนก็ต่อเมื่อระดับความรุนแรงเป็น critical หรืออยู่ในช่วงกลางคืน (22:00 ถึง 07:00) เท่านั้น

ในระบบอัตโนมัติของ Home Assistant ให้ใส่ข้อมูลที่เกี่ยวข้องเข้าไปด้วย:

```yaml
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 ของคุณ:

```yaml
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 ได้ ซึ่งมีประโยชน์สำหรับการยืนยันตัวตนหรือการติดตามที่มา:

```yaml
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 เพื่อทดสอบการตั้งค่าของคุณ:

1. เลือก `rest_command.echobell_notify`
2. กรอกข้อมูลทดสอบ:

```yaml
title: "Test Notification"
message: "This is a test from Home Assistant"
```

3. คลิก "Call Service"
4. ตรวจดูการแจ้งเตือนในแอป Echobell ของคุณ

### ตรวจสอบบันทึกของ Home Assistant

หากการแจ้งเตือนไม่มาถึง ให้ตรวจสอบบันทึกของ Home Assistant:

```bash
# 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` ในทริกเกอร์เพื่อหน่วงการทำงาน:

```yaml
trigger:
  - platform: numeric_state
    entity_id: sensor.temperature
    above: 30
    for:
      minutes: 5 # Only trigger after 5 minutes above threshold
```

### การรวมการอัปเดตเป็นชุด

สำหรับหลายเหตุการณ์ที่เกี่ยวข้องกัน ลองรวมเข้าด้วยกันเป็นการแจ้งเตือนเดียว:

```yaml
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`:

```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 }}"}'
```

### จำกัดข้อมูลที่ละเอียดอ่อน

ระมัดระวังเรื่องข้อมูลที่คุณใส่ลงในการแจ้งเตือน:

```yaml
# Good - General information
message: "Motion detected in living room"

# Avoid - Sensitive details
message: "Security code: 1234, camera feed: http://..."
```

## การแก้ปัญหาที่พบบ่อย

### การแจ้งเตือนไม่มาถึง

1. **ตรวจสอบการเชื่อมต่อเครือข่าย**: ตรวจสอบว่าอินสแตนซ์ Home Assistant ของคุณเข้าถึงอินเทอร์เน็ตได้
2. **ตรวจสอบ webhook URL**: คัดลอกและวางอย่างระมัดระวัง รวมถึงส่วนโทเคน
3. **ตรวจสอบแอป Echobell**: ตรวจสอบว่าคุณติดตามช่องนั้นอยู่
4. **ตรวจดูบันทึกของ Home Assistant**: มองหาข้อผิดพลาด HTTP หรือการหมดเวลา

### ข้อผิดพลาดของเทมเพลต

หากเทมเพลตแสดงผลไม่ถูกต้อง:

1. ทดสอบเทมเพลตใน Developer Tools → Template
2. ตรวจสอบว่าชื่อตัวแปรตรงกันระหว่าง Home Assistant กับ Echobell
3. ตรวจหาการพิมพ์ผิดในไวยากรณ์เทมเพลต: ต้องเป็น `{{variable}}` ไม่ใช่ `{{ variable}}`

### ระบบอัตโนมัติไม่ทำงาน

1. ตรวจสอบเงื่อนไขของทริกเกอร์ใน Home Assistant
2. ทดสอบระบบอัตโนมัติด้วยตนเองจาก Developer Tools
3. ตรวจสอบว่าสถานะของเอนทิตีตรงกับการตั้งค่าทริกเกอร์ของคุณ
4. ตรวจดู trace ของระบบอัตโนมัติเพื่อหาสาเหตุ

## ตัวอย่าง: การตั้งค่าระบบรักษาความปลอดภัยแบบครบชุด

นี่คือตัวอย่างครบวงจรที่เชื่อมเซ็นเซอร์ความปลอดภัยหลายตัวเข้าด้วยกัน:

```yaml
# 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 เรียบร้อยแล้ว:

- สำรวจ [ประเภทการแจ้งเตือน](/docs/notification) สำหรับระดับความสำคัญที่ต่างกัน
- เรียนรู้เรื่อง [เงื่อนไข](/docs/conditions) เพื่อกรองการแจ้งเตือนอย่างชาญฉลาด
- ดู [ตัวแปรในเทมเพลต](/docs/template) สำหรับการจัดรูปแบบข้อความขั้นสูง
- อ่าน [แนวปฏิบัติที่ดีของ webhook](/docs/webhook) เพื่อการเชื่อมต่อที่ดีที่สุด

## แหล่งข้อมูลที่เกี่ยวข้อง

- [เอกสาร REST Command ของ Home Assistant](https://www.home-assistant.io/integrations/rest_command/)
- [แพลตฟอร์ม RESTful Notify ของ Home Assistant](https://www.home-assistant.io/integrations/notify.rest/)
- [เอกสาร Webhook ของ Echobell](/docs/webhook)
- [ระบบเทมเพลตของ Echobell](/docs/template)
