Showing posts with label Home Assistant. Show all posts
Showing posts with label Home Assistant. Show all posts

9/23/2025

Home Assistant with Docker กล้อง ONVIF แจ้งเตือนเข้า Rocketchat

 *** หาต้องการให้แสดงรูป ต้องใช้ url
https://rocket.scivalve.com
- หากใช้ IP จะเห็น รูปได้เครื่องอยู่ในวง network เดียวกัน
- ใช้ chat.scivalve.com ใน app จะไม่เห็น เพราะ app ชี้ url ไปที่ rocket.scivalve.com
ให้ใช้ rocket.scivalve.com จะเห็นทั้งในหมด
ขึ้นตอน
1. Add integration ONVIF ถ้ากล้องอยู่วงเดียวกันกับ HA จะเจอเลย ถ้าไม่เจอ Add Manual Port 10080
2. Add กล้องสำหรับ view เข้า Dashboard ไว้ดู By entity แล้วเลือก Profile กล้องที่ต้องการ
*** หาวิธีส่งรูปตรง ๆ จาก HA แล้วไม่ได้ ติดปัญหา กล้องไม่มี function ที่ snap เป็น jpg จึงทำไม่ได้ ****
แก้ปัญหาโดย เขียน code python save ลง docker แล้วให้ HA เรียกใช้ python ส่งแจ้งเตือนเข้า Rocketchat
3. สร้างไฟล์ python ที่ Home Assistant
ไว้ที่ /HA/snapshot.py
Code

  1. #!/usr/bin/env python3
  2. import requests
  3.  
  4. url = "http://xxx.xx.xx.xx:8080/snapshot.cgi?user=xxx&pwd=xxx"
  5. res = requests.get(url, stream=True)
  6.  
  7. if res.status_code == 200:
  8.     #with open("/HA/www/snapshot.jpg", "wb") as f:
  9.     with open("/config/www/snapshot.jpg", "wb") as f:
  10.        for chunk in res.iter_content(1024):
  11.             f.write(chunk)
  12.  
  13.     url = "http://xxx.xx.xx.xx:3000/api/v1/rooms.upload/RoomId"
  14.     #url = "https://chat.scivalve.com/api/v1/rooms.upload/RoomId"
  15.     headers = {
  16.         "X-Auth-Token": "xxx",
  17.         "X-User-Id": "xxx"
  18.     }
  19.    
  20.     files = {
  21.         #"file": ("snapshot.jpg", open("/HA/www/snapshot.jpg", "rb"), "image/jpeg")
  22.         "file": ("snapshot.jpg", open("/config/www/snapshot.jpg", "rb"), "image/jpeg")
  23.     }
  24.  
  25.     data = {
  26.         "msg": "พบการเคลื่อนไหวจากกล้อง SERVER A"
  27.     }
  28.  
  29.     response = requests.post(url, headers=headers, files=files, data=data)
  30.     #print(response.text)
  31.     #print("Snapshot saved")
  32. else:
  33.     print("Error:", res.status_code)


***สำคัญไฟล์ ที่ save และส่ง ต้องใช้ใน /config/ เพื่อให้รูปได้จริง หากไว้ใน /HA/ จะ run คำสั่ง python ตรง ๆ ได้ แต่เมื่อ Home Assistant เรียกใช้จะส่งรูปไม่ได้ ***
RoomId หาได้จาก คำสั่ง
  1. curl -H "X-Auth-Token: xxx" \
  2.      -H "X-User-Id: xxx" \
  3.      http://xxx.xxx.xx.xx:3000/api/v1/rooms.info?roomName=ชื่อห้อง

เช่น
  1. curl -H "X-Auth-Token: xxx" \
  2.      -H "X-User-Id: xxx" \
  3.      http://xxx.xxx.xx.xx:3000/api/v1/rooms.info?roomName=Test

จะได้ _id คือ room id
{"room":{"_id":"xxx","fname":"Test"
{"room":{"_id":"xxx","fname":"IT_Notification",

4. configuration.yaml สำหรับเรียกใช้ python เพิ่ม
  1. shell_command:
  2.    send_snapshot: "python3 /config/snapshot.py"
  3.  


5. automation.yaml เพิ่ม
  1. - id: motion_alert_server_a
  2.   alias: "แจ้งเตือน SERVER A พร้อมส่งรูป"
  3.   trigger:
  4.     - platform: state
  5.       entity_id: binary_sensor.ip_camera_cell_motion_detection
  6.       to: "on"
  7.   action:
  8.     - service: shell_command.send_snapshot

9/18/2025

ส่งค่าอุณหภูมิจาก Pi ไป Home Assistant แบบ Docker

 ส่งค่าอุณหภูมิจาก Pi ไป Home Assistant แบบ Docker

ต่อจาก
https://intranet.scivalve.com/blog.php?u=281&b=2051

1. ตัว HA ลงแบบ Docker ก็ต้อง Mosquitto ด้วย Docker container
1.1. สร้างโฟลเดอร์เก็บ config/data/log ของ Mosquitto:

  1. mkdir -p /HA/mosquitto/config /HA/mosquitto/data /HA/mosquitto/log

1.2. สร้างไฟล์ config
  1. nano /HA/mosquitto/config/mosquitto.conf

1.3. ใส่ค่า
แบบใช้ได้หมด ไม่ต้องใส่ use password ไม่ปลอดภัย
  1. persistence true
  2. persistence_location /mosquitto/data/
  3. log_dest file /mosquitto/log/mosquitto.log
  4.  
  5. allow_anonymous true
  6. listener 1883

แบบตั้ง user password
  1. persistence true
  2. persistence_location /mosquitto/data/
  3. log_dest file /mosquitto/log/mosquitto.log
  4.  
  5. allow_anonymous false
  6. password_file /mosquitto/config/password.txt
  7. listener 1883


1.4. สร้าง user ด้วยคำสั่ง
docker run --rm -it \
  1.  -v /HA/mosquitto/config:/mosquitto/config \
  2.   eclipse-mosquitto mosquitto_passwd -c /mosquitto/config/password.txt hauserXXXXX

มันจะถาม password ให้คุณใส่ → เก็บไว้ใน /HA/mosquitto/config/password.txt

1.5. รัน container Mosquitto:
  1. docker run -d \
  2.   --name mosquitto \
  3.   -p 1883:1883 \
  4.   -p 9001:9001 \
  5.   -v /HA/mosquitto/config:/mosquitto/config \
  6.   -v /HA/mosquitto/data:/mosquitto/data \
  7.   -v /HA/mosquitto/log:/mosquitto/log \
  8.   eclipse-mosquitto


1.6. Restart Mosquitto
  1. docker restart mosquitto


1.7. ทดสอบ ที่เครื่อง HA Run
  1. mosquitto_sub -h localhost -p 1883 -u "hauser" -P "รหัสผ่าน" -t "#"


1.8. ที่เครื่องเรา ติดตั้ง
  1. apt install mosquitto-clients -y

แล้วลองส่งค่าไป
  1. mosquitto_pub -h 192.168.2.5 -p 1883 -u "xxx" -P "xxxx" -t "test/topic" -m "Hello MQTT"
  1. mosquitto_pub -h 192.168.2.5 -p 1883 -u "xxx" -P "xxxx" -t "hass/sensor/temperature" -m "27"

ที่ Terminal ของ HA จะต้องเห็นข้อความ Hello MQTT

1.9. ที่ เครื่อง HA เพิ่ม ที่ไฟล์ configuration.yaml เป็น Sensor และรอรับค่า MQTT
  1. mqtt:
  2.   sensor:
  3.     - name: "Server Temp."
  4.       state_topic: "hass/sensor/temperature"
  5.       value_template: "{{ value_json.temperature }}"
  6.       unit_of_measurement: "°C"
  7.  
  8.     - name: "Server Hum."
  9.       state_topic: "hass/sensor/temperature"
  10.       value_template: "{{ value_json.humidity }}"
  11.       unit_of_measurement: "%"


1.10. เข้า Setting --> Devices & Services Add integration MQTT เข้าไปด้วย
ใส่ IP User Password

1.11. ที่เครื่อง pi ทดลอง run python Code temp2mqtt.2.5.py
  1. #!/usr/bin/python3
  2. import sys
  3. import Adafruit_DHT
  4.  
  5. import time, json
  6. import RPi.GPIO as GPIO
  7. import paho.mqtt.client as mqtt
  8.  
  9. import requests
  10.  
  11. # Config
  12. MQTT_BROKER = "192.168.2.5"   # IP ของ Home Assistant
  13. MQTT_PORT = 1883
  14. MQTT_USER = "xx"
  15. MQTT_PASSWORD = "xx"
  16. MQTT_TOPIC = "hass/sensor/temperature"
  17.  
  18. SENSOR = Adafruit_DHT.AM2302  # หรือ DHT11 แล้วแต่ที่ใช้
  19. PIN = 4  # GPIO ที่ต่อ sensor
  20.  
  21. # Connect MQTT
  22. client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
  23. client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
  24. client.connect(MQTT_BROKER, MQTT_PORT, 60)
  25.  
  26. # Try to grab a sensor reading.  Use the read_retry method which will retry up
  27. # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
  28. humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)
  29.  
  30. # Un-comment the line below to convert the temperature to Fahrenheit.
  31. # temperature = temperature * 9/5.0 + 32
  32.  
  33. from datetime import datetime
  34. if humidity is not None and temperature is not None:
  35.    print('Temp={0:0.1f}*  Humidity={1:0.1f}%  Date={2}'.format(temperature, humidity, datetime.today().strftime('%Y-%m-%d %H:%M:%S')))
  36.    payload = {
  37.         "temperature": round(temperature, 2),
  38.         "humidity": round(humidity, 2)
  39.    }
  40.    #print('{0}'.format(json.dumps(payload)))
  41.    client.publish(MQTT_TOPIC, json.dumps(payload))
  42.  


แก้ Code ให้ส่ง MQTT ไป 2 เครื่อง
  1. #!/usr/bin/python3
  2. import Adafruit_DHT
  3. import time, json
  4. from datetime import datetime
  5. import paho.mqtt.client as mqtt
  6.  
  7. # Config broker 1
  8. MQTT_BROKER1   = "192.168.0.187"
  9. MQTT_PORT1     = 1883
  10. MQTT_USER1     = "xxx"
  11. MQTT_PASSWORD1 = "xxx"
  12. BROKER1_TOPIC  = "hass/sensor/temperature"
  13.  
  14. # Config broker 2
  15. MQTT_BROKER2   = "192.168.2.5"
  16. MQTT_PORT2     = 1883
  17. MQTT_USER2     = "xxx"
  18. MQTT_PASSWORD2 = "xxx"
  19. BROKER2_TOPIC  = "hass/sensor/temperature"
  20.  
  21. SENSOR = Adafruit_DHT.AM2302
  22. PIN = 4
  23.  
  24. # อ่านค่าจาก DHT
  25. humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)
  26.  
  27. if humidity is not None and temperature is not None:
  28.     payload = {
  29.         "temperature": round(temperature, 2),
  30.         "humidity": round(humidity, 2)
  31.     }
  32.  
  33.     print(f"Publish: {payload} at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  34.  
  35.     # ส่งไป broker 1
  36.     client1 = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
  37.     client1.username_pw_set(MQTT_USER1, MQTT_PASSWORD1)
  38.     client1.connect(MQTT_BROKER1, MQTT_PORT1, 60)
  39.     client1.publish(BROKER1_TOPIC, json.dumps(payload))
  40.     client1.disconnect()
  41.  
  42.     # ส่งไป broker 2
  43.     client2 = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
  44.     client2.username_pw_set(MQTT_USER2, MQTT_PASSWORD2)
  45.     client2.connect(MQTT_BROKER2, MQTT_PORT2, 60)
  46.     client2.publish(BROKER2_TOPIC, json.dumps(payload))
  47.     client2.disconnect()

 

HA + Pi สั่งปิดเปิด Air Auto แทนเครื่อง MN-SERVER

 HA + Pi สั่งปิดเปิด Air Auto แทนเครื่อง MN-SERVER

คำสั่ง Restart service HA docker

  1. docker restart Docker-ID

ตั้งค่า Timezone ให้ Debian เพื่อให้เวลาตรง
  1. timedatectl set-timezone Asia/Bangkok
  2. timedatectl
  3. date


1. ติดตั้ง Home Assistant with Docker
https://intranet.scivalve.com/blog.php?u=281

2. HA และ PI สร้าง Private Key เพื่อให้ HA สามารถ SSH เข้าเครื่อง PI โดยไม่ต้องใช้รหัส
จะทำให้ HA สามารถเรียกคำสั่งโปรแกรมที่เครื่อง PI ได้
2.1. ใช้ Terminal ล็อคอินเข้าไปที่ เครื่อง Server ที่รัน Docker และ Home Assistant ของคุณ
2.2. รันคำสั่งนี้เพื่อสร้าง Key คู่ใหม่:
  1. ssh-keygen -t rsa -b 4096

2.3. ระบบจะถามคำถาม 2-3 ข้อ ให้ กด Enter ผ่านไปทั้งหมด เพื่อยอมรับค่าเริ่มต้น และ ไม่ต้องใส่รหัสผ่าน (passphrase) นะครับ
2.4. คำสั่งนี้จะสร้างไฟล์ id_rsa (กุญแจลับ) และ id_rsa.pub (กุญแจสาธารณะ) ขึ้นมาในโฟลเดอร์ .ssh ของ user ที่คุณกำลังล็อคอินอยู่บน Server (เช่น /root/.ssh/ หากคุณล็อคอินเป็น root)

[บนเครื่อง HA Server] ขั้นตอนที่ 2: คัดลอกเนื้อหา Public Key

2.5. รันคำสั่งนี้เพื่อแสดงเนื้อหาของ Public Key:
  1. cat ~/.ssh/id_rsa.pub

2.6. คัดลอก (Copy) ข้อความทั้งหมด ที่แสดงขึ้นมา มันจะขึ้นต้นด้วย ssh-rsa และลงท้ายด้วยชื่อ user@hostname ของคุณ

[บนเครื่อง Raspberry Pi] ขั้นตอนที่ 3: ติดตั้ง Public Key

2.7. ตอนนี้ ให้ใช้ Terminal ล็อคอินเข้าไปที่ เครื่อง Raspberry Pi ของคุณ
2.8. รันคำสั่งนี้เพื่อสร้างโฟลเดอร์และไฟล์ที่จำเป็น (หากยังไม่มี) และตั้งค่า permission ให้ถูกต้อง:
  1. mkdir -p ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys

2.9. เปิดไฟล์ authorized_keys ด้วยโปรแกรม nano:
  1. nano ~/.ssh/authorized_keys

2.10. วาง (Paste) Public Key ที่คุณคัดลอกมาจากขั้นตอนที่ 2 ลงในไฟล์นี้ จากนั้นกด Ctrl+X > กด Y > กด Enter เพื่อบันทึกและออกจากโปรแกรม

[บนเครื่อง HA Server] ขั้นตอนที่ 4: คัดลอก Private Key ให้ Docker
2.11. กลับมาที่ Terminal ของ เครื่อง HA Server
2.12. ตอนนี้ให้ทำขั้นตอนที่เราคุยกันล่าสุด คือคัดลอก Private Key (id_rsa) ที่เพิ่งสร้างในขั้นตอนที่ 1 ไปยังโฟลเดอร์คอนฟิกของ HA ที่แชร์กับ Docker

# แก้ /root/.ssh/id_rsa หาก Key ของคุณอยู่ที่อื่น
# แก้ /path/to/your/ha/config/ ให้เป็น Path จริง
เครื่องจริงอยู่ที่ /config/id_rsa
  1. cp /root/.ssh/id_rsa /path/to/your/ha/config/

2.13. ตั้งค่า permission ให้ไฟล์ที่คัดลอกไป:
  1. chmod 600 /path/to/your/ha/config/id_rsa

2.14. ทดสอบ SSH จากเครื่อง HA ไปเครื่อง PI จะต้องไม่ขึ้นถามรหัส Login

3. แก้ไข ไฟล์ configuration.yaml เพื่อให้มี Switch ปิดเปิด Air manual เพิ่ม
  1. ### Air ####
  2. shell_command:
  3.   # !!! สำคัญ: แก้ไข <IP_ของ_RPi> และ path ไปยังไฟล์ .py ของคุณให้ถูกต้อง !!!
  4.   #### TEST #####
  5.   #air1_on:  "sshpass -p 'XXX' ssh pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  6.   #air1_off: "sshpass -p 'XXX' ssh pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  7.   #air1_on:  "ssh -i /HA/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  8.   #air1_off: "ssh -i /HA/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  9.   #air1_on:  "ssh pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  10.   #air1_off: "ssh pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  11.   ##### Test Rocketchat #######
  12.   #air1_on:  "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  13.   #air1_off: "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Test.py 2302 4'"
  14.  
  15.   air1_on:  "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/On_air1.py'"
  16.   air1_off: "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Off_air1.py'"
  17.  
  18.   air2_on:  "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/On_air2.py'"
  19.   air2_off: "ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' pi@192.168.0.3 'python3 /home/pi/Off_air2.py'"
  20.  
  21.   # --- DEBUG COMMANDS ---
  22.   #debug_find_path: "pwd > pwd.txt && ls -la >> pwd.txt"
  23.  
  24. # New sensor for human-readable status
  25. sensor:
  26.   - platform: template
  27.     sensors:
  28.       air_status:
  29.         friendly_name: "สถานะแอร์"
  30.         value_template: >-
  31.           {% set state = states('input_select.active_air') %}
  32.           {% if state == 'air1' %}
  33.             Air 1 กำลังทำงาน
  34.           {% elif state == 'air2' %}
  35.             Air 2 กำลังทำงาน
  36.           {% elif state == 'both' %}
  37.             Air 1 และ Air 2 กำลังทำงาน
  38.           {% else %}
  39.             แอร์ทุกเครื่องปิดอยู่
  40.           {% endif %}
  41.  
  42. switch:
  43.   - platform: template
  44.     switches:
  45.       air1_switch:
  46.         friendly_name: "Air 1"
  47.         value_template: "{{ states('input_select.active_air') in ['air1', 'both'] }}"
  48.         turn_on:
  49.           - service: shell_command.air1_on
  50.           - service: input_select.select_option
  51.             target:
  52.               entity_id: input_select.active_air
  53.             data:
  54.               option: >-
  55.                 {% if is_state('input_select.active_air', 'air2') %}
  56.                   both
  57.                 {% else %}
  58.                   air1
  59.                 {% endif %}
  60.         turn_off:
  61.           - service: shell_command.air1_off
  62.           - service: input_select.select_option
  63.             target:
  64.               entity_id: input_select.active_air
  65.             data:
  66.               option: >-
  67.                 {% if is_state('input_select.active_air', 'both') %}
  68.                   air2
  69.                 {% else %}
  70.                   none
  71.                 {% endif %}
  72.  
  73.       air2_switch:
  74.         friendly_name: "Air 2"
  75.         value_template: "{{ states('input_select.active_air') in ['air2', 'both'] }}"
  76.         turn_on:
  77.           - service: shell_command.air2_on
  78.           - service: input_select.select_option
  79.             target:
  80.               entity_id: input_select.active_air
  81.             data:
  82.               option: >-
  83.                 {% if is_state('input_select.active_air', 'air1') %}
  84.                   both
  85.                 {% else %}
  86.                   air2
  87.                 {% endif %}
  88.         turn_off:
  89.           - service: shell_command.air2_off
  90.           - service: input_select.select_option
  91.             target:
  92.               entity_id: input_select.active_air
  93.             data:
  94.               option: >-
  95.                 {% if is_state('input_select.active_air', 'both') %}
  96.                   air1
  97.                 {% else %}
  98.                   none
  99.                 {% endif %}
  100.  
  101.       # New switch for controlling both
  102.       all_air_switch:
  103.         friendly_name: "Air1+Air2"
  104.         value_template: "{{ is_state('input_select.active_air', 'both') }}"
  105.         turn_on:
  106.           - service: shell_command.air1_on
  107.           - service: shell_command.air2_on
  108.           - service: input_select.select_option
  109.             target:
  110.               entity_id: input_select.active_air
  111.             data:
  112.               option: 'both'
  113.         turn_off:
  114.           - service: shell_command.air1_off
  115.           - service: shell_command.air2_off
  116.           - service: input_select.select_option
  117.             target:
  118.               entity_id: input_select.active_air
  119.             data:
  120.               option: 'none'
  121.  
  122. input_select:
  123.   active_air:
  124.     name: Air ที่ทำงานอยู่
  125.     options:
  126.       - none
  127.       - air1
  128.       - air2
  129.       - both # Added 'both' state
  130.     initial: none


4. ไฟล์ automation.yaml เพื่อให้สลับ Air อัตโนมัติ เพิ่ม
  1. #### Off ก่อนแล้วค่อย ON เพื่อไม่ให้ Switch เปิดทั้ง 2
  2. #### delay 15 วิ รอให้ คำสั่งแรกทำงานเสร็จก่อน ถ้าทำต่อเลย คำสั่งแรกยังทำไม่เสร็จ Status จะไม่ถูก
  3. - id: air1_0000
  4.   alias: "Air1 00:00 - 02:59"
  5.   trigger:
  6.     - platform: time
  7.       at: "00:00:00"
  8.   action:
  9.     - service: switch.turn_off
  10.       target:
  11.         entity_id: switch.all_air_switch
  12.     - delay: "00:00:15"   # รอ 15 วินาที      
  13.     - service: switch.turn_on
  14.       target:
  15.         entity_id: switch.air1_switch
  16.  
  17. - id: air2_0300
  18.   alias: "Air2 03:00 - 05:59"
  19.   trigger:
  20.     - platform: time
  21.       at: "03:00:00"
  22.   action:
  23.     - service: switch.turn_off
  24.       target:
  25.         entity_id: switch.all_air_switch
  26.     - delay: "00:00:15"   # รอ 15 วินาที      
  27.     - service: switch.turn_on
  28.       target:
  29.         entity_id: switch.air2_switch
  30.  
  31. - id: air1_0600
  32.   alias: "Air1 06:00 - 08:59"
  33.   trigger:
  34.     - platform: time
  35.       at: "06:00:00"
  36.   action:
  37.     - service: switch.turn_off
  38.       target:
  39.         entity_id: switch.all_air_switch
  40.     - delay: "00:00:15"   # รอ 15 วินาที      
  41.     - service: switch.turn_on
  42.       target:
  43.         entity_id: switch.air1_switch
  44.  
  45. - id: air2_0900
  46.   alias: "Air2 09:00 - 11:59"
  47.   trigger:
  48.     - platform: time
  49.       at: "09:00:00"
  50.   action:
  51.     - service: switch.turn_off
  52.       target:
  53.         entity_id: switch.all_air_switch
  54.     - delay: "00:00:15"   # รอ 15 วินาที      
  55.     - service: switch.turn_on
  56.       target:
  57.         entity_id: switch.air2_switch
  58.  
  59. - id: air1_1200
  60.   alias: "Air1 12:00 - 14:59"
  61.   trigger:
  62.     - platform: time
  63.       at: "12:00:00"
  64.   action:
  65.     - service: switch.turn_off
  66.       target:
  67.         entity_id: switch.all_air_switch
  68.     - delay: "00:00:15"   # รอ 15 วินาที      
  69.     - service: switch.turn_on
  70.       target:
  71.         entity_id: switch.air1_switch
  72.  
  73. - id: air2_1500
  74.   alias: "Air2 15:00 - 17:59"
  75.   trigger:
  76.     - platform: time
  77.       at: "15:00:00"
  78.   action:
  79.     - service: switch.turn_off
  80.       target:
  81.         entity_id: switch.all_air_switch
  82.     - delay: "00:00:15"   # รอ 15 วินาที    
  83.     - service: switch.turn_on
  84.       target:
  85.         entity_id: switch.air2_switch
  86.  
  87. - id: air1_1800
  88.   alias: "Air1 18:00 - 20:59"
  89.   trigger:
  90.     - platform: time
  91.       at: "18:00:00"
  92.   action:
  93.     - service: switch.turn_off
  94.       target:
  95.         entity_id: switch.all_air_switch
  96.     - delay: "00:00:15"   # รอ 15 วินาที      
  97.     - service: switch.turn_on
  98.       target:
  99.         entity_id: switch.air1_switch
  100.  
  101. - id: air2_2100
  102.   alias: "Air2 21:00 - 23:59"
  103.   trigger:
  104.     - platform: time
  105.       at: "21:00:00"
  106.   action:
  107.     - service: switch.turn_off
  108.       target:
  109.         entity_id: switch.all_air_switch
  110.     - delay: "00:00:15"   # รอ 15 วินาที      
  111.     - service: switch.turn_on
  112.       target:
  113.         entity_id: switch.air2_switch

5. ที่เครื่อง PI เพิ่มไฟล์ 4 ไฟล์ สำหรับ HA เรียกใช้
On_air1.py, On_air2.py, Off_air1.py, Off_air2.py
Code
On_air1.py
  1. #!/usr/bin/python3
  2. import time
  3. import RPi.GPIO as GPIO
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. GPIO.setwarnings(False)
  8. GPIO.setup(20, GPIO.OUT)
  9. GPIO.output(20, GPIO.LOW)
  10. #GPIO.setup(21, GPIO.OUT)
  11. #GPIO.output(21, GPIO.LOW)
  12.  
  13. ##### Rocketchat #####
  14. import requests
  15. from datetime import datetime
  16. def send_to_rocketchat(message):
  17.    url = "http://xx.xx.xx.xx:3000/api/v1/chat.postMessage"
  18.  
  19.    headers = {
  20.       "Content-type": "application/json",
  21.       "X-Auth-Token": "XXX",
  22.       "X-User-Id": "XXX"
  23.    }
  24.  
  25.    payload = {
  26.        "channel": "#Test", #IT_Notification
  27.        "text": message
  28.    }
  29.    response = requests.post(url, json=payload, headers=headers)
  30.  
  31. message = "แจ้งเตือน เปิด AIR1 ปิด AIR2 " + datetime.today().strftime('%Y-%m-%d %H:%M:%S')
  32.  
  33. send_to_rocketchat(message)
  34.  

On_air2.py
  1. #!/usr/bin/python3
  2. import time
  3. import RPi.GPIO as GPIO
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. GPIO.setwarnings(False)
  8. #GPIO.setup(20, GPIO.OUT)
  9. #GPIO.output(20, GPIO.LOW)
  10. GPIO.setup(21, GPIO.OUT)
  11. GPIO.output(21, GPIO.LOW)
  12.  
  13.  
  14. ##### Rocketchat #####
  15. import requests
  16. from datetime import datetime
  17. def send_to_rocketchat(message):
  18.    url = "http://192.168.2.76:3000/api/v1/chat.postMessage"
  19.  
  20.    headers = {
  21.       "Content-type": "application/json",
  22.       "X-Auth-Token": "XXX",
  23.       "X-User-Id": "XXX"
  24.    }
  25.  
  26.    payload = {
  27.        "channel": "#Test", #IT_Notification
  28.        "text": message
  29.    }
  30.    response = requests.post(url, json=payload, headers=headers)
  31.  
  32. message = "แจ้งเตือน เปิด AIR2 ปิด AIR1 " + datetime.today().strftime('%Y-%m-%d %H:%M:%S')
  33.  
  34. send_to_rocketchat(message)
  35.  

Off_air1.py

  1. import time
  2. import RPi.GPIO as GPIO
  3.  
  4. GPIO.setmode(GPIO.BCM)
  5.  
  6. GPIO.setwarnings(False)
  7. GPIO.setup(20, GPIO.OUT)
  8. GPIO.output(20, GPIO.HIGH)
  9. #GPIO.setup(21, GPIO.OUT)
  10. #GPIO.output(21, GPIO.HIGH)
  11. GPIO.cleanup()
  12.  

Off_air2.py
  1. #!/usr/bin/python3
  2. import time
  3. import RPi.GPIO as GPIO
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. GPIO.setwarnings(False)
  8. #GPIO.setup(20, GPIO.OUT)
  9. #GPIO.output(20, GPIO.HIGH)
  10. GPIO.setup(21, GPIO.OUT)
  11. GPIO.output(21, GPIO.HIGH)
  12. GPIO.cleanup()

 

Home Assistant with Docker HACS+Editor-card

 Home Assistant with Docker HACS+Editor-card

 

1. add lxc debian template
2. install sudo ติดตั้ง docker และ program ที่ต้องใช้
  1. for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do apt-get remove $pkg; done
  2. apt-get install ca-certificates curl
  3. install -m 0755 -d /etc/apt/keyrings
  4. curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
  5. echo   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  6. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" |   tee /etc/apt/sources.list.d/docker.list > /dev/null
  7. apt-get update
  8. apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

3.ติดตั้ง Portainer : https://intranet.scivalve.com/blog.php?u=3&b=1961
  1. docker volume create portainer_data
  2. docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

4. สร้างที่เก็บ config
  1. mkdir /HA

5. ติดตั้ง docker แล้วตั้งค่า
  1. docker run -d \
  2.   --name homeassistant \
  3.   --privileged \
  4.   --restart=unless-stopped \
  5.   -e TZ=Asia/Bangkok \
  6.   -v /HA:/config \
  7.   -v /run/dbus:/run/dbus:ro \
  8.   --network=host \
  9.   ghcr.io/home-assistant/home-assistant:stable

6. ติดตั้ง HACS
  1. docker ps
  2. docker exec -it [CONTAINER_ID] bash -c "wget -O - https://get.hacs.xyz | bash -"

7. restart HA
  1. docker restart <container_name_or_id>

8. เข้า IP:8123 ตั้งค่า HA, Password และอื่น ๆ เพิ่ม integration ชื่อ HACS
เข้าไปที่ Settings --> Devices & Services กด Add integration ค้นหา HACS เพิ่มกด Link ไป github แล้วเอา เลยไปใส่ใน Account github
9. ติดตั้ง Config Editor เพื่อช่วยแก้ไฟล์ config ต่าง ๆ
- เข้าเมนู HACS
- ค้นหาคำว่า config editor
- Download addon ทั้ง 2 ตัว (Config Editor, Config Editor Card)
- เพิ่มข้อความในไฟล์ configuration.yaml
  1. config_editor:


- Restart HA + Reload browser
- เพิ่มเมนูใน Dashboard
- Setting --> Dashboards --> + Add dashboard
- ไปที่ Dashboards ที่เพิ่มมาใหม่ เปลี่ยน View configuration ที่ Add ขึ้นมาใหม่ให้เป็น Panel (single card) เพื่อจะได้เต็มจอ
- Add card ค้นหา Config Editor Card กด save
type text จะเป็น
  1. type: custom:config-editor-card

2/24/2020

Home Assistant : HA Install On Pi Run Play MP3

Home Assistant : HA Install On Pi Run Play MP3
1. Download PI และ Restore ใส่ SD Card นำไป Boot
https://www.raspberrypi.org/downloads/raspbian/

2. ตั้งค่า wifi
https://raspberrypihq.com/how-to-connect-your-raspberry-pi-to-wifi/
ถ้ามี Error แสดง
Wi-Fi is currently blocked by rfkill

ใช้คำสั่ง
  1. sudo rfkill unblock 0
  2. ifconfig wlan0 up

https://www.raspberrypi.org/forums/viewtopic.php?t=206223

3. Set Wifi Connect
  1. iwlist wlan0 scan
  2. nano /etc/wpa_supplicant/wpa_supplicant.conf

เพิ่ม
network={
ssid="SCI_EMP"
psk="XXXXXX"
}
แล้ว reboot เครื่อง Run คำสั่ง
  1. ifconfig wlan0

จะได้ IP

4. Config rapberry pi
เลือก Time zone
  1. open ssh timezone

  1. raspi-config

- เปลี่ยนชื่อ Host ตามที่ต้องการ เช่น pi-moning
- เปิด ssh
และอื่น ๆ
Run set locale
  1. locale-gen en_US en_US.UTF-8 th_TH th_TH.UTF-8

ติดตั้ง ntp และ config ntp

5. ติดตั้ง
https://www.home-assistant.io/docs/installation/raspberry-pi/
Run Command
  1. sudo apt-get update
  2. sudo apt-get upgrade -y
  3. sudo apt-get install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev
  4. sudo useradd -rm homeassistant -G dialout,gpio,i2c
  5. cd /srv
  6. sudo mkdir homeassistant
  7. sudo chown homeassistant:homeassistant homeassistant
  8. sudo -u homeassistant -H -s
  9. cd /srv/homeassistant
  10. python3 -m venv .
  11. source bin/activate
  12. python3 -m pip install wheel
  13. pip3 install homeassistant


hass ใช้เวลาสักพัก ประมาณ 20-30 นาที
ถ้าขึ้น INFO (MainThread) [homeassistant.core] Timer:starting คือเสร็จแล้ว
6. เข้าหน้า Web ตั้งรหัสผ่าน user
http://ipaddress:8123

7. ทำ hass auto service
ทำต่อจากข้อ 8
https://intranet.sci.com/blog.php?u=281&b=1603
เพิ่มเติม
https://www.home-assistant.io/docs/autostart/systemd/
https://community.home-assistant.io/t/unit-home-assistant-homeassistant-service-not-found/28507

8. Confing configuration.yaml
all config file
  1. cd /home/homeassistant/.homeassistant

คำสั่ง restart service
  1. systemctl restart home-assistant@homeassistant

AIQ
สร้างไฟล aqi.sh
#!/bin/sh
#New Key 20200206 348 Day
result=$(curl -s --location --request GET "api.airvisual.com/v2/city?city=Samut%20Sakhon&state=Samut%20Sakhon&country=Thailand&key=XXXXXXXXXXXXXX")
echo $result | jq -r .data.current.pollution.aqius


สมัครฟรี 10000 ครั้ง/เดือน หมดอายุ 348 วัน XXXX คือคีย์ที่ได้จาก Web
https://www.airvisual.com/thailand/bangkok

9. ติดต้ง jq
  1. sudo apt install jq

10. Code sh ยัดค่าใส่ sensor
  1. nano configuration.yaml

เพิ่ม
#AIQ From aiq.sh
sensor:
- platform: command_line
name: AQI
scan_interval: 3600
command: "/home/homeassistant/.homeassistant/aqi.sh"


11. เล่นเพลง MP3
copy mp3 ไปไว้ตามที่ต้องการ
ติดตั้งโปรแกรม
  1. apt-get install omxplayer

ทดลองเล่นด้วยคำสั่ง
  1. omxplayer /home/pi/MP3/Auto_7.55.mp3

ถ้ามี Error
Error: Unable to open font

ให้ติดตั้ง fonts เพิ่ม
  1. apt-get install libpcre3 fonts-freefont-ttf

https://raspberrypi.stackexchange.com/questions/32755/install-omxplayer-on-the-commandline

จากข้อ 11. user อื่น ๆ เล่นได้แต่พอไป Test run จาก หน้า Web Home Assistant จะไม่ได้ ไม่เล่นเพลง
ต้อง Add user homeassistant เข้า Group ด้วย ไม่อย่างนั้น จะ Error เมื่อเรียกจาก homeassistant
  1. usermod -a -G audio homeassistant
  2. usermod -G video homeassistant

แล้ว reboot เครื่อง

ลอง Run ที่ User homeassistant
  1. su - homeassistant
  2. /usr/bin/omxplayer /home/pi/MP3/1.mp3


12. Fix IP Wifi
https://electrondust.com/2017/11/25/setting-raspberry-pi-wifi-static-ip-raspbian-stretch-lite/
  1. nono /etc/dhcpcd.conf

เพิ่มไว้ล่างสุด
interface wlan0
static ip_address=192.168.0.175
static routers=192.168.0.2
static domain_name_servers=8.8.8.8

แล้ว Reboot เครื่อง

13. configuration.yaml
  1. nano configuration.yaml

shell_command:
auto_normal: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Auto_7.55.mp3
song_national: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Song_National.mp3
no_act: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/No_Act.mp3
song_enough: /usr/bin/omxplayer /home/homeassistant/.homeassistant/MP3/Song_Enough.mp3
stop_mp3: bash /home/homeassistant/.homeassistant/stop_mp3.sh


14. scrip.yaml
  1. nano scrip.yaml

'1582256882671':
alias: 1. ^{ ^a ^d ^h ^` ^b ^i ^a ^v Manual
sequence:
- service: shell_command.no_act
- service: system_log.write
'1582256923595':
alias: 2. ^` ^~ ^g ^h ^h ^g ^~ ^` ^~ ^g Manual
sequence:
- service: shell_command.song_enough
'1582256950346':
alias: 3. ^` ^~ ^g ^j ^u Manual
sequence:
- service: shell_command.song_national
'1582259558847':
alias: Stop Sound
sequence:
- data: {}
service: shell_command.stop_mp3


15. automations.yaml
  1. nano automations.yaml

- id: '1582084332514'
alias: 1. ^` ^b ^i ^a ^v ^{ ^a ^u ^{ ^a ^w ^a ^y 7:55
description: ^` ^b ^i ^a ^v ^{ ^a ^u ^` ^{ ^t ^` ^~ ^g ^h ^h ^g ^~ ^` ^~ ^g + ^` ^~ ^g ^j ^u
trigger:
- at: '7:54:57'
platform: time
condition: []
action:
- service: shell_command.auto_normal
initial_state: true
- id: '1582106072377'
alias: 2. ^g ^t ^a ^h ^a 7:30, 7:50 ^a ^` ^{ ^t ^` ^~ ^g ^j ^u 8:00
description: ^{ ^a ^g ^t ^a ^h ^a ^a ^` ^{ ^t ^` ^~ ^g ^j ^u 8:00
trigger:
- at: '7:30:00'
platform: time
condition: []
action:
- data: {}
service: shell_command.no_act
- delay: 00:19:45
- service: shell_command.no_act
- delay: 00:09:45
- service: shell_command.song_national
initial_state: false


***** เพิ่มเติม IP Wifi มีปัญหา ****
พบปัญหา Wifi ที่ Config จาก Service dhcpcd มีปัญหา
เครื่องเปิดปกติทำงานปกติ แต่ Wifi ใช้ไม่ได้เข้าไม่ได้

เปลี่ยนวิธี Config ip wifi ใหม่ ใช้
https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address

  1. nano /etc/network/interfaces

เพิ่ม
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.175
netmask 255.255.255.0
gateway 192.168.0.2
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


ของ Lan ถ้าต้องการใช้ก็ Fix จากไฟล์นี้เหมือนกัน เช่น
auto eth0
iface eth0 inet static
address 10.1.1.30
netmask 255.255.255.0
gateway 10.1.1.1


แล้วปิด Service dhcpcd ไปให้ไปใช้ network/interfaces แทน
  1. sudo systemctl disable dhcpcd
  2. sudo systemctl enable networking


แล้ว Reboot เครื่อง ก็จะสามารถใช้ IP ที่ Fix ได้

10/28/2019

Home Assistant : ติดตั้ง Tracking Wifi แจ้งเตือนผ่าน Slack+Mail

Home Assistant : ติดตั้ง Tracking Wifi แจ้งเตือนผ่าน Slack+Mail
IP: 192.168.0.50:8123
1. ติดตั้ง Home Assistant + Mqtt
https://intranet.sci.com/blog.php?u=281&b=1603

** ใช้วง 0 เพราะ Access point TP-Ling TL-WR841ND ไม่รองรับการ Ping ข้าม Subnet คนละวง ทำให้ Track Ping ไม่ติด**

2. สร้าง Device สร้างไฟล์ known_devices.yaml เพื่อให้นำมา Track ได้หลาย Devices เช่น
  1. 19LIB:
  2.   hide_if_away: false
  3.   icon:
  4.   mac:
  5.   name: 19LIB
  6.   picture:
  7.   track: true
  8.   vendor:
  9.   gravatar: suwit@scivalve.com
  10. 20WH:
  11.   hide_if_away: false
  12.   icon:
  13.   mac:
  14.   name: 20WH
  15.   picture:
  16.   track: true
  17.   vendor:
  18.   gravatar: suwit@scivalve.com
  19. 226MN:
  20.   hide_if_away: false
  21.   icon:
  22.   mac:
  23.   name: 226MN
  24.   picture:
  25.   track: true
  26.   vendor:
  27.   gravatar: suwit@scivalve.com


3. แก้ไฟล์ configuration.yaml
  1. #//////////// SCI ADD //////////////////#
  2. # interval_seconds ping every 30 seconds.
  3. # consider_home if ping not reply 300 seconds change status to away. Mark Not Use.
  4. device_tracker:
  5.   - platform: ping
  6.     interval_seconds: 30
  7.     #consider_home: 300
  8.     hosts:
  9.       19LIB: 192.168.0.19
  10.       20WH: 192.168.0.20
  11.       226MN: 192.168.0.226
  12.       223S2: 192.168.0.223
  13.       221PD1: 192.168.0.221
  14.       227PD2: 192.168.0.227
  15.       37QA: 192.168.0.37
  16.       173Test: 192.168.0.173
  17.  
  18. mqtt:
  19.   broker: 127.0.0.1
  20. notify:
  21.   - name: ha_slack
  22.     platform: slack
  23.     api_key: XXXXXXXXXXXXXXXXXXXXXXX Token ID
  24.     default_channel: '#wifi'
  25.  
  26.   - name: "SendMail"
  27.     platform: smtp
  28.     server: smtp.gmail.com
  29.     port: 587
  30.     timeout: 15
  31.     sender: scivalve.suwit@gmail.com
  32.     encryption: starttls
  33.     username: scivalve.suwit@gmail.com
  34.     password: xxxxxxxx
  35.     recipient:
  36.       - suwit.jph@gmail.com
  37.       - komkid@gmail.com
  38.       - seksan.kamt56@gmail.com
  39.     sender_name: Home Assistant Check WIFI


4. สร้าง Automation ส่งเมล์ และ ส่ง Slack เมื่อ Status Wifi ไม่ใช่ Home เช่น
  1. - id: '1569644672332'
  2.   alias: Check_173Test
  3.   trigger:
  4.   - entity_id: device_tracker.173test
  5.     platform: state
  6.   condition:
  7.   - after: '8:00'
  8.     before: '18:00'
  9.     condition: time
  10.   - condition: state
  11.     entity_id: device_tracker.173test
  12.     state: not_home
  13.   action:
  14.   - data:
  15.       message: 'Can''t connect WIFI 192.168.0.173 Test.! : {{ now ().year }}-{{ now
  16.         ().month }}-{{ now ().day }}_{{ now ().hour }}-{{ now ().minute }}-{{ now
  17.         ().second }}'
  18.     service: notify.sendmail
  19.   - data:
  20.       message: 'Can''t connect WIFI 192.168.0.173 Test.! : {{ now ().year }}-{{ now
  21.         ().month }}-{{ now ().day }}_{{ now ().hour }}-{{ now ().minute }}-{{ now
  22.         ().second }}'
  23.       target:
  24.       - '#wifi'
  25.       title: Wifi Loss
  26.     service: notify.ha_slack


5. สร้าง Channel ชื่อ wifi ที่ slack เพิ่ม User และต้อง add app sci-bot ด้วยไม่อย่างนั้นจะส่ง slack มาไม่ผ่าน
https://app.slack.com/client/T03LZ3H08/CNN6QLQ9F/user_profile/UG00D2V5F

Track Multi Device Example:
https://joshmccarty.com/track-homeaway-status-household-using-smart-phones-home-assistant/

https://www.home-assistant.io/components/device_tracker/


5/29/2019

Home Assistant :: Server Switch ใช้กรณี ปิดเปิด Air Manual

Home Assistant :: Server Switch ใช้กรณี ปิดเปิด Air Manual เมื่อ MN-Server ไม่ทำงานหรือสั่งการไม่ได้ (สถานะต้องปิดไว้ถ้าเปิดจะเปิด 2 เครื่องพร้อมกัน)

http://192.168.2.200:8123/




มีไว้สำหรับปิด หรือ เปิด Air Manual เมื่อ MN-Server ไม่ทำงาน หรือสั่งงานไม่ได้
1. ROOM_A_ON_OFF (ไว้ปิดเปิด Air ตึกหน้า เปิดจะเปิดทั้งสองตัว เพราะมี PI เครื่องเดียว สั่ง GPI อันเดียว)
  1. nano /home/homeassistant/.homeassistant/configuration.yaml

  1.  - platform: mqtt
  2.     name: "ROOM_A_ON_OFF"
  3.     state_topic: "hass/state/ROOM_A_ON_OFF"
  4.     command_topic: "hass/cmd/ROOM_A_ON_OFF"
  5.     payload_on: "ON"
  6.     payload_of: "OFF"
  7.     retain: true

ใช้ Service ที่เครื่อง Raspberry PI ในการรับส่ง mqtt ชื่อ ROOM_A_ON_OFF
Code ที่เครื่อง PI
  1. nano /home/pi/mqttAir.py

  1.  
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4.  
  5. # Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
  6. #
  7. # All rights reserved. This program and the accompanying materials
  8. # are made available under the terms of the Eclipse Distribution License v1.0
  9. # which accompanies this distribution.
  10. #
  11. # The Eclipse Distribution License is available at
  12. #   http://www.eclipse.org/org/documents/edl-v10.php.
  13. #
  14. # Contributors:
  15. #    Roger Light - initial implementation
  16. # Copyright (c) 2010,2011 Roger Light <roger@atchoo.org>
  17. # All rights reserved.
  18.  
  19. # This shows a simple example of an MQTT subscriber.
  20.  
  21. #import context  # Ensures paho is in PYTHONPATH
  22. import paho.mqtt.client as mqtt
  23.  
  24. ### PI ###
  25. import RPi.GPIO as GPIO                 #Add the GPIO library to a Python sketch
  26. import time                             #Add the time library to a Python sketch
  27. GPIO.setwarnings(False)
  28. GPIO.setmode(GPIO.BOARD)                #Setup GPIO using Board numbering
  29. GPIO.setup(16, GPIO.OUT)
  30.  
  31. ### Piface ###
  32. import pifacedigitalio
  33. pf = pifacedigitalio.PiFaceDigital()
  34.  
  35. def on_connect(mqttc, obj, flags, rc):
  36.     print("rc: " + str(rc))
  37.  
  38. def on_message(mqttc, obj, msg):
  39.     print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
  40.     mqttc.publish("hass/state/ROOM_A_ON_OFF", str(msg.payload), 0, True)
  41. ## ON OFF ##
  42.    if str(msg.payload) == "ON":
  43.         #GPIO.output(16,GPIO.HIGH)
  44.         pf.relays[1].turn_on()
  45.         pf.relays[0].turn_on()
  46.     else:
  47.         pf.relays[1].turn_off()
  48.         pf.relays[0].turn_off()
  49.         #GPIO.output(16,GPIO.LOW)
  50.  
  51. def on_publish(mqttc, obj, mid):
  52.     print("mid: " + str(mid))
  53.  
  54. def on_subscribe(mqttc, obj, mid, granted_qos):
  55.     print("Subscribed: " + str(mid) + " " + str(granted_qos))
  56.  
  57. def on_log(mqttc, obj, level, string):
  58.     print(string)
  59.  
  60. # If you want to use a specific client id, use
  61. # mqttc = mqtt.Client("client-id")
  62. # but note that the client id must be unique on the broker. Leaving the client
  63. # id parameter empty will generate a random id for you.
  64. mqttc = mqtt.Client()
  65. mqttc.on_message = on_message
  66. mqttc.on_connect = on_connect
  67. mqttc.on_publish = on_publish
  68. mqttc.on_subscribe = on_subscribe
  69. # Uncomment to enable debug messages
  70. # mqttc.on_log = on_log
  71. mqttc.connect("192.168.2.200", 1883, 60)
  72. mqttc.subscribe("hass/cmd/ROOM_A_ON_OFF", 0)
  73.  
  74. mqttc.loop_forever()
  75.  
  76.  


Run เป็น Service
https://intranet.sci.com/blog.php?u=281&b=1583
Service
  1. sudo python /home/pi/mqttAir.py

ไว้รับคำสั่ง mqtt และสั่งเปิด Air ตาม Code

****** หมายเหตุ บางครั้งสั่งแล้วไม่ปิดไม่เปิด Service อาจจะค้าง ต้อง Restart Service ใหม่ หรือ Reboot เครื่อง PI ใหม่ *******

2. ROOM_B_ON_OFF (รอติดตั้ง ห้อง Server ตึกหลัง)

8/17/2018

Home Assistant :: Home Assistant Send Mail Gmail SMTP

Home Assistant :: Home Assistant Send Mail Gmail SMTP
1. Config configuration.yaml
  1. cd /home/homeassistant/.homeassistant
  1. nano configuration.yaml


2. Insert code notification and set Gmail user password
  1. notify:
  2.   - name: "SendMail"
  3.     platform: smtp
  4.     server: smtp.gmail.com
  5.     port: 587
  6.     timeout: 15
  7.     sender: scivalve.suwit@gmail.com
  8.     encryption: starttls
  9.     username: scivalve.suwit@gmail.com
  10.     password: Password Gmail
  11.     recipient:
  12.       - suwit.jph@gmail.com
  13.       - komkid@gmail.com
  14.       - seksan.kamt56@gmail.com
  15.     sender_name: My Home Assistant
  16.  


3. Test Send mail select service menu and select service we create on stab 2.
insert Service Data Optional And click CALL SERVICE check mail.
  1. {"message":"Test mail"}

4. Set Automation for send mail when track device ping down.
  1. nano automations.yaml

Add this code.
  1. - id: '1534491838266'
  2.   alias: CheckPingDown
  3.   trigger:
  4.   - entity_id: device_tracker.sonoff
  5.     platform: state
  6.   condition:
  7.   - after: '8:00'
  8.     before: '18:00'
  9.     condition: time
  10.   - condition: state
  11.     entity_id: device_tracker.sonoff
  12.     state: not_home
  13.   action:
  14.   - data:
  15.       message: Can't connect sonoff Garden. Please Check WIFI Or Sonoff.
  16.     service: notify.sendmail


https://www.home-assistant.io/components/notify.smtp/