本帖最后由 chinyaolin 于 2020-12-18 10:49 编辑
兩顆論壇改裝模塊 ( 第一顆 hassmart_1ch_aa6aa8 & 第二顆 hassmart_1ch_aa4dfb )
希望達成 hassmart_1ch_aa6aa8 連接實體燈, 與 hassmart_1ch_aa4dfb 空接聯動
PS1. HASS 已經集成 ESPHome API, 不再需要 MQTT
PS2. 已經有更新的解決方案 「ESPHome 透過 WIFI 直接互控 不再需要 HA」 , 有興趣可參考一下
不廢話,直接上代碼
第一顆 hassmart_1ch_aa6aa8
這裡就是一般單開模塊的設定,沒有什麼特別之處
esphome:
name: hassmart_1ch_aa6aa8
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret my_wifi_ssid
password: !secret my_wifi_pw
logger:
api:
password: !secret my_api_pw
ota:
web_server:
binary_sensor:
- platform: gpio
id: btn1
internal: true
pin:
number: GPIO0
mode: INPUT_PULLUP
on_press:
- switch.toggle: relay1
switch:
- platform: gpio
name: "hassmart_1ch_aa6aa8_sw1"
pin: 12
id: relay1
icon: "mdi:lightbulb-on-outline"
status_led:
pin:
number: GPIO13
inverted: no
第二顆 hassmart_1ch_aa4dfb
設定一個 binary_sensor 對應到 HASS 中的第一顆模塊所建立的開關實例
並且在這個實例發生變化時同步使得第二顆模塊的 relay 變化
binary_sensor:
- platform: homeassistant # 客廳燈
id: light_livingroom
name: "light_livingroom"
entity_id: switch.hassmart_1ch_aa6aa8_sw1
on_state:
- if:
condition:
binary_sensor.is_on: light_livingroom
then:
- switch.turn_on: relay1
- logger.log: "light_livingroom is ON!"
else:
- switch.turn_off: relay1
- logger.log: "light_livingroom is OFF!"
設定 switch 時, 多加一些判斷條件
當 switch 有變化時, 同步呼叫 HASS 中第一顆開關的實例, 達成聯動
switch:
# GPIO 12 Relay1
- platform: gpio
name: "hassmart_1ch_aa4dfb_sw1"
pin: 12
id: relay1
icon: "mdi:ceiling-light"
on_turn_on:
- homeassistant.service:
service: switch.turn_on
data:
entity_id: switch.hassmart_1ch_aa6aa8_sw1
on_turn_off:
- homeassistant.service:
service: switch.turn_off
data:
entity_id: switch.hassmart_1ch_aa6aa8_sw1
完整代碼
如果上面幾段都能看懂的話, 可以自行組合, 也不需要回覆
完成後, 第一顆開關的狀態會透過 HASS 反應至第二顆
反之, 第二顆開關的狀態同樣會透過 HASS 反應回第一顆
|