Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## 功能:
- ## 1. 能用Line下指令控制設備
- ## 2. 能加上確認執行(是/否) 以免誤動作
- ## 3. 能詢問要調整設備的數值(限整數)為何?如調整亮度、溫度
- ##---------------------------------------------------------------
- ## 要達成能夠簡易對答請先完成以下事前準備:
- ## 1. 申請line bot 並在webhook選項加上webhook_id
- ## 2. 申請line notify功能
- ## 3. 把你與line bot, line notify加進群組
- ## 4. 為這個群組申請一組line notify token,並在HA新增notify服務
- ## 5. 將本篇所有"notify.simon_home_notify"換成自己的line notify實體ID
- ## 6. 將此文的webhook_id改為你在line bot設定的webhook_id
- ## 7. 所有內容存在/config/packages/line_webhook_cmd.yaml內
- ## 8. YAML檢查錯誤沒問題後,重新載入腳本、自動化、輸入文字、輸入數字
- ## 9. 測試沒問題後,即可自由新增關鍵字與動作,存檔完記得要再做一次步驟8
- ## Credit Simon from Line社群
- ###########################################################
- ## Automation
- ###########################################################
- automation:
- - id: line_webhook_cmd_timer
- alias: line_webhook_cmd_timer
- description: "Line指令太久沒反應取消指令"
- mode: restart
- trigger:
- - platform: state
- entity_id: input_text.line_lastcmd
- from: " "
- not_to: " "
- condition: []
- action:
- - delay: "00:00:20"
- - if:
- - condition: template
- value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"
- then:
- - service: notify.simon_home_notify
- data:
- message: "未收到回答,取消執行"
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: " "
- - id: line_webhook_cmd
- alias: line_webhook_cmd
- description: "Line下指令"
- mode: single
- trigger:
- - platform: webhook
- webhook_id: !secret my_WHID #記得更換成自己的webhook_id
- condition: []
- action:
- - choose:
- - alias: "當回答'是'就執行該腳本"
- conditions:
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '是' }}"
- sequence:
- - service: script.execute_line_cmd
- - alias: "當回答'否'就執行該腳本"
- conditions:
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '否' }}"
- sequence:
- - service: script.cancel_line_cmd
- - alias: "當回答'數字'就執行調整數值腳本"
- conditions:
- - condition: template
- value_template: "{{ (trigger.json.events.0.message.text | int(0)) != 0 }}"
- sequence:
- - if:
- - condition: template
- value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"
- then:
- - service: input_number.set_value
- target:
- entity_id: input_number.line_numbercmd
- data:
- value: "{{ trigger.json.events.0.message.text | int(0) }}"
- - alias: "執行數字指令"
- service: script.number_line_cmd
- - alias: "把需要調整數值的動作關鍵字貼在此處,只加關鍵字即可"
- conditions:
- - or:
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '調整客廳亮度' }}"
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '調整冷氣溫度' }}"
- sequence:
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: "{{ trigger.json.events.0.message.text }}"
- - service: notify.simon_home_notify
- data:
- message: "要{{ trigger.json.events.0.message.text }}到多少? (請輸入整數)"
- - alias: "把需要再次確認的動作關鍵字貼在此處,只加關鍵字即可"
- conditions:
- - or:
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '開除濕機' }}"
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '關除濕機' }}"
- sequence:
- - alias: "確認是否執行指令"
- service: script.confirm_line_cmd
- data:
- cmd: "{{ trigger.json.events.0.message.text }}"
- - alias: "不需要再確認的就直接在sequence填入動作,複製此區以增加控制設備與動作"
- conditions:
- - condition: template
- value_template: "{{ trigger.json.events.0.message.text == '開客廳燈' }}" #自行設定觸發的關鍵字
- sequence:
- #當關鍵字觸發時要去做什麼動作,可以去開發工具>服務內
- #先將左下角設UI模式選擇要做什麼動作(開關設備等等),選好後改為YAML顯示,再複製貼在下面
- - service: light.turn_on
- entity_id: light.living_room
- - service: notify.simon_home_notify
- data:
- message: "好,客廳燈已開"
- ###########################################################
- ##
- ## Script
- ##
- ###########################################################
- script:
- confirm_line_cmd:
- alias: "確認是否執行指令"
- sequence:
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: "{{ cmd }}"
- - service: notify.simon_home_notify
- data:
- message: "確認要{{ states('input_text.line_lastcmd') }}嗎? (是/否)"
- cancel_line_cmd:
- alias: "取消指令"
- sequence:
- - if:
- - condition: template
- value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"
- then:
- - service: notify.simon_home_notify
- data:
- message: "了解,{{ states('input_text.line_lastcmd') }}已取消執行"
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: " "
- #如果沒遇到關鍵字就說否,會要求先下指令,如不提示可將else以後刪除或註解
- else:
- - service: notify.simon_home_notify
- data:
- message: "請先下指令!"
- execute_line_cmd:
- alias: "確實執行指令"
- sequence:
- #為避免執行指令時間超過時限,先將指令以變數儲存並清除
- - variables:
- key_word: "{{ states('input_text.line_lastcmd') }}"
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: " "
- - if:
- - condition: template
- value_template: "{{ key_word != '' }}"
- then:
- - choose:
- ## 在此增加實際需要執行的內容與關鍵字
- - conditions:
- - condition: template
- value_template: "{{ key_word == '開除濕機' }}"
- sequence:
- - service: notify.simon_home_notify
- data:
- message: "除濕機已打開並持續除濕"
- - conditions:
- - condition: template
- value_template: "{{ key_word == '關除濕機' }}"
- sequence:
- - service: notify.simon_home_notify
- data:
- message: "除濕機已關閉"
- #如果沒遇到關鍵字就說是,會要求先下指令,如不提示可將else以後刪除或註解
- else:
- - service: notify.simon_home_notify
- data:
- message: "請先下指令!"
- number_line_cmd:
- alias: "執行調整數值指令"
- sequence:
- #為避免執行指令時間超過時限,先將指令以變數儲存並清除
- - variables:
- key_word: "{{ states('input_text.line_lastcmd') }}"
- set_value: "{{ (states('input_number.line_numbercmd')|int(0)) }}"
- - service: input_text.set_value
- target:
- entity_id: input_text.line_lastcmd
- data:
- value: " "
- - alias: "將數字歸零"
- service: input_number.set_value
- target:
- entity_id: input_number.line_numbercmd
- data:
- value: 0
- - choose:
- ## 在此增加實際需要執行的內容與關鍵字
- - conditions:
- - condition: template
- value_template: "{{ key_word == '調整客廳亮度' }}"
- sequence:
- - if:
- - alias: "設定超過範圍"
- condition: template
- value_template: "{{ set_value < 100 }}"
- then:
- - service: notify.simon_home_notify
- data:
- message: "已將客廳亮度調整至{{ set_value }}"
- else:
- - service: notify.simon_home_notify
- data:
- message: "數值超過範圍請重下指令!"
- - conditions:
- - condition: template
- value_template: "{{ key_word == '調整冷氣溫度' }}"
- sequence:
- - if:
- - alias: "設定超過範圍"
- condition: template
- value_template: "{{ 30 > set_value > 20 }}"
- then:
- - service: notify.simon_home_notify
- data:
- message: "已將冷氣溫度調至{{ set_value }}度"
- else:
- - service: notify.simon_home_notify
- data:
- message: "數值超過範圍請重下指令!"
- ###########################################################
- ##
- ## Input Text
- ##
- ###########################################################
- input_text:
- line_lastcmd:
- name: Line LastCmd
- initial: " "
- input_number:
- line_numbercmd:
- name: Line NumberCmd
- initial: 0
- min: 0
- max: 100
- step: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement