DEV Community

Cover image for ลืมปิดแอร์ไม่เป็นไร เดี๋ยว Node-RED ปิดแอร์ให้ ใน Home Assistant
Pattanapong Cherthong
Pattanapong Cherthong

Posted on • Edited on

3

ลืมปิดแอร์ไม่เป็นไร เดี๋ยว Node-RED ปิดแอร์ให้ ใน Home Assistant

Full Node-RED Workflow

ก่อนที่จะสร้าง Node-RED Workflow มาทำความเข้าใจในส่วนของการ tracking กันก่อนครับ ใน Home Assistant ถ้าต้องการจะ tracking ว่าสมาชิกในบ้านคนนี้ อยู่บ้านหรือไม่ สามารถ tracking ได้หลายๆทางเพื่อความแม่นยำ โดยสามารถเข้าไปเซ็ตได้ใน Configuration > People จะมีให้ใส่ device สำหรับการ tracking

Screen Shot 2564-06-20 at 01.49.31

Screen Shot 2564-06-20 at 01.52.34
ซึ่งผมได้ tracking จาก 2 ทางคือ

  • Entity จากแอพ Home Assistant ในมือถือ โดยจะเช็คจาก Location ล่าสุด ว่าอยู่ในบริเวณบ้าน หรือ ออกนอกบริเวณบ้านไปแล้ว
  • และเนื่องจาก MikroTik integration สามารถดึง Entity ของอุปกรณ์มือถือที่เชื่อมต่อใน Network มาได้ จะทำให้เราเช็คได้ว่า ถ้ามือถือเครื่องนี้ต่อ Wifi อยู่แสดงว่าอยู่บ้าน

(จริงๆแค่ track จาก Location ใน App มือถืออันเดียวก็เพียงพอแล้ว แค่ผมเอา entity จาก MikroTik มา track เพิ่มเพื่อความแม่นยำ)

มาสร้าง Node-RED Workflow กันเลยดีกว่าครับ
Node-RED 1

1.เริ่มจากสร้าง events: state มาเช็ค state ของ person ที่ต้องการติดตาม ดูว่าถ้าไม่อยู่บ้าน (not_home) ก็ให้ทำ Node ถัดไป
Node 1

2.เพิ่ม delay หน่วงเวลาไป 1 ชั่วโมง
Node 2
จะตั้งเวลาเท่าไรนั้น ขึ้นอยู่กับความเหมาะสมครับ เช่นอย่างของผมตั้งเวลาไว้เผื่อเดินไปเซเว่นใกล้บ้านแล้วกลับมาไม่เกิน 1 ชั่วโมง

3.หลังจาก ครบ 1 ชั่วโมงแล้ว เช็ค current state ดูว่าถ้าหาก state ยังขึ้นเป็น not_home ก็ให้ทำ Node ถัดไป
Node 3

Node-RED 2

4.หลังจากนั้นเช็ค current state ของแอร์ว่ายังเปิดอยู่ไหม ถ้าแอร์ยังเปิดอยู่ก็ให้ทำ Node ถัดไป
Screen Shot 2564-06-20 at 03.13.39

5.หลังจากเช็คว่าถ้าแอร์ยังเปิดอยู่ ก็ทำการ call service สั่งให้ปิดแอร์
Screen Shot 2564-06-20 at 03.16.17

6.ทำการ call service notify เพื่อทำการแจ้งเตือนในแอพ Home Assistant ว่า "ดำเนินการปิดแอร์ให้แล้วเนื่องจากคุณลืมปิดแอร์"
(ขั้นตอนนี้อาจจะปรับเปลี่ยนไปใช้เป็น LINE Notify หรือ Notify อื่นๆก็ได้นะครับ)
call service notify

ตัวอย่าง Data สำหรับ notify
JSON Data

ดูข้อมูลเพิ่มเติมสำหรับการส่ง notify ได้ที่
https://www.home-assistant.io/integrations/notify/

หลังจากนี้เมื่อออกจากบ้านและลืมปิดแอร์ภายใน 1 ชั่วโมงก็จะมี Notification แจ้งเตือนในมือถือและ Node-RED ก็จะสั่งปิดแอร์ให้เราอัตโนมัติ

หน้าตา Notification จากแอพ Home Assistant ในมือถือ
Notification

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay