本帖最后由 ciasdmxhxjjpd@c 于 2022-7-21 17:50 编辑
RT,
# BLE
esp32_ble_tracker:
on_ble_advertise:
- mac_address: A4:C1:38:6E:85:0B
then:
- lambda: |-
for (auto data : x.get_service_datas()) {
ESP_LOGD("ble_adv", " - %s: (length %i) - %s", data.uuid.to_string().c_str(), data.data.size(),hexencode(data.data).c_str());
}
sensor:
- platform: template
name: "yeelight remote controller"
id: ble_sensor
ble 获取到指定mac地址的广播数据格式
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.9B.0B.85.6E.38.C1.A4.01.10.03.00.00.00 (17)
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.9C.0B.85.6E.38.C1.A4.01.10.03.01.00.00 (17)
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.9E.0B.85.6E.38.C1.A4.01.10.03.02.00.00 (17)
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.A0.0B.85.6E.38.C1.A4.01.10.03.04.00.00 (17)
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.A1.0B.85.6E.38.C1.A4.01.10.03.03.00.00 (17)
[D][ble_adv:099]: - 0xFE95: (length 17) - 50.30.8E.06.A1.0B.85.6E.38.C1.A4.01.10.03.03.00.00 (17)
我在github里,写了数据格式含义。https://github.com/0neday/yeelig ... controller-adv-data
请问,在 lambda 里如何接着检查 获取数据的倒数第三个字节,并通过 if else 比较选择,再执行相应的动作。
比如当获取到开灯的数据,50.30.8E.06.89.0B.85.6E.38.C1.A4.01.10.03.01.00.00,截取倒数第三个字节01,if esle选择下执行体,执行 或者 调用 写好的 script 脚本?
- light.turn_on: light_id
请大佬赐教!
----------------------------已解决
根据 esphome ble 官方文档,on_ble_service_data_advertise 中 的 x 是一个向量,即数组数据,从下标 0 开始,往后数第14 个字节就是我们要找的key code 特征识别字符。同时定义一个全局变量存储数据帧number,防止点一次发多个数据包。frame number = x[4]
globals:
- id: frame_counter
type: int
initial_value: '0'
# BLE
esp32_ble_tracker:
on_ble_service_data_advertise:
- mac_address: A4:C1:38:6E:85:0B
service_uuid: FE95
then:
# 吊顶灯切换
- if:
condition:
lambda: "return(x[14] == 1 && id(frame_counter) != x[4]);"
then:
- lambda: |-
id(frame_counter) = x[4];
id(yeelight_remote_controller).publish_state(x[14]);
- switch.turn_on: buzzer_beep
- light.toggle: ceiling_light
|