ferbulous

working 2a yaml

Oct 1st, 2023
396
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.98 KB | None | 1 0
  1. # This is a custom firmware for Human Presence Sensor 2A
  2. # https://docs.screek.io/2a
  3.  
  4. esphome:
  5. name: screek-human-sensor-2a
  6. comment: Screek Human Sensor 2A
  7. friendly_name: Human Sensor 2A
  8. name_add_mac_suffix: True
  9. platformio_options:
  10. board_build.flash_mode: dio
  11. # board_build.f_cpu: 80000000L
  12. project:
  13. name: Screek.Human_Presence_Sensor
  14. version: 2A
  15. on_boot:
  16. - priority: 100
  17. then:
  18. lambda: |-
  19. id(cpu_speed) = ESP.getCpuFreqMHz();
  20. - priority: -200
  21. then:
  22. lambda: |-
  23. id(zone1_target_exsits).publish_state(false);
  24. id(zone2_target_exsits).publish_state(false);
  25. id(zone3_target_exsits).publish_state(false);
  26. id(zone_ex1_target_exsits).publish_state(false);
  27.  
  28. preferences:
  29. flash_write_interval: 5s
  30.  
  31. esp32:
  32. board: esp32dev
  33. framework:
  34. type: arduino
  35.  
  36. globals:
  37. - id: cpu_speed
  38. type: int
  39. restore_value: no
  40. initial_value: '0'
  41. - id: last_update_ld2450
  42. type: unsigned long
  43. restore_value: no
  44. initial_value: '0'
  45. - id: init_zone_publish
  46. type: bool
  47. restore_value: no
  48. initial_value: "false"
  49. - id: last_illuminance
  50. type: float
  51. restore_value: no
  52. initial_value: "-1"
  53.  
  54. improv_serial:
  55.  
  56. logger:
  57.  
  58. debug:
  59. update_interval: 30s
  60.  
  61. api:
  62.  
  63. ota:
  64.  
  65. wifi:
  66. ssid: !secret wifi_ssid
  67. password: !secret wifi_password
  68.  
  69. ap:
  70. ssid: "HUMAN-SENSOR 2A Store"
  71.  
  72. captive_portal:
  73.  
  74. web_server:
  75. port: 80
  76.  
  77.  
  78. text_sensor:
  79. - platform: debug
  80. reset_reason:
  81. name: "ESP Reset Reason"
  82. icon: mdi:anchor
  83. disabled_by_default: True
  84. - platform: wifi_info
  85. ip_address:
  86. name: ESP IP Address
  87. entity_category: "diagnostic"
  88. disabled_by_default: True
  89. icon: mdi:ip-network
  90. mac_address:
  91. name: ESP MAC
  92. entity_category: "diagnostic"
  93. icon: mdi:ip-network
  94. disabled_by_default: True
  95. - platform: template
  96. name: "Zone1 Info"
  97. id: tips_zone1_conf
  98. icon: mdi:information-outline
  99. entity_category: config
  100. lambda: |-
  101. return {"Configure below" };
  102. update_interval: 1000s
  103. - platform: template
  104. name: "Zone2 Info"
  105. id: tips_zone2_conf
  106. icon: mdi:information-outline
  107. entity_category: config
  108. lambda: |-
  109. return {"Configure below" };
  110. update_interval: 1000s
  111. - platform: template
  112. name: "Zone3 Info"
  113. id: tips_zone3_conf
  114. icon: mdi:information-outline
  115. entity_category: config
  116. lambda: |-
  117. return {"Configure below" };
  118. update_interval: 1000s
  119. - platform: template
  120. name: "Zout1 Info"
  121. id: tips_zone_ex1_conf
  122. icon: mdi:information-outline
  123. entity_category: config
  124. lambda: |-
  125. return {"Zone Exclusion 1" };
  126. update_interval: 1000s
  127.  
  128. number:
  129. - platform: template
  130. name: "Any Presence Timeout"
  131. id: any_presence_timeout
  132. min_value: 0
  133. max_value: 600
  134. mode: box
  135. device_class: duration
  136. entity_category: config
  137. unit_of_measurement: s
  138. icon: mdi:timer-off
  139. step: 1
  140. optimistic: True
  141. initial_value: 0
  142. restore_value: True
  143. - platform: template
  144. name: "Zone1 Timeout"
  145. id: zone1_x_timeout
  146. min_value: 0
  147. max_value: 600
  148. mode: box
  149. device_class: duration
  150. entity_category: config
  151. unit_of_measurement: s
  152. icon: mdi:timer-off
  153. step: 1
  154. optimistic: True
  155. initial_value: 0
  156. restore_value: True
  157. - platform: template
  158. name: "Zone2 Timeout"
  159. id: zone2_x_timeout
  160. min_value: 0
  161. max_value: 600
  162. mode: box
  163. device_class: duration
  164. entity_category: config
  165. unit_of_measurement: s
  166. icon: mdi:timer-off
  167. step: 1
  168. optimistic: True
  169. initial_value: 0
  170. restore_value: True
  171. - platform: template
  172. name: "Zone3 Timeout"
  173. id: zone3_x_timeout
  174. min_value: 0
  175. max_value: 600
  176. mode: box
  177. device_class: duration
  178. entity_category: config
  179. unit_of_measurement: s
  180. icon: mdi:timer-off
  181. step: 1
  182. optimistic: True
  183. initial_value: 0
  184. restore_value: True
  185. # Zone 1
  186. - platform: template
  187. name: Zone1 X-Begin
  188. id: zone1_x_begin
  189. min_value: -4000
  190. max_value: 4000
  191. mode: box
  192. device_class: distance
  193. entity_category: config
  194. unit_of_measurement: mm
  195. icon: mdi:arrow-left-bold
  196. step: 10
  197. optimistic: True
  198. initial_value: 0
  199. restore_value: True
  200. on_value:
  201. then:
  202. - script.execute: check_zone1_vaild
  203. - platform: template
  204. name: Zone1 X-End
  205. id: zone1_x_end
  206. mode: box
  207. min_value: -4000
  208. max_value: 4000
  209. device_class: distance
  210. unit_of_measurement: mm
  211. entity_category: config
  212. icon: mdi:arrow-right-bold
  213. step: 10
  214. initial_value: 0
  215. optimistic: True
  216. restore_value: True
  217. on_value:
  218. then:
  219. - script.execute: check_zone1_vaild
  220. - platform: template
  221. name: Zone1 Y-Begin
  222. id: zone1_y_begin
  223. mode: box
  224. min_value: 0
  225. max_value: 6000
  226. device_class: distance
  227. entity_category: config
  228. icon: mdi:arrow-up-bold
  229. unit_of_measurement: mm
  230. step: 10
  231. initial_value: 0
  232. optimistic: True
  233. restore_value: True
  234. on_value:
  235. then:
  236. - script.execute: check_zone1_vaild
  237. - platform: template
  238. name: Zone1 Y-End
  239. id: zone1_y_end
  240. icon: mdi:arrow-down-bold
  241. mode: box
  242. min_value: 0
  243. max_value: 6000
  244. initial_value: 0
  245. entity_category: config
  246. device_class: distance
  247. unit_of_measurement: mm
  248. step: 10
  249. optimistic: True
  250. restore_value: True
  251. on_value:
  252. then:
  253. - script.execute: check_zone1_vaild
  254.  
  255. # Zone 2
  256. - platform: template
  257. name: Zone2 X-Begin
  258. id: zone2_x_begin
  259. min_value: -4000
  260. max_value: 4000
  261. mode: box
  262. device_class: distance
  263. entity_category: config
  264. unit_of_measurement: mm
  265. icon: mdi:arrow-left-bold
  266. step: 10
  267. optimistic: True
  268. initial_value: 0
  269. restore_value: True
  270. on_value:
  271. then:
  272. - script.execute: check_zone2_vaild
  273. - platform: template
  274. name: Zone2 X-End
  275. id: zone2_x_end
  276. mode: box
  277. min_value: -4000
  278. max_value: 4000
  279. device_class: distance
  280. unit_of_measurement: mm
  281. entity_category: config
  282. icon: mdi:arrow-right-bold
  283. step: 10
  284. initial_value: 0
  285. optimistic: True
  286. restore_value: True
  287. on_value:
  288. then:
  289. - script.execute: check_zone2_vaild
  290. - platform: template
  291. name: Zone2 Y-Begin
  292. id: zone2_y_begin
  293. mode: box
  294. min_value: 0
  295. max_value: 6000
  296. device_class: distance
  297. entity_category: config
  298. icon: mdi:arrow-up-bold
  299. unit_of_measurement: mm
  300. step: 10
  301. initial_value: 0
  302. optimistic: True
  303. restore_value: True
  304. on_value:
  305. then:
  306. - script.execute: check_zone2_vaild
  307. - platform: template
  308. name: Zone2 Y-End
  309. id: zone2_y_end
  310. icon: mdi:arrow-down-bold
  311. mode: box
  312. min_value: 0
  313. max_value: 6000
  314. initial_value: 0
  315. entity_category: config
  316. device_class: distance
  317. unit_of_measurement: mm
  318. step: 10
  319. optimistic: True
  320. restore_value: True
  321. on_value:
  322. then:
  323. - script.execute: check_zone2_vaild
  324.  
  325. # Zone 3
  326. - platform: template
  327. name: Zone3 X-Begin
  328. id: zone3_x_begin
  329. min_value: -4000
  330. max_value: 4000
  331. mode: box
  332. device_class: distance
  333. entity_category: config
  334. unit_of_measurement: mm
  335. icon: mdi:arrow-left-bold
  336. step: 10
  337. optimistic: True
  338. initial_value: 0
  339. restore_value: True
  340. on_value:
  341. then:
  342. - script.execute: check_zone3_vaild
  343. - platform: template
  344. name: Zone3 X-End
  345. id: zone3_x_end
  346. mode: box
  347. min_value: -4000
  348. max_value: 4000
  349. device_class: distance
  350. unit_of_measurement: mm
  351. entity_category: config
  352. icon: mdi:arrow-right-bold
  353. step: 10
  354. initial_value: 0
  355. optimistic: True
  356. restore_value: True
  357. on_value:
  358. then:
  359. - script.execute: check_zone3_vaild
  360. - platform: template
  361. name: Zone3 Y-Begin
  362. id: zone3_y_begin
  363. mode: box
  364. min_value: 0
  365. max_value: 6000
  366. device_class: distance
  367. entity_category: config
  368. icon: mdi:arrow-up-bold
  369. unit_of_measurement: mm
  370. step: 10
  371. initial_value: 0
  372. optimistic: True
  373. restore_value: True
  374. on_value:
  375. then:
  376. - script.execute: check_zone3_vaild
  377. - platform: template
  378. name: Zone3 Y-End
  379. id: zone3_y_end
  380. icon: mdi:arrow-down-bold
  381. mode: box
  382. min_value: 0
  383. max_value: 6000
  384. initial_value: 0
  385. entity_category: config
  386. device_class: distance
  387. unit_of_measurement: mm
  388. step: 10
  389. optimistic: True
  390. restore_value: True
  391. on_value:
  392. then:
  393. - script.execute: check_zone3_vaild
  394.  
  395. # Zout1
  396. - platform: template
  397. name: Zout1 X-Begin
  398. id: zone_ex1_x_begin
  399. min_value: -4000
  400. max_value: 4000
  401. mode: box
  402. device_class: distance
  403. entity_category: config
  404. unit_of_measurement: mm
  405. icon: mdi:arrow-left-bold
  406. step: 10
  407. optimistic: True
  408. initial_value: 0
  409. restore_value: True
  410. on_value:
  411. then:
  412. - script.execute: check_zout1_vaild
  413. - platform: template
  414. name: Zout1 X-End
  415. id: zone_ex1_x_end
  416. mode: box
  417. min_value: -4000
  418. max_value: 4000
  419. device_class: distance
  420. unit_of_measurement: mm
  421. entity_category: config
  422. icon: mdi:arrow-right-bold
  423. step: 10
  424. initial_value: 0
  425. optimistic: True
  426. restore_value: True
  427. on_value:
  428. then:
  429. - script.execute: check_zout1_vaild
  430. - platform: template
  431. name: Zout1 Y-Begin
  432. id: zone_ex1_y_begin
  433. mode: box
  434. min_value: 0
  435. max_value: 6000
  436. device_class: distance
  437. entity_category: config
  438. icon: mdi:arrow-up-bold
  439. unit_of_measurement: mm
  440. step: 10
  441. initial_value: 0
  442. optimistic: True
  443. restore_value: True
  444. on_value:
  445. then:
  446. - script.execute: check_zout1_vaild
  447. - platform: template
  448. name: Zout1 Y-End
  449. id: zone_ex1_y_end
  450. icon: mdi:arrow-down-bold
  451. mode: box
  452. min_value: 0
  453. max_value: 6000
  454. initial_value: 0
  455. entity_category: config
  456. device_class: distance
  457. unit_of_measurement: mm
  458. step: 10
  459. optimistic: True
  460. restore_value: True
  461. on_value:
  462. then:
  463. - script.execute: check_zout1_vaild
  464.  
  465. binary_sensor:
  466. - platform: status
  467. name: Online
  468. id: ink_ha_connected
  469. - platform: template
  470. name: "Any Presence"
  471. id: any_target_exsits
  472. device_class: occupancy
  473. filters:
  474. - delayed_off: !lambda |-
  475. return id(any_presence_timeout).state * 1000.0;
  476. - platform: template
  477. name: "Zone1 Presence"
  478. id: zone1_target_exsits
  479. device_class: occupancy
  480. filters:
  481. - delayed_off: !lambda |-
  482. return id(zone1_x_timeout).state * 1000.0;
  483. - platform: template
  484. name: "Zone2 Presence"
  485. id: zone2_target_exsits
  486. device_class: occupancy
  487. filters:
  488. - delayed_off: !lambda |-
  489. return id(zone2_x_timeout).state * 1000.0;
  490. - platform: template
  491. name: "Zone3 Presence"
  492. id: zone3_target_exsits
  493. device_class: occupancy
  494. filters:
  495. - delayed_off: !lambda |-
  496. return id(zone3_x_timeout).state * 1000.0;
  497. - platform: template
  498. name: "Zout1 Presence"
  499. id: zone_ex1_target_exsits
  500. icon: mdi:account-multiple-remove
  501. device_class: occupancy
  502.  
  503. i2c:
  504. sda: GPIO21
  505. scl: GPIO19
  506. scan: true
  507. id: bus_a
  508.  
  509. script:
  510. - id: check_zone1_vaild
  511. then:
  512. - lambda: |-
  513. if (id(zone1_x_begin).state > id(zone1_x_end).state){
  514. id(tips_zone1_conf).publish_state("Err: X-Begin > X-End");
  515. return;
  516. }
  517. if (id(zone1_y_begin).state > id(zone1_y_end).state){
  518. id(tips_zone1_conf).publish_state("Err: Y-Begin > Y-End");
  519. return;
  520. }
  521. if (id(zone1_x_begin).state == 0, id(zone1_x_end).state == 0, id(zone1_y_begin).state == 0, id(zone1_y_end).state == 0){
  522. id(tips_zone1_conf).publish_state("Configure below");
  523. return;
  524. }
  525.  
  526. int x_size = id(zone1_x_end).state - id(zone1_x_begin).state;
  527. int y_size = id(zone1_y_end).state - id(zone1_y_begin).state;
  528.  
  529. char combined[80];
  530. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  531. id(tips_zone1_conf).publish_state(combined);
  532. - id: check_zone2_vaild
  533. then:
  534. - lambda: |-
  535. if (id(zone2_x_begin).state > id(zone2_x_end).state){
  536. id(tips_zone2_conf).publish_state("Err: X-Begin > X-End");
  537. return;
  538. }
  539. if (id(zone2_y_begin).state > id(zone2_y_end).state){
  540. id(tips_zone2_conf).publish_state("Err: Y-Begin > Y-End");
  541. return;
  542. }
  543. if (id(zone2_x_begin).state == 0, id(zone2_x_end).state == 0, id(zone2_y_begin).state == 0, id(zone2_y_end).state == 0){
  544. id(tips_zone2_conf).publish_state("Configure below");
  545. return;
  546. }
  547.  
  548. int x_size = id(zone2_x_end).state - id(zone2_x_begin).state;
  549. int y_size = id(zone2_y_end).state - id(zone2_y_begin).state;
  550.  
  551. char combined[80];
  552. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  553. id(tips_zone2_conf).publish_state(combined);
  554. - id: check_zone3_vaild
  555. then:
  556. - lambda: |-
  557. if (id(zone3_x_begin).state > id(zone3_x_end).state){
  558. id(tips_zone3_conf).publish_state("Err: X-Begin > X-End");
  559. return;
  560. }
  561. if (id(zone3_y_begin).state > id(zone3_y_end).state){
  562. id(tips_zone3_conf).publish_state("Err: Y-Begin > Y-End");
  563. return;
  564. }
  565. if (id(zone3_x_begin).state == 0, id(zone3_x_end).state == 0, id(zone3_y_begin).state == 0, id(zone3_y_end).state == 0){
  566. id(tips_zone3_conf).publish_state("Configure below");
  567. return;
  568. }
  569.  
  570. int x_size = id(zone3_x_end).state - id(zone3_x_begin).state;
  571. int y_size = id(zone3_y_end).state - id(zone3_y_begin).state;
  572.  
  573. char combined[80];
  574. sprintf(combined, "Curr Size: %d x %d", x_size, y_size);
  575. id(tips_zone3_conf).publish_state(combined);
  576. - id: check_zout1_vaild
  577. then:
  578. - lambda: |-
  579. if (id(zone_ex1_x_begin).state > id(zone_ex1_x_end).state){
  580. id(tips_zone_ex1_conf).publish_state("Err: X-Begin > X-End");
  581. return;
  582. }
  583. if (id(zone_ex1_y_begin).state > id(zone_ex1_y_end).state){
  584. id(tips_zone_ex1_conf).publish_state("Err: Y-Begin > Y-End");
  585. return;
  586. }
  587. id(tips_zone_ex1_conf).publish_state("Zone Exclusion 1");
  588.  
  589. sensor:
  590. - platform: template
  591. name: "ESP CPU Speed"
  592. accuracy_decimals: 0
  593. icon: mdi:cpu-32-bit
  594. unit_of_measurement: Mhz
  595. disabled_by_default: True
  596. lambda: |-
  597. return (id(cpu_speed));
  598. entity_category: "diagnostic"
  599. update_interval: 600s
  600. - platform: template
  601. id: sys_esp_temperature
  602. name: ESP Temperature
  603. lambda: return temperatureRead();
  604. unit_of_measurement: °C
  605. device_class: TEMPERATURE
  606. update_interval: 45s
  607. entity_category: "diagnostic"
  608. - platform: uptime
  609. name: ESP Uptime
  610. id: sys_uptime
  611. update_interval: 60s
  612. - platform: wifi_signal
  613. name: RSSI
  614. id: wifi_signal_db
  615. update_interval: 60s
  616. entity_category: "diagnostic"
  617. - platform: template
  618. id: esp_memory
  619. icon: mdi:memory
  620. name: ESP Free Memory
  621. lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
  622. unit_of_measurement: 'kB'
  623. state_class: measurement
  624. entity_category: "diagnostic"
  625. update_interval: 60s
  626. - platform: bh1750
  627. name: "Illuminance"
  628. accuracy_decimals: 1
  629. id: bh1750_light
  630. update_interval: 1s
  631. filters:
  632. - lambda: !lambda |-
  633. if (id(last_illuminance) == x){
  634. return {};
  635. }
  636. if (id(bh1750_fast_update).state){
  637. id(last_illuminance) = x;
  638. // ESP_LOGD("custom", "Fast Update BH1850");
  639. return x;
  640. }
  641. if (abs(id(last_illuminance) - x) > 1){
  642.  
  643. id(last_illuminance) = x;
  644. return x;
  645. }
  646. return {};
  647.  
  648. #-------------------------------------#
  649. # 高级雷达数据
  650. - platform: template
  651. name: "All Target Counts"
  652. id: all_target_count
  653. accuracy_decimals: 0
  654. icon: "mdi:counter"
  655. unit_of_measurement: "targets"
  656. - platform: template
  657. name: "Zone1 Target Counts"
  658. id: zone1_target_count
  659. accuracy_decimals: 0
  660. icon: "mdi:counter"
  661. unit_of_measurement: "targets"
  662. - platform: template
  663. name: "Zone2 Target Counts"
  664. id: zone2_target_count
  665. accuracy_decimals: 0
  666. icon: "mdi:counter"
  667. unit_of_measurement: "targets"
  668. - platform: template
  669. name: "Zone3 Target Counts"
  670. id: zone3_target_count
  671. accuracy_decimals: 0
  672. icon: "mdi:counter"
  673. unit_of_measurement: "targets"
  674. - platform: template
  675. name: "Zout1 Target Counts"
  676. id: zone_ex1_target_count
  677. accuracy_decimals: 0
  678. icon: mdi:account-multiple-minus-outline
  679. unit_of_measurement: "targets"
  680.  
  681. # Target 1
  682. - platform: template
  683. name: "Target1 X"
  684. id: target1_x
  685. accuracy_decimals: 0
  686. unit_of_measurement: 'mm'
  687. state_class: measurement
  688. device_class: distance
  689. - platform: template
  690. name: "Target1 Y"
  691. id: target1_y
  692. accuracy_decimals: 0
  693. unit_of_measurement: 'mm'
  694. state_class: measurement
  695. device_class: distance
  696. - platform: template
  697. name: "Target1 Speed"
  698. id: target1_speed
  699. accuracy_decimals: 2
  700. unit_of_measurement: 'm/s'
  701. state_class: measurement
  702. device_class: speed
  703. - platform: template
  704. name: "Target1 Resolution"
  705. id: target1_resolution
  706. accuracy_decimals: 0
  707. unit_of_measurement: 'mm'
  708. state_class: measurement
  709. device_class: distance
  710.  
  711. # Target 2
  712. - platform: template
  713. name: "Target2 X"
  714. id: target2_x
  715. accuracy_decimals: 0
  716. unit_of_measurement: 'mm'
  717. state_class: measurement
  718. device_class: distance
  719. - platform: template
  720. name: "Target2 Y"
  721. id: target2_y
  722. accuracy_decimals: 0
  723. unit_of_measurement: 'mm'
  724. state_class: measurement
  725. device_class: distance
  726. - platform: template
  727. name: "Target2 Speed"
  728. id: target2_speed
  729. accuracy_decimals: 0
  730. unit_of_measurement: 'm/s'
  731. state_class: measurement
  732. device_class: speed
  733. - platform: template
  734. name: "Target2 Resolution"
  735. id: target2_resolution
  736. accuracy_decimals: 0
  737. unit_of_measurement: 'mm'
  738. state_class: measurement
  739. device_class: distance
  740.  
  741. # Target 3
  742. - platform: template
  743. name: "Target3 X"
  744. id: target3_x
  745. accuracy_decimals: 0
  746. unit_of_measurement: 'mm'
  747. state_class: measurement
  748. device_class: distance
  749. - platform: template
  750. name: "Target3 Y"
  751. id: target3_y
  752. accuracy_decimals: 0
  753. unit_of_measurement: 'mm'
  754. state_class: measurement
  755. device_class: distance
  756. - platform: template
  757. name: "Target3 Speed"
  758. id: target3_speed
  759. accuracy_decimals: 0
  760. unit_of_measurement: 'm/s'
  761. state_class: measurement
  762. device_class: speed
  763. - platform: template
  764. name: "Target3 Resolution"
  765. id: target3_resolution
  766. accuracy_decimals: 0
  767. unit_of_measurement: 'mm'
  768. state_class: measurement
  769. device_class: distance
  770.  
  771.  
  772. light:
  773. - platform: status_led
  774. name: sys_status
  775. pin: GPIO13
  776. internal: True
  777. restore_mode: ALWAYS_OFF
  778. - platform: binary
  779. name: "Red Info Light"
  780. output: board_info_ed
  781. entity_category: "config"
  782. restore_mode: ALWAYS_OFF
  783.  
  784. time:
  785. - platform: sntp
  786. id: time_now
  787.  
  788. output:
  789. - platform: gpio
  790. id: board_info_ed
  791. pin: GPIO12
  792.  
  793.  
  794. switch:
  795. - platform: factory_reset
  796. name: Factory Reset
  797. disabled_by_default: True
  798. icon: mdi:heart-broken
  799. - platform: template
  800. name: Zout1 Enable
  801. id: zone_ex1_enable
  802. optimistic: True
  803. icon: mdi:account-cancel
  804. entity_category: config
  805. restore_mode: RESTORE_DEFAULT_OFF
  806. - platform: template
  807. name: Illuminance Fast-Update
  808. id: bh1750_fast_update
  809. optimistic: True
  810. entity_category: diagnostic
  811. restore_mode: RESTORE_DEFAULT_OFF
  812. icon: mdi:run-fast
  813.  
  814. button:
  815. - platform: restart
  816. icon: mdi:power-cycle
  817. name: "ESP Reboot"
  818. entity_category: diagnostic
  819.  
  820. uart:
  821. id: uart_bus
  822. tx_pin:
  823. number: GPIO5
  824. mode:
  825. input: true
  826. pullup: true
  827. rx_pin:
  828. number: GPIO4
  829. mode:
  830. input: true
  831. pullup: true
  832. baud_rate: 256000
  833. parity: NONE
  834. stop_bits: 1
  835. data_bits: 8
  836. debug:
  837. direction: BOTH
  838. dummy_receiver: True
  839. after:
  840. delimiter: [0X55, 0XCC]
  841. sequence:
  842. - lambda: |-
  843. if ((millis() - id(last_update_ld2450)) <= 500) {
  844. return;
  845. };
  846. id(last_update_ld2450) = millis();
  847.  
  848. // p1
  849. int16_t p1_x = (uint16_t((bytes[5] << 8) | bytes[4] ));
  850. if ((bytes[5] & 0x80) >> 7){
  851. p1_x -= pow(2, 15);
  852. }else{
  853. p1_x = 0 - p1_x;
  854. }
  855.  
  856. int16_t p1_y = (uint16_t((bytes[7] << 8) | bytes[6] ));
  857. if ((bytes[7] & 0x80) >> 7){
  858. p1_y -= pow(2, 15);
  859. }else{
  860. p1_y = 0 - p1_y;
  861. }
  862.  
  863. int p1_speed = (bytes[9] << 8 | bytes[8] );
  864. if ((bytes[9] & 0x80) >> 7){
  865. p1_speed -= pow(2, 15);
  866. }else{
  867. p1_speed = 0 - p1_speed;
  868. }
  869. int16_t p1_distance_resolution = (uint16_t((bytes[11] << 8) | bytes[10] ));
  870.  
  871. // p2
  872. int16_t p2_x = (uint16_t((bytes[13] << 8) | bytes[12] ));
  873. if ((bytes[13] & 0x80) >> 7){
  874. p2_x -= pow(2, 15);
  875. }else{
  876. p2_x = 0 - p2_x;
  877. }
  878.  
  879. int16_t p2_y = (uint16_t((bytes[15] << 8) | bytes[14] ));
  880. if ((bytes[15] & 0x80) >> 7){
  881. p2_y -= pow(2, 15);
  882. }else{
  883. p2_y = 0 - p2_y;
  884. }
  885.  
  886. int p2_speed = (bytes[17] << 8 | bytes[16] );
  887. if ((bytes[17] & 0x80) >> 7){
  888. p2_speed -= pow(2, 15);
  889. }else{
  890. p2_speed = 0 - p2_speed;
  891. }
  892. int16_t p2_distance_resolution = (uint16_t((bytes[19] << 8) | bytes[18] ));
  893.  
  894. // p3
  895. int16_t p3_x = (uint16_t((bytes[21] << 8) | bytes[20] ));
  896. if ((bytes[21] & 0x80) >> 7){
  897. p3_x -= pow(2, 15);
  898. }else{
  899. p3_x = 0 - p3_x;
  900. }
  901.  
  902. int16_t p3_y = (uint16_t((bytes[23] << 8) | bytes[22] ));
  903. if ((bytes[23] & 0x80) >> 7){
  904. p3_y -= pow(2, 15);
  905. }else{
  906. p3_y = 0 - p3_y;
  907. }
  908.  
  909. int p3_speed = (bytes[25] << 8 | bytes[24] );
  910. if ((bytes[25] & 0x80) >> 7){
  911. p3_speed -= pow(2, 15);
  912. }else{
  913. p3_speed = 0 - p3_speed;
  914. }
  915.  
  916. int16_t p3_distance_resolution = (uint16_t((bytes[27] << 8) | bytes[26] ));
  917.  
  918. bool p1_vaild = (p1_x != 0 || p1_y > 0);
  919. bool p2_vaild = (p2_x != 0 || p2_y > 0);
  920. bool p3_vaild = (p3_x != 0 || p3_y > 0);
  921.  
  922. // zone exlude 1
  923.  
  924. int16_t target_count_in_zone_ex1 = 0;
  925.  
  926. int16_t zone_ex1_x_min = id(zone_ex1_x_begin).state;
  927. int16_t zone_ex1_x_max = id(zone_ex1_x_end).state;
  928. int16_t zone_ex1_y_min = id(zone_ex1_y_begin).state;
  929. int16_t zone_ex1_y_max = id(zone_ex1_y_end).state;
  930.  
  931. bool p1_zone_ex_enter = false;
  932. bool p2_zone_ex_enter = false;
  933. bool p3_zone_ex_enter = false;
  934.  
  935. if (id(zone_ex1_enable).state){
  936. if (p1_vaild){
  937. if (p1_x >= zone_ex1_x_min && p1_x <= zone_ex1_x_max && p1_y >= zone_ex1_y_min && p1_y <= zone_ex1_y_max){
  938. p1_zone_ex_enter = true;
  939. target_count_in_zone_ex1 ++;
  940. }
  941. }
  942. if (p2_vaild){
  943. if (p2_x >= zone_ex1_x_min && p2_x <= zone_ex1_x_max && p2_y >= zone_ex1_y_min && p2_y <= zone_ex1_y_max){
  944. p2_zone_ex_enter = true;
  945. target_count_in_zone_ex1 ++;
  946. }
  947. }
  948. if (p3_vaild){
  949. if (p3_x >= zone_ex1_x_min && p3_x <= zone_ex1_x_max && p3_y >= zone_ex1_y_min && p3_y <= zone_ex1_y_max){
  950. p3_zone_ex_enter = true;
  951. target_count_in_zone_ex1 ++;
  952. }
  953. }
  954. }
  955.  
  956. bool has_target_in_zone_ex1 = (target_count_in_zone_ex1 > 0);
  957.  
  958. int16_t all_target_counts = 0;
  959. if (p1_vaild && !p1_zone_ex_enter){
  960. all_target_counts ++;
  961. }
  962. if (p2_vaild && !p2_zone_ex_enter){
  963. all_target_counts ++;
  964. }
  965. if (p3_vaild && !p3_zone_ex_enter){
  966. all_target_counts ++;
  967. }
  968.  
  969. bool has_target_in_zone_all = (all_target_counts > 0);
  970.  
  971. // zone 1 check
  972.  
  973. int16_t target_count_in_zone1 = 0;
  974. int16_t zone1_x_min = id(zone1_x_begin).state;
  975. int16_t zone1_x_max = id(zone1_x_end).state;
  976. int16_t zone1_y_min = id(zone1_y_begin).state;
  977. int16_t zone1_y_max = id(zone1_y_end).state;
  978.  
  979. if (p1_vaild && !p1_zone_ex_enter){
  980. if (p1_x >= zone1_x_min && p1_x <= zone1_x_max && p1_y >= zone1_y_min && p1_y <= zone1_y_max){
  981. target_count_in_zone1 ++;
  982. }
  983. }
  984. if (p2_vaild && !p2_zone_ex_enter){
  985. if (p2_x >= zone1_x_min && p2_x <= zone1_x_max && p2_y >= zone1_y_min && p2_y <= zone1_y_max){
  986. target_count_in_zone1 ++;
  987. }
  988. }
  989. if (p3_vaild && !p3_zone_ex_enter){
  990. if (p3_x >= zone1_x_min && p3_x <= zone1_x_max && p3_y >= zone1_y_min && p3_y <= zone1_y_max){
  991. target_count_in_zone1 ++;
  992. }
  993. }
  994. bool has_target_in_zone1 = (target_count_in_zone1 > 0);
  995.  
  996. // zone 2 check
  997.  
  998. int16_t target_count_in_zone2 = 0;
  999. int16_t zone2_x_min = id(zone2_x_begin).state;
  1000. int16_t zone2_x_max = id(zone2_x_end).state;
  1001. int16_t zone2_y_min = id(zone2_y_begin).state;
  1002. int16_t zone2_y_max = id(zone2_y_end).state;
  1003.  
  1004. if (p1_vaild && !p1_zone_ex_enter){
  1005. if (p1_x >= zone2_x_min && p1_x <= zone2_x_max && p1_y >= zone2_y_min && p1_y <= zone2_y_max){
  1006. target_count_in_zone2 ++;
  1007. }
  1008. }
  1009. if (p2_vaild && !p2_zone_ex_enter){
  1010. if (p2_x >= zone2_x_min && p2_x <= zone2_x_max && p2_y >= zone2_y_min && p2_y <= zone2_y_max){
  1011. target_count_in_zone2 ++;
  1012. }
  1013. }
  1014. if (p3_vaild && !p3_zone_ex_enter){
  1015. if (p3_x >= zone2_x_min && p3_x <= zone2_x_max && p3_y >= zone2_y_min && p3_y <= zone2_y_max){
  1016. target_count_in_zone2 ++;
  1017. }
  1018. }
  1019.  
  1020. bool has_target_in_zone2 = (target_count_in_zone2 > 0);
  1021.  
  1022. // zone 3 check
  1023.  
  1024. int16_t target_count_in_zone3 = 0;
  1025. int16_t zone3_x_min = id(zone3_x_begin).state;
  1026. int16_t zone3_x_max = id(zone3_x_end).state;
  1027. int16_t zone3_y_min = id(zone3_y_begin).state;
  1028. int16_t zone3_y_max = id(zone3_y_end).state;
  1029.  
  1030. if (p1_vaild && !p1_zone_ex_enter){
  1031. if (p1_x >= zone3_x_min && p1_x <= zone3_x_max && p1_y >= zone3_y_min && p1_y <= zone3_y_max){
  1032. target_count_in_zone3 ++;
  1033. }
  1034. }
  1035. if (p2_vaild && !p2_zone_ex_enter){
  1036. if (p2_x >= zone3_x_min && p2_x <= zone3_x_max && p2_y >= zone3_y_min && p2_y <= zone3_y_max){
  1037. target_count_in_zone3 ++;
  1038. }
  1039. }
  1040. if (p3_vaild && !p3_zone_ex_enter){
  1041. if (p3_x >= zone3_x_min && p3_x <= zone3_x_max && p3_y >= zone3_y_min && p3_y <= zone3_y_max){
  1042. target_count_in_zone3 ++;
  1043. }
  1044. }
  1045. bool has_target_in_zone3 = (target_count_in_zone3 > 0);
  1046.  
  1047. // public all info
  1048. if (id(target1_x).state != p1_x){
  1049. id(target1_x).publish_state(p1_x);
  1050. }
  1051. if (id(target1_y).state != p1_y){
  1052. id(target1_y).publish_state(p1_y);
  1053. }
  1054.  
  1055. float p1_m_speed = float(p1_speed) / 100.0;
  1056. if (id(target1_speed).state != p1_m_speed){
  1057. id(target1_speed).publish_state(p1_m_speed);
  1058. }
  1059. if (id(target1_resolution).state != p1_distance_resolution){
  1060. id(target1_resolution).publish_state(p1_distance_resolution);
  1061. }
  1062.  
  1063. if (id(target2_x).state != p2_x){
  1064. id(target2_x).publish_state(p2_x);
  1065. }
  1066. if (id(target2_y).state != p2_y){
  1067. id(target2_y).publish_state(p2_y);
  1068. }
  1069. if (id(target2_speed).state != p2_speed){
  1070. id(target2_speed).publish_state(p2_speed);
  1071. }
  1072. if (id(target2_resolution).state != p2_distance_resolution){
  1073. id(target2_resolution).publish_state(p2_distance_resolution);
  1074. }
  1075.  
  1076. if (id(target3_x).state != p3_x){
  1077. id(target3_x).publish_state(p3_x);
  1078. }
  1079. if (id(target3_y).state != p3_y){
  1080. id(target3_y).publish_state(p3_y);
  1081. }
  1082. if (id(target3_speed).state != p3_speed){
  1083. id(target3_speed).publish_state(p3_speed);
  1084. }
  1085. if (id(target3_resolution).state != p3_distance_resolution){
  1086. id(target3_resolution).publish_state(p3_distance_resolution);
  1087. }
  1088.  
  1089. // publish target info
  1090.  
  1091. if (id(all_target_count).state != all_target_counts){
  1092. id(all_target_count).publish_state(all_target_counts);
  1093. id(any_target_exsits).publish_state(has_target_in_zone_all);
  1094. }
  1095.  
  1096. if (id(zone1_target_count).state != target_count_in_zone1){
  1097. id(zone1_target_count).publish_state(target_count_in_zone1);
  1098. id(zone1_target_exsits).publish_state(has_target_in_zone1);
  1099. }
  1100.  
  1101. if (id(zone2_target_count).state != target_count_in_zone2){
  1102. id(zone2_target_count).publish_state(target_count_in_zone2);
  1103. id(zone2_target_exsits).publish_state(has_target_in_zone2);
  1104. }
  1105.  
  1106. if (id(zone3_target_count).state != target_count_in_zone3){
  1107. id(zone3_target_count).publish_state(target_count_in_zone3);
  1108. id(zone3_target_exsits).publish_state(has_target_in_zone3);
  1109. }
  1110.  
  1111. // zout
  1112. if (id(zone_ex1_target_count).state != target_count_in_zone_ex1){
  1113. id(zone_ex1_target_count).publish_state(target_count_in_zone_ex1);
  1114. id(zone_ex1_target_exsits).publish_state(has_target_in_zone_ex1);
  1115. }
  1116.  
  1117. if (!id(init_zone_publish)){
  1118. id(init_zone_publish) = true;
  1119. }
Advertisement
Add Comment
Please, Sign In to add comment