- 积分
- 4795
- 金钱
- 4287
- 威望
- 0
- 贡献
- 0
- HASS币
- 156
论坛元老
佑桑
- 积分
- 4795
- 金钱
- 4287
- HASS币
- 156
|
本帖最后由 chinyaolin 于 2019-8-23 20:40 编辑
一直以來, 我自己喜歡使用 HA 內建或是官方的方案來處理我自己的需求
所以到目前為止非內建元件只有 hacs 以及 hassmart定制WiFi窗帘电机, 今天要多加一個了
人在台北, 夏天坐在客廳沙發上, 想像著透過 HA 控制空調...
零、硬體設備
博聯 Broadlink RM Pro、小米多功能網關、小米門窗感應器、小米溫濕度、一套可正常運作的 HA
一、建立 MQTT HVAC 空調元件及其他 entity 實例
climate:
- platform: mqtt
name: Livingroom
initial: 29
min_temp: 25
max_temp: 30
modes: ["off", "cool"]
mode_command_topic: "myHASS/livingroom/ac/mode/set"
mode_state_topic: "myHASS/livingroom/ac/mode/state"
swing_modes: ["auto", "5"]
swing_mode_command_topic: "myHASS/livingroom/ac/swing_mode/set"
temperature_command_topic: "myHASS/livingroom/ac/temperature/set"
current_temperature_topic: "myHASS/livingroom/ac/temperature/state"
binary_sensor:
broadlink_livingroom:
initial: 10.59.1.241
input_text:
broadlink_livingroom:
initial: 10.59.1.241
二、建立小米感應器、小米溫濕度相關的自動化
利用小米門窗感應, 貼在空調適合的位置, 能反應出運轉狀態, 並透過自動化 public 至 MQTT
利用小米溫濕度, 在室溫有變化時, 透過自動化 public 至 MQTT
- alias: sensors state for climate are changed
hide_entity: true
trigger:
- platform: state
entity_id:
- binary_sensor.door_window_sensor_158d0002767720 #客廳冷氣門窗感應
- sensor.temperature_158d000254c592 #客廳溫度
action:
- service: mqtt.publish
data_template:
topic: myHASS/livingroom/ac/mode/state
payload: >
{% if is_state("binary_sensor.door_window_sensor_158d0002767720", "off") %}off
{% elif is_state("binary_sensor.door_window_sensor_158d0002767720", "on") %}cool
{% else %}unavailable
{% endif %}
- service: mqtt.publish
data_template:
topic: myHASS/livingroom/ac/temperature/state
payload: '{{states("sensor.temperature_158d000254c592")}}'
三、建立空調 MQTT 變化時相應的自動化
這裡特別的是自動化觸發後, 呼叫同一隻 script 並帶入參數, 達成單一隻 script 可重覆使用的目的
另外, 空調擺葉風向的值會在 HA 重啟後不見, 所以這裡自動化需特別處理一下
#
#
# 客廳冷氣
#
#################################################################################
- alias: mqtt of climate.livingroom changed
hide_entity: true
trigger:
- platform: mqtt
topic: myHASS/livingroom/ac/power/set
- platform: mqtt
topic: myHASS/livingroom/ac/temperature/set
- platform: mqtt
topic: myHASS/livingroom/ac/fan/set
- platform: mqtt
topic: myHASS/livingroom/ac/swing_mode/set
condition:
- condition: template
value_template: '{{ is_state("climate.livingroom","cool") }}'
action:
# 呼叫 發送冷氣紅外碼專用的 script 將參數傳入
- service: script.ac_state_to_broadlink_ir_script_3rd
data_template:
# 五個參數
target_ac_room: livingroom
target_broadlink_ip: '{{ states("input_text.broadlink_livingroom" ) }}'
target_ac_mode: '{{ states("climate.livingroom") }}'
target_ac_temperature: '{{state_attr("climate.livingroom", "temperature") | int}}'
target_ac_swing_mode: '{{state_attr("climate.livingroom", "swing_mode")}}'
#################################################################################
- alias: mqtt of climate.livingroom power on cool
hide_entity: true
trigger:
- platform: mqtt
topic: myHASS/livingroom/ac/mode/set
payload: 'cool'
action:
- service: climate.set_swing_mode
data_template:
entity_id: climate.livingroom
swing_mode: >
{% set smode = state_attr("climate.livingroom", "swing_mode") %}
{% if smode == 'auto' %}auto
{% elif smode == '5' %}5
{% else %}auto
{% endif %}
- service: script.ac_state_to_broadlink_ir_script_3rd
data_template:
target_ac_room: livingroom
target_broadlink_ip: '{{ states("input_text.broadlink_livingroom" ) }}'
target_ac_mode: 'cool'
target_ac_temperature: '{{state_attr("climate.livingroom", "temperature") | int}}'
target_ac_swing_mode: '{{state_attr("climate.livingroom", "swing_mode")}}'
#################################################################################
- alias: mqtt of climate.livingroom power off
hide_entity: true
trigger:
- platform: mqtt
topic: myHASS/livingroom/ac/mode/set
payload: 'off'
action:
- service: script.ac_state_to_broadlink_ir_script_3rd
data_template:
target_ac_room: livingroom
target_broadlink_ip: '{{ states("input_text.broadlink_livingroom" ) }}'
target_ac_mode: 'off'
target_ac_temperature: ''
target_ac_swing_mode: ''
四、撰寫可重覆使用的 script
共五組參數組合出空調指令
指令樣式 on/off(開關)_off/cool(運轉)_sauto/s5~1(擺葉)_fauto/f5~1(風速)_t25/t25~30(溫度)
例 : on_cool_sauto_fauto_t22 / on_cool_s5_fauto_t26 / off
其中風速尚未學習紅外碼, 所以目前未使用
在 state_attr 中所帶的 entity_id = variable.broadlink_kolin_ac_ir_pattern
就是透過 variable 儲存的全域變數對照後取得真正要發送出去的博聯紅外碼
請容我在這裡隱藏一下程式碼
五、安裝及設定 variable 來使用全域變數
透過 HACS 元件倉庫 新增一個源來安裝 : https://github.com/rogro82/hass-variables
直接手動安裝 https://github.com/rogro82/hass-variables
重啟 HA, 設定 configuration.yaml, 再次重啟 HA 後就可使用
附件是完整的 variable 變數, 適用於 博聯 Broadlink RM Pro + 聲寶冷氣 SAMPO
variable:
broadlink_kolin_ac_ir_pattern:
value: "test"
attributes:
{
"on_cool_sauto_fauto_t16": "JgCIAAABJrITNRQ...xUTExMADQU",
"on_cool_sauto_fauto_t17": "JgCIAAABJbQVMhM...RYRFREADQU",
"on_cool_sauto_fauto_t18": "JgCIAAABJbQSNRM...xUTFBIADQU",
"on_cool_sauto_fauto_t19": "JgCIAAABJ7ETNhI...xUTFBMADQU",
"on_cool_sauto_fauto_t20": "JgCIAAABI7QTNBQ...BUTEhIADQU",
"on_cool_sauto_fauto_t21": "JgCIAAABJbIUNRI...hUSFBIADQU",
"on_cool_sauto_fauto_t22": "JgCIAAABJbQSNBM...xUSFBEADQU",
"on_cool_sauto_fauto_t23": "JgCIAAABJrMUMxQ...xQUExQADQU",
"on_cool_sauto_fauto_t24": "JgCIAAABJ7EUNRQ...RIUEhMADQU",
"on_cool_sauto_fauto_t25": "JgCIAAABI7MUNRQ...RMVEhEADQU",
"on_cool_sauto_fauto_t26": "JgCIAAABJrMTNBU...xMVExIADQU",
"on_cool_sauto_fauto_t27": "JgCIAAABJbITNBQ...BMUFBEADQU",
"on_cool_sauto_fauto_t28": "JgCIAAABJbMTNRM...RYSExIADQU",
"on_cool_sauto_fauto_t29": "JgCIAAABJbMTNBM...hYRFBMADQU",
"on_cool_sauto_fauto_t30": "JgCIAAABJbMTNBQ...RIUFBIADQU",
"on_cool_sauto_fauto_t31": "JgCIAAABI7QTMxQ...hUSFBIADQU",
"on_cool_s5_fauto_t31" : "JgCIAAABKbEVMhQ...RMUExUADQU",
"on_cool_s5_fauto_t30" : "JgCIAAABJ7AVMhc...REVEhQADQU",
"on_cool_s5_fauto_t29" : "JgCIAAABKLAVMxQ...BIWERUADQU",
"on_cool_s5_fauto_t28" : "JgCIAAABKa8VMhU...BIVEhQADQU",
"on_cool_s5_fauto_t27" : "JgCIAAABKLAVMhY...hIVERUADQU",
"on_cool_s5_fauto_t26" : "JgCIAAABKLAVMxU...RIWERUADQU",
"on_cool_s5_fauto_t25" : "JgCIAAABKa8VMhU...hIVERUADQU",
"off" : "JgCIAAABJrITNhI...RUSFRMADQU"
}
六、最終成果
|
|