Journal

Adaptive Automation Engine: Node-RED Conflict Resolution และ Occupancy Learning สำหรับ Smart Home

Adaptive Automation Engine: Node-RED Conflict Resolution and Occupancy Learning for Smart Home

12 พฤษภาคม 2569 · 1 นาที

Adaptive Automation Engine: บ้านที่เรียนรู้ตัวเอง

การตั้ง automation scene แบบ manual มักนำไปสู่ conflict เมื่อ scene หลายตัว activate พร้อมกัน เช่น "Movie Mode" ปิดไฟ ขณะที่ "Reading Mode" เปิดไฟ Adaptive Automation Engine แก้ปัญหานี้ด้วยการเรียนรู้ pattern และ resolve conflict อัตโนมัติ

สถาปัตยกรรม 3 ชั้น

ชั้นที่ 1: Occupancy Learning Node-RED เก็บ event log ทุก state change (motion, light, climate) ใน InfluxDB แล้ว query หา occupancy pattern ด้วย Flux:

from(bucket: "smarthome")   |> range(start: -30d)   |> filter(fn: (r) => r._measurement == "motion_sensor")   |> aggregateWindow(every: 30m, fn: mean)   |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

สร้าง Typical Day Profile แยกตาม weekday/weekend: - 05:30–07:00: morning routine probability 0.85 - 07:00–09:00: departure probability 0.70 - 17:00–19:00: return probability 0.78 - 22:00–23:30: bedtime probability 0.90

ชั้นที่ 2: Scene Priority Matrix กำหนด priority ให้แต่ละ scene เพื่อ resolve conflict:

json {   "scenes": [     {"name": "Security Alert", "priority": 100, "override_all": true},     {"name": "Sleep Mode", "priority": 80, "domains": ["light", "climate"]},     {"name": "Movie Mode", "priority": 60, "domains": ["light", "media"]},     {"name": "Reading Mode", "priority": 50, "domains": ["light"]},     {"name": "Eco Mode", "priority": 30, "domains": ["climate", "switch"]}   ] }

เมื่อ conflict เกิดขึ้น scene ที่มี priority สูงกว่า win แต่ถ้า domains ไม่ overlap ทั้งสอง scene สามารถ coexist ได้

ชั้นที่ 3: Conflict Detection Node (Node-RED)

Custom function node ใน Node-RED:

javascript // Active scenes registry const activeScenes = flow.get('active_scenes') || {}; const newScene = msg.payload.scene; const newPriority = scenes[newScene].priority;  // Check conflicts for (const [name, data] of Object.entries(activeScenes)) {     const overlap = data.domains.some(d => scenes[newScene].domains.includes(d));     if (overlap && data.priority < newPriority) {         // Deactivate lower-priority conflicting scene         node.send([{payload: {action: 'deactivate', scene: name}}, null]);         delete activeScenes[name];     } else if (overlap && data.priority >= newPriority) {         // Reject new scene - current has priority         node.send([null, {payload: {rejected: newScene, reason: 'priority_conflict'}}]);         return;     } } activeScenes[newScene] = {priority: newPriority, domains: scenes[newScene].domains}; flow.set('active_scenes', activeScenes);

Audit Log

ทุก scene activation/deactivation บันทึก: - timestamp, scene name, trigger source, conflict_resolved (boolean) - บันทึกใน InfluxDB tag: - Dashboard แสดง conflict frequency chart รายสัปดาห์

Learning และ Auto-Suggestion

หลัง 30 วัน engine วิเคราะห์ pattern: - ถ้า user มักปิด eco_mode ภายใน 10 นาทีหลัง activate → suggest raising priority - ถ้า movie_mode มักถูก interrupt โดย sleep_mode → suggest time-based activation rules - ส่ง weekly report ผ่าน LINE: "Movie Mode ถูก interrupt 5 ครั้งสัปดาห์นี้ ต้องการปรับ schedule ไหม?"

ผลลัพธ์ที่วัดได้

บ้าน pilot ที่ใช้ระบบนี้ 60 วันพบ: - Automation conflict ลดลง 78% - Manual override ลดลง 45% (ระบบตอบสนองตรงใจมากขึ้น) - Comfort score เพิ่มขึ้นจาก self-report: 7.2/10 → 8.8/10

คำถามที่พบบ่อย

ต้องมีพื้นฐาน programming ไหมสำหรับระบบนี้?
ต้องการความรู้ JavaScript พื้นฐานสำหรับ function node ใน Node-RED และ Flux query สำหรับ InfluxDB ถ้าไม่มีประสบการณ์ สามารถเริ่มจาก Node-RED visual programming ก่อน แล้วค่อยเพิ่ม custom function
สามารถใช้ Home Assistant automation แทน Node-RED ได้ไหม?
ได้บางส่วน Home Assistant automation รองรับ condition และ priority แต่ไม่มี conflict detection ที่ sophisticated เท่า Node-RED function node สำหรับ simple home HA อาจเพียงพอ แต่ multi-scene complex home แนะนำ Node-RED
ข้อมูล occupancy learning เก็บไว้ที่ไหน?
ใน InfluxDB บน local server (Raspberry Pi 5 หรือ Intel NUC) ไม่มีข้อมูลออกนอกบ้าน Retention policy ตั้งให้เก็บ raw data 90 วัน และ aggregate data 2 ปี