forked from dgrnbrg/appdaemon-configs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ld2410ble.yaml
256 lines (245 loc) · 8.64 KB
/
ld2410ble.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
substitutions:
ld2410_id: 'ld2410_id'
ld2410_name: ""
ld2410_password: "HiLink"
sensor_throttle: '500ms'
binary_sensor_debounce: '250ms'
esphome:
on_boot:
priority: -100
then:
- switch.turn_on: "${ld2410_id}_enable_switch"
# This will cause the startup script to run
- binary_sensor.template.publish:
id: "${ld2410_id}_ble_connected"
state: OFF
esp32:
framework:
sdkconfig_options:
CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
script:
- id: ${ld2410_id}_start_after_delay
then:
# random 1-15s delay
- delay: !lambda 'int d = (rand() % 14000) + 1000; ESP_LOGD("${ld2410_id}", "starting after %d millis", d); return d;'
- lambda: |-
id(${ld2410_id})->set_enabled(true);
ble_client:
- mac_address: ${mac_address}
id: ${ld2410_id}
on_disconnect:
then:
- binary_sensor.template.publish:
id: "${ld2410_id}_ble_connected"
state: OFF
- logger.log:
format: "disconnected"
tag: "${ld2410_id}_connect"
- delay: 100ms
- lambda: |-
id(${ld2410_id}_motion_detected).publish_state(false);
id(${ld2410_id}_occupancy_detected).publish_state(false);
id(${ld2410_id}_static_distance).publish_state(NAN);
id(${ld2410_id}_motion_distance).publish_state(NAN);
id(${ld2410_id}_static_energy).publish_state(NAN);
id(${ld2410_id}_moving_energy).publish_state(NAN);
id(${ld2410_id}_detection_distance).publish_state(NAN);
on_connect:
then:
- logger.log:
format: "connecting"
tag: "${ld2410_id}_connect"
- delay: 100ms
- ble_client.ble_write:
id: ${ld2410_id}
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff2-0000-1000-8000-00805f9b34fb
value: !lambda |-
// data starts with preamble
std::vector<uint8_t> data = { 0xfd, 0xfc, 0xfb, 0xfa, 0x08, 0x00, 0xa8, 0x00 };
// postamble
std::vector<uint8_t> postamble = { 0x04, 0x03, 0x02, 0x01 };
std::string pw = {"${ld2410_password}"};
ESP_LOGD("2410_ble_auth", "password for ${mac_address} = %s", pw.c_str());
for (auto& c : pw) {
data.push_back(c);
}
data.insert(data.end(), postamble.begin(), postamble.end());
return data;
- delay: 100ms
- ble_client.ble_write:
id: ${ld2410_id}
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff2-0000-1000-8000-00805f9b34fb
# enable config
value: [0xfd, 0xfc, 0xfb, 0xfa, 0x04, 0x00, 0xff, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
- delay: 100ms
- ble_client.ble_write:
id: ${ld2410_id}
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff2-0000-1000-8000-00805f9b34fb
# enable engineering mode
value: [0xfd, 0xfc, 0xfb, 0xfa, 0x02, 0x00b, 0x00, 0x04, 0x03, 0x02, 0x01]
- delay: 100ms
- ble_client.ble_write:
id: ${ld2410_id}
service_uuid: 0000fff0-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000fff2-0000-1000-8000-00805f9b34fb
# diable config
value: [0xfd, 0xfc, 0xfb, 0xfa, 0x02, 0x00, 0xfe, 0x00, 0x04, 0x03, 0x02, 0x01]
- delay: 100ms
- logger.log:
format: "completed ble connection process, waiting for updates..."
tag: "${ld2410_id}_connect"
# automatically try to toggle if it didn't connect
- delay: 10s # we give it 10 seconds to connect
- if:
condition:
binary_sensor.is_off: ${ld2410_id}_ble_connected
then: # we need to toggle it
- logger.log:
format: "Failed to connect to push updates, retrying..."
tag: "${ld2410_id}_connect"
- switch.turn_off: "${ld2410_id}_enable_switch_internal"
- script.execute: ${ld2410_id}_start_after_delay
switch:
- platform: template
name: "${ld2410_name}Enabled"
id: "${ld2410_id}_enable_switch"
entity_category: diagnostic
optimistic: True
turn_on_action:
switch.turn_on: "${ld2410_id}_enable_switch_internal"
turn_off_action:
switch.turn_off: "${ld2410_id}_enable_switch_internal"
- platform: template
id: "${ld2410_id}_enable_switch_internal"
internal: True
turn_on_action:
lambda: |-
ESP_LOGD("${ld2410_id}", "Enabling ble client");
id(${ld2410_id})->set_enabled(true);
turn_off_action:
lambda: |-
ESP_LOGD("${ld2410_id}", "Disabling ble client");
id(${ld2410_id})->set_enabled(false);
lambda: |-
return id(${ld2410_id})->enabled;
binary_sensor:
- platform: template
name: "${ld2410_name}Motion Detected"
id: ${ld2410_id}_motion_detected
device_class: motion
filters:
- delayed_on_off: ${binary_sensor_debounce}
- platform: template
name: "${ld2410_name}Occupancy Detected"
id: ${ld2410_id}_occupancy_detected
device_class: occupancy
filters:
- delayed_on_off: ${binary_sensor_debounce}
- platform: template
name: "${ld2410_name}LD2410 Connected"
id: ${ld2410_id}_ble_connected
on_release:
then:
- if:
condition:
switch.is_on: ${ld2410_id}_enable_switch
then: # we need to toggle it
- logger.log:
format: "Connection lost, reconnecting..."
tag: "${ld2410_id}"
- switch.turn_off: "${ld2410_id}_enable_switch_internal"
- script.execute: ${ld2410_id}_start_after_delay
sensor:
- platform: template
name: "${ld2410_name}Motion Distance"
id: ${ld2410_id}_motion_distance
update_interval: never
device_class: distance
unit_of_measurement: "cm"
filters:
- throttle: ${sensor_throttle}
- platform: template
name: "${ld2410_name}Static Distance"
id: ${ld2410_id}_static_distance
update_interval: never
device_class: distance
unit_of_measurement: "cm"
filters:
- throttle: ${sensor_throttle}
- platform: template
name: "${ld2410_name}Static Energy"
id: ${ld2410_id}_static_energy
update_interval: never
unit_of_measurement: "%"
filters:
- throttle: ${sensor_throttle}
- platform: template
name: "${ld2410_name}Moving Energy"
id: ${ld2410_id}_moving_energy
update_interval: never
unit_of_measurement: "%"
filters:
- throttle: ${sensor_throttle}
- platform: template
name: "${ld2410_name}Detection Distance"
id: ${ld2410_id}_detection_distance
update_interval: never
device_class: distance
unit_of_measurement: "cm"
filters:
- throttle: ${sensor_throttle}
- platform: ble_client
type: characteristic
ble_client_id: ${ld2410_id}
service_uuid: '0000fff0-0000-1000-8000-00805f9b34fb'
characteristic_uuid: '0000fff1-0000-1000-8000-00805f9b34fb'
update_interval: never
notify: True
internal: True
filters:
- throttle: ${sensor_throttle}
id: ${ld2410_id}_internal_parser
lambda: |-
// Only report connected once we have seen the first update
if (!${ld2410_id}_ble_connected->state) {
id(${ld2410_id}_ble_connected).publish_state(true);
}
if (x.size() < 16) {
ESP_LOGD("${ld2410_id}_notify", "notify was given too little data (%d bytes)", x.size());
return NAN;
}
if (x[0] != 0xf4 || x[1] != 0xf3 || x[2] != 0xf2 || x[3] != 0xf1) {
return NAN;
}
bool moving = x[8] & 0x1;
bool stationary = x[8] & 0x2;
float detect_distance = x[15];
detect_distance += x[16] << 8;
float static_distance = x[12];
static_distance += x[13] << 8;
float motion_distance = x[9];
motion_distance += x[10] << 8;
float static_energy = x[14];
float motion_energy = x[11];
// if (!stationary) {
// static_distance = NAN;
// static_energy = NAN;
// }
// if (!moving) {
// motion_distance = NAN;
// motion_energy = NAN;
// }
if (!moving && !stationary) {
detect_distance = NAN;
}
id(${ld2410_id}_motion_detected).publish_state(moving);
id(${ld2410_id}_occupancy_detected).publish_state(stationary);
id(${ld2410_id}_static_distance).publish_state(static_distance);
id(${ld2410_id}_motion_distance).publish_state(motion_distance);
id(${ld2410_id}_static_energy).publish_state(motion_energy);
id(${ld2410_id}_moving_energy).publish_state(static_energy);
id(${ld2410_id}_detection_distance).publish_state(detect_distance);
return NAN;