『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 150|回复: 2

[技术探讨] 开关控制多个米家Mesh筒灯同时开关

[复制链接]

4

主题

20

帖子

168

积分

注册会员

Rank: 2

积分
168
金钱
148
HASS币
0
发表于 5 天前 | 显示全部楼层 |阅读模式
本帖最后由 你这泼猴! 于 2024-11-16 17:04 编辑

家里用了一些米家Mesh的筒灯,使用HA自动化通过Xiaomi Gateway3+多模2的方式始终没法实现同时的开和关,总是有个时间差,即便是在米家里给灯编组,在频繁开关的时候响应会特别慢,论坛里搜索了一下没有找到太好的办法,似乎米家灯组的开关状态是通过云更新的,导致延迟很大。今天研究了一下知道Xiaomi Gateway3跟多模2的通讯与通过MQTT的,那么就可以直接绕过Gatway3直接通过MQTT控制米家的灯组,通过MQTTX抓了一下开关灯的消息,用ESPhome直接发送,实测效果非常好,响应迅速,而且再HA挂掉的时候应该是依然可以控制灯。

在这里分享一下,抛砖引玉看看大佬们还有没有更优的方案。

我家的筒灯是通过  机械开关+SonoffMiniR4通断器( ESPHome)的方式控制的, 通断器设置成常闭,这样筒灯是一直供电的,设定一个不用的针脚为虚拟灯,ESPHome里设置读机械开关的状态来切换虚拟灯的状态,然后根据虚拟灯的状态发MQTT消息。 ESPhome配置的写法上还得请大佬们指点一下,怎么再精简一下

但是有个问题,就是打开MQTT之后,模块会广播很多状态,这个要怎么关掉呢,其实是用不上的。
substitutions:
  device_name: light-aisle
  friendly_name: Light_Aisle_Sonoff
  Location: "Aisle"
  static_ip: 192.168.9.224
  light_group: "group.1857342344098631680" #灯组的id
packages:
  device_base: !include sonoffminir4-template.yaml

# Enable Home Assistant API
api:
  encryption:
    key: "FcT0aNnFW+YcpQz/L9U+7NqLD/1kvALMztH8flPbzLw="

ota:
  - platform: esphome
    password: "86bc12805a19fe4544eef6e652bf7d5a"
mqtt:
  broker: 192.168.9.15 #多模2的ip
  port: 1883

switch:
  - id: !extend isolate_mode
    restore_mode: RESTORE_DEFAULT_ON
  
  - id: !extend toggle_mode
    restore_mode: RESTORE_DEFAULT_ON

light:
  - id: !extend light_1
    restore_mode: RESTORE_DEFAULT_ON
  - id: !extend virtual_light
    on_turn_on:
      - mqtt.publish_json:
          topic: "miio/command"
          payload: |-
            root["method"] = "set_properties";
            root["params"][0]["did"] = "${light_group}";
            root["params"][0]["siid"] = 2;
            root["params"][0]["piid"] = 1;
            root["params"][0]["value"] = true;
    on_turn_off:
      - mqtt.publish_json:
          topic: "miio/command"
          payload: |-
            root["method"] = "set_properties";
            root["params"][0]["did"] = "${light_group}";
            root["params"][0]["siid"] = 2;
            root["params"][0]["piid"] = 1;
            root["params"][0]["value"] = false;
字数受限了, sonoffminir4-template.yaml 见一楼


回复

使用道具 举报

4

主题

20

帖子

168

积分

注册会员

Rank: 2

积分
168
金钱
148
HASS币
0
 楼主| 发表于 5 天前 | 显示全部楼层

#sonoffminir4-template.yaml
#ESP home configuration for Sonoff Mini R4
###### Basic info ##########
# GPIO00 Button on the module
# GPIO01 Tx
# GPIO02 Rx
# GPIO19 LED
# GPIO26 Relay output
# GPIO27 S2
# GND S1 
# icon library :https://pictogrammers.com/library/mdi/

esphome:
  name:  ${device_name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:




wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${friendly_name}
    password: ${friendly_name}
  manual_ip:
    static_ip: ${static_ip}
    gateway: 192.168.9.1
    subnet: 255.255.255.0
captive_portal:

web_server:
  port: 80

sensor:
  - platform: wifi_signal
    name: ${device_name} Wifi Signal Strength
    update_interval: 90s
    entity_category: "diagnostic"

  - platform: uptime
    name: ${device_name} Uptime
    update_interval: 300s
    entity_category: "diagnostic"

text_sensor:
  - platform: wifi_info
    ssid:
      name: Connected SSID
    ip_address:
      name: IP Address
    #dns_address:
      #name: DNS Address
    mac_address:
      name: MAC Address

status_led:
  pin:
    number: GPIO19
    inverted: true

output:
  # Physical relay on GPIO
  - platform: gpio
    pin: GPIO26
    id: relay_1
  # virtual relay for different mode
  - platform: gpio
    pin: GPIO25
    id: virtual_relay_1
  - platform: gpio
    pin: GPIO23
    id: virtual_relay_2
  # virtual light for isolate mode
  - platform: gpio
    pin: GPIO14
    id: virtual_relay_3


light:
  - platform: binary
    id: light_1
    name: Light_${Location}
    icon: mdi:ceiling-light-multiple-outline
    restore_mode: RESTORE_DEFAULT_OFF
    output: relay_1

  - platform: binary
    id: virtual_light
    name: virtual_light_${Location}
    icon: mdi:ceiling-light-multiple-outline
    restore_mode: RESTORE_DEFAULT_OFF
    output: virtual_relay_3
    

switch:
  - platform: output
    output: virtual_relay_1
    id: isolate_mode
    icon: mdi:toggle-switch
    name: ${Location}_Isolate_mode_Enable
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: output
    output: virtual_relay_2
    id: toggle_mode
    icon: mdi:toggle-switch
    name: ${Location}_Toggle_mode_Enable
    restore_mode: RESTORE_DEFAULT_ON
  

binary_sensor:

# button on the module
  - platform: gpio
    pin: GPIO00
    id: button
    filters:
      - invert:
      - delayed_off: 50ms
    on_press:
      then:
        - if: 
            condition:
              - switch.is_off: isolate_mode
            then:
              - light.toggle: light_1
            else:
              - light.toggle: virtual_light


# external switch

  - platform: gpio
    name: Switch_in_${Location}
    pin: GPIO27
    id: external_switch
    filters:
      - invert:
      - delayed_off: 50ms
    on_press:
      then:
        - if:
            condition:
              - switch.is_off: isolate_mode
            then:
              - if: 
                  condition: 
                    - switch.is_on: toggle_mode
                  then:
                    - light.toggle: light_1
                  else: 
                    - light.turn_on: light_1
            else:
              - if: 
                  condition: 
                    - switch.is_on: toggle_mode
                  then:
                    - light.toggle: virtual_light
                  else: 
                    - light.turn_on: virtual_light
    on_release:
      then:
        - if:
            condition:
              - switch.is_off: isolate_mode
            then:
              - if: 
                  condition: 
                    - switch.is_on: toggle_mode
                  then:
                    - light.toggle: light_1
                  else: 
                    - light.turn_off: light_1
            else:
              - if: 
                  condition: 
                    - switch.is_on: toggle_mode
                  then:
                    - light.toggle: virtual_light
                  else: 
                    - light.turn_off: virtual_light
回复

使用道具 举报

2

主题

15

帖子

60

积分

注册会员

Rank: 2

积分
60
金钱
45
HASS币
0
发表于 5 天前 | 显示全部楼层

支持一下,加积分
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-21 17:29 , Processed in 0.162116 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表