Advertisement
xangin

HA Webhook line command with simple confirm

Feb 20th, 2023 (edited)
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 11.20 KB | None | 0 0
  1. ## 功能:
  2. ## 1. 能用Line下指令控制設備
  3. ## 2. 能加上確認執行(是/否) 以免誤動作
  4. ## 3. 能詢問要調整設備的數值(限整數)為何?如調整亮度、溫度
  5. ##---------------------------------------------------------------
  6. ## 要達成能夠簡易對答請先完成以下事前準備:
  7. ## 1. 申請line bot 並在webhook選項加上webhook_id
  8. ## 2. 申請line notify功能
  9. ## 3. 把你與line bot, line notify加進群組
  10. ## 4. 為這個群組申請一組line notify token,並在HA新增notify服務
  11. ## 5. 將本篇所有"notify.simon_home_notify"換成自己的line notify實體ID
  12. ## 6. 將此文的webhook_id改為你在line bot設定的webhook_id
  13. ## 7. 所有內容存在/config/packages/line_webhook_cmd.yaml內
  14. ## 8. YAML檢查錯誤沒問題後,重新載入腳本、自動化、輸入文字、輸入數字
  15. ## 9. 測試沒問題後,即可自由新增關鍵字與動作,存檔完記得要再做一次步驟8
  16. ## Credit Simon from Line社群
  17. ###########################################################
  18. ##       Automation
  19. ###########################################################
  20. automation:
  21.   - id: line_webhook_cmd_timer
  22.     alias: line_webhook_cmd_timer
  23.     description: "Line指令太久沒反應取消指令"
  24.     mode: restart    
  25.     trigger:
  26.       - platform: state
  27.         entity_id: input_text.line_lastcmd
  28.         from: " "
  29.         not_to: " "
  30.     condition: []
  31.     action:
  32.       - delay: "00:00:20"
  33.       - if:
  34.           - condition: template
  35.             value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"      
  36.         then:
  37.           - service: notify.simon_home_notify
  38.             data:
  39.               message: "未收到回答,取消執行"        
  40.           - service: input_text.set_value
  41.             target:
  42.               entity_id: input_text.line_lastcmd
  43.             data:
  44.               value: " "              
  45.  
  46.  
  47.   - id: line_webhook_cmd
  48.     alias: line_webhook_cmd
  49.     description: "Line下指令"
  50.     mode: single
  51.     trigger:
  52.       - platform: webhook
  53.         webhook_id: !secret my_WHID #記得更換成自己的webhook_id
  54.     condition: []
  55.     action:
  56.       - choose:
  57.           - alias: "當回答'是'就執行該腳本"
  58.             conditions:
  59.               - condition: template
  60.                 value_template: "{{ trigger.json.events.0.message.text == '是' }}"
  61.             sequence:
  62.               - service: script.execute_line_cmd  
  63.  
  64.           - alias: "當回答'否'就執行該腳本"
  65.             conditions:
  66.               - condition: template
  67.                 value_template: "{{ trigger.json.events.0.message.text == '否' }}"
  68.             sequence:
  69.               - service: script.cancel_line_cmd
  70.  
  71.           - alias: "當回答'數字'就執行調整數值腳本"
  72.             conditions:
  73.               - condition: template
  74.                 value_template: "{{ (trigger.json.events.0.message.text | int(0)) != 0 }}"
  75.             sequence:
  76.               - if:
  77.                   - condition: template
  78.                     value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"
  79.                 then:
  80.                   - service: input_number.set_value
  81.                     target:
  82.                       entity_id: input_number.line_numbercmd
  83.                     data:
  84.                       value: "{{ trigger.json.events.0.message.text | int(0) }}"
  85.                   - alias: "執行數字指令"
  86.                     service: script.number_line_cmd  
  87.  
  88.           - alias: "把需要調整數值的動作關鍵字貼在此處,只加關鍵字即可"
  89.             conditions:
  90.               - or:
  91.                   - condition: template
  92.                     value_template: "{{ trigger.json.events.0.message.text == '調整客廳亮度' }}"
  93.                   - condition: template
  94.                     value_template: "{{ trigger.json.events.0.message.text == '調整冷氣溫度' }}"                    
  95.             sequence:
  96.               - service: input_text.set_value
  97.                 target:
  98.                   entity_id: input_text.line_lastcmd
  99.                 data:
  100.                   value: "{{ trigger.json.events.0.message.text }}"
  101.               - service: notify.simon_home_notify
  102.                 data:
  103.                   message: "要{{ trigger.json.events.0.message.text }}到多少? (請輸入整數)"
  104.                  
  105.                  
  106.           - alias: "把需要再次確認的動作關鍵字貼在此處,只加關鍵字即可"
  107.             conditions:
  108.               - or:
  109.                   - condition: template
  110.                     value_template: "{{ trigger.json.events.0.message.text == '開除濕機' }}"
  111.                   - condition: template
  112.                     value_template: "{{ trigger.json.events.0.message.text == '關除濕機' }}"                    
  113.             sequence:
  114.               - alias: "確認是否執行指令"
  115.                 service: script.confirm_line_cmd  
  116.                 data:                    
  117.                   cmd: "{{ trigger.json.events.0.message.text }}"
  118.          
  119.           - alias: "不需要再確認的就直接在sequence填入動作,複製此區以增加控制設備與動作"
  120.             conditions:              
  121.               - condition: template
  122.                 value_template: "{{ trigger.json.events.0.message.text == '開客廳燈' }}" #自行設定觸發的關鍵字
  123.             sequence:    
  124.               #當關鍵字觸發時要去做什麼動作,可以去開發工具>服務內
  125.               #先將左下角設UI模式選擇要做什麼動作(開關設備等等),選好後改為YAML顯示,再複製貼在下面
  126.               - service: light.turn_on
  127.                 entity_id: light.living_room          
  128.               - service: notify.simon_home_notify
  129.                 data:
  130.                   message: "好,客廳燈已開"                              
  131.                              
  132. ###########################################################
  133. ##
  134. ##       Script
  135. ##
  136. ###########################################################        
  137. script:
  138.   confirm_line_cmd:
  139.     alias: "確認是否執行指令"
  140.     sequence:
  141.       - service: input_text.set_value
  142.         target:
  143.           entity_id: input_text.line_lastcmd
  144.         data:
  145.           value: "{{ cmd }}"            
  146.       - service: notify.simon_home_notify
  147.         data:
  148.           message: "確認要{{ states('input_text.line_lastcmd')  }}嗎? (是/否)"
  149.  
  150.  
  151.   cancel_line_cmd:
  152.     alias: "取消指令"
  153.     sequence:
  154.       - if:
  155.           - condition: template
  156.             value_template: "{{ states('input_text.line_lastcmd') != ' ' }}"
  157.         then:
  158.           - service: notify.simon_home_notify
  159.             data:
  160.               message: "了解,{{ states('input_text.line_lastcmd') }}已取消執行"
  161.           - service: input_text.set_value
  162.             target:
  163.               entity_id: input_text.line_lastcmd
  164.             data:
  165.               value: " "        
  166.         #如果沒遇到關鍵字就說否,會要求先下指令,如不提示可將else以後刪除或註解
  167.         else:
  168.           - service: notify.simon_home_notify
  169.             data:
  170.               message: "請先下指令!"
  171.                                      
  172.   execute_line_cmd:
  173.     alias: "確實執行指令"
  174.     sequence:
  175.       #為避免執行指令時間超過時限,先將指令以變數儲存並清除
  176.       - variables:
  177.           key_word: "{{ states('input_text.line_lastcmd') }}"    
  178.       - service: input_text.set_value
  179.         target:
  180.           entity_id: input_text.line_lastcmd
  181.         data:
  182.           value: " "
  183.       - if:
  184.           - condition: template
  185.             value_template: "{{ key_word != '' }}"
  186.         then:
  187.           - choose:
  188.          ## 在此增加實際需要執行的內容與關鍵字  
  189.               - conditions:
  190.                   - condition: template
  191.                     value_template: "{{ key_word ==  '開除濕機' }}"
  192.                 sequence:
  193.                   - service: notify.simon_home_notify
  194.                     data:
  195.                       message: "除濕機已打開並持續除濕"
  196.                      
  197.               - conditions:
  198.                   - condition: template
  199.                     value_template: "{{ key_word ==  '關除濕機' }}"
  200.                 sequence:
  201.                   - service: notify.simon_home_notify
  202.                     data:
  203.                       message: "除濕機已關閉"  
  204.        
  205.        
  206.         #如果沒遇到關鍵字就說是,會要求先下指令,如不提示可將else以後刪除或註解
  207.         else:
  208.           - service: notify.simon_home_notify
  209.             data:
  210.               message: "請先下指令!"                
  211.                        
  212.   number_line_cmd:
  213.     alias: "執行調整數值指令"
  214.     sequence:
  215.       #為避免執行指令時間超過時限,先將指令以變數儲存並清除
  216.       - variables:
  217.           key_word: "{{ states('input_text.line_lastcmd') }}"    
  218.           set_value: "{{ (states('input_number.line_numbercmd')|int(0))  }}"
  219.       - service: input_text.set_value
  220.         target:
  221.           entity_id: input_text.line_lastcmd
  222.         data:
  223.           value: " "
  224.       - alias: "將數字歸零"
  225.         service: input_number.set_value
  226.         target:
  227.           entity_id: input_number.line_numbercmd
  228.         data:
  229.           value: 0
  230.          
  231.       - choose:
  232.      ## 在此增加實際需要執行的內容與關鍵字  
  233.           - conditions:
  234.               - condition: template
  235.                 value_template: "{{ key_word ==  '調整客廳亮度' }}"
  236.             sequence:
  237.               - if:
  238.                   - alias: "設定超過範圍"
  239.                     condition: template
  240.                     value_template: "{{ set_value < 100 }}"
  241.                 then:
  242.                   - service: notify.simon_home_notify
  243.                     data:
  244.                       message: "已將客廳亮度調整至{{ set_value }}"
  245.                 else:
  246.                   - service: notify.simon_home_notify
  247.                     data:
  248.                       message: "數值超過範圍請重下指令!"                
  249.                
  250.           - conditions:
  251.               - condition: template
  252.                 value_template: "{{ key_word ==  '調整冷氣溫度' }}"
  253.             sequence:
  254.               - if:
  255.                   - alias: "設定超過範圍"
  256.                     condition: template
  257.                     value_template: "{{ 30 > set_value > 20 }}"
  258.                 then:
  259.                   - service: notify.simon_home_notify
  260.                     data:
  261.                       message: "已將冷氣溫度調至{{ set_value }}度"
  262.                 else:
  263.                   - service: notify.simon_home_notify
  264.                     data:
  265.                       message: "數值超過範圍請重下指令!"
  266.          
  267.                  
  268.  
  269.        
  270.        
  271. ###########################################################
  272. ##
  273. ##       Input Text
  274. ##
  275. ###########################################################
  276. input_text:
  277.   line_lastcmd:
  278.     name: Line LastCmd
  279.     initial: " "
  280.    
  281.    
  282. input_number:
  283.   line_numbercmd:
  284.     name: Line NumberCmd
  285.     initial: 0
  286.     min: 0
  287.     max: 100
  288.     step: 1  
Tags: ha
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement