From 3df23400dc80ebe569dbec5c2f1034c75b71ac3c Mon Sep 17 00:00:00 2001 From: sen Date: Sat, 2 Sep 2023 12:18:42 +0800 Subject: [PATCH] Tweak code to include region timeout support for athua. --- 2a/yaml/beta/human-sensor-2a-beta-github.yaml | 295 +++++++++++++++--- 1 file changed, 255 insertions(+), 40 deletions(-) diff --git a/2a/yaml/beta/human-sensor-2a-beta-github.yaml b/2a/yaml/beta/human-sensor-2a-beta-github.yaml index c46b4d2..d7a3e40 100644 --- a/2a/yaml/beta/human-sensor-2a-beta-github.yaml +++ b/2a/yaml/beta/human-sensor-2a-beta-github.yaml @@ -14,7 +14,7 @@ esphome: name: Screek.Human_Presence_Sensor version: 2A on_boot: - - priority: 900 + - priority: 100 then: lambda: |- id(cpu_speed) = ESP.getCpuFreqMHz(); @@ -33,7 +33,7 @@ external_components: - source: type: git url: https://github.com/screekworkshop/custom_components_support_for_screek_2a - ref: main + ref: 2023.8 components: [esp32, uart] esp32: @@ -68,7 +68,27 @@ logger: debug: update_interval: 30s +api: + +ota: + # use your own ota password plz. this is a words by Socrates. + password: "all-things-in-their-being-are-good-for-something" + safe_mode: False + +wifi: + power_save_mode: LIGHT + reboot_timeout: 10min + ap: + ssid: "HUMAN-SENSOR 2A" + +captive_portal: + +web_server: + port: 80 + + text_sensor: +# 公共代码部分(23年9月2日_11时53分_) - platform: debug reset_reason: name: "ESP Reset Reason" @@ -118,25 +138,50 @@ text_sensor: return {"Zone Exclusion 1" }; update_interval: 1000s -api: - -ota: - # use your own ota password plz. this is a words by Socrates. - password: "all-things-in-their-being-are-good-for-something" - safe_mode: False - -wifi: - power_save_mode: LIGHT - reboot_timeout: 10min - ap: - ssid: "HUMAN-SENSOR 2A" - -captive_portal: - -web_server: - port: 80 - number: + - platform: template + name: "Zone1 Timeout" + id: zone1_x_timeout + min_value: 0 + max_value: 600 + mode: box + device_class: duration + entity_category: config + unit_of_measurement: s + icon: mdi:timer-off + step: 1 + optimistic: True + initial_value: 0 + restore_value: True + - platform: template + name: "Zone2 Timeout" + id: zone2_x_timeout + min_value: 0 + max_value: 600 + mode: box + device_class: duration + entity_category: config + unit_of_measurement: s + icon: mdi:timer-off + step: 1 + optimistic: True + initial_value: 0 + restore_value: True + - platform: template + name: "Zone3 Timeout" + id: zone3_x_timeout + min_value: 0 + max_value: 600 + mode: box + device_class: duration + entity_category: config + unit_of_measurement: s + icon: mdi:timer-off + step: 1 + optimistic: True + initial_value: 0 + restore_value: True + # Zone 1 - platform: template name: Zone1 X-Begin id: zone1_x_begin @@ -151,6 +196,9 @@ number: optimistic: True initial_value: 0 restore_value: True + on_value: + then: + - script.execute: check_zone1_vaild - platform: template name: Zone1 X-End id: zone1_x_end @@ -165,6 +213,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone1_vaild - platform: template name: Zone1 Y-Begin id: zone1_y_begin @@ -179,6 +230,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone1_vaild - platform: template name: Zone1 Y-End id: zone1_y_end @@ -193,6 +247,11 @@ number: step: 10 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone1_vaild + + # Zone 2 - platform: template name: Zone2 X-Begin id: zone2_x_begin @@ -207,6 +266,9 @@ number: optimistic: True initial_value: 0 restore_value: True + on_value: + then: + - script.execute: check_zone2_vaild - platform: template name: Zone2 X-End id: zone2_x_end @@ -221,6 +283,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone2_vaild - platform: template name: Zone2 Y-Begin id: zone2_y_begin @@ -235,6 +300,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone2_vaild - platform: template name: Zone2 Y-End id: zone2_y_end @@ -249,6 +317,11 @@ number: step: 10 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone2_vaild + + # Zone 3 - platform: template name: Zone3 X-Begin id: zone3_x_begin @@ -263,6 +336,9 @@ number: optimistic: True initial_value: 0 restore_value: True + on_value: + then: + - script.execute: check_zone3_vaild - platform: template name: Zone3 X-End id: zone3_x_end @@ -277,6 +353,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone3_vaild - platform: template name: Zone3 Y-Begin id: zone3_y_begin @@ -291,6 +370,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone3_vaild - platform: template name: Zone3 Y-End id: zone3_y_end @@ -305,6 +387,11 @@ number: step: 10 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zone3_vaild + + # Zout1 - platform: template name: Zout1 X-Begin id: zone_ex1_x_begin @@ -319,6 +406,9 @@ number: optimistic: True initial_value: 0 restore_value: True + on_value: + then: + - script.execute: check_zout1_vaild - platform: template name: Zout1 X-End id: zone_ex1_x_end @@ -333,6 +423,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zout1_vaild - platform: template name: Zout1 Y-Begin id: zone_ex1_y_begin @@ -347,6 +440,9 @@ number: initial_value: 0 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zout1_vaild - platform: template name: Zout1 Y-End id: zone_ex1_y_end @@ -361,6 +457,9 @@ number: step: 10 optimistic: True restore_value: True + on_value: + then: + - script.execute: check_zout1_vaild binary_sensor: - platform: status @@ -374,14 +473,23 @@ binary_sensor: name: "Zone1 Presence" id: zone1_target_exsits device_class: occupancy + filters: + - delayed_off: !lambda |- + return id(zone1_x_timeout).state * 1000.0; - platform: template name: "Zone2 Presence" id: zone2_target_exsits device_class: occupancy + filters: + - delayed_off: !lambda |- + return id(zone2_x_timeout).state * 1000.0; - platform: template name: "Zone3 Presence" id: zone3_target_exsits device_class: occupancy + filters: + - delayed_off: !lambda |- + return id(zone3_x_timeout).state * 1000.0; - platform: template name: "Zout1 Presence" id: zone_ex1_target_exsits @@ -394,6 +502,86 @@ i2c: scan: true id: bus_a +script: + - id: check_zone1_vaild + then: + - lambda: |- + if (id(zone1_x_begin).state > id(zone1_x_end).state){ + id(tips_zone1_conf).publish_state("Err: X-Begin > X-End"); + return; + } + if (id(zone1_y_begin).state > id(zone1_y_end).state){ + id(tips_zone1_conf).publish_state("Err: Y-Begin > Y-End"); + return; + } + 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){ + id(tips_zone1_conf).publish_state("Configure below"); + return; + } + + int x_size = id(zone1_x_end).state - id(zone1_x_begin).state; + int y_size = id(zone1_y_end).state - id(zone1_y_begin).state; + + char combined[80]; + sprintf(combined, "Curr Size: %d x %d", x_size, y_size); + id(tips_zone1_conf).publish_state(combined); + - id: check_zone2_vaild + then: + - lambda: |- + if (id(zone2_x_begin).state > id(zone2_x_end).state){ + id(tips_zone2_conf).publish_state("Err: X-Begin > X-End"); + return; + } + if (id(zone2_y_begin).state > id(zone2_y_end).state){ + id(tips_zone2_conf).publish_state("Err: Y-Begin > Y-End"); + return; + } + 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){ + id(tips_zone2_conf).publish_state("Configure below"); + return; + } + + int x_size = id(zone2_x_end).state - id(zone2_x_begin).state; + int y_size = id(zone2_y_end).state - id(zone2_y_begin).state; + + char combined[80]; + sprintf(combined, "Curr Size: %d x %d", x_size, y_size); + id(tips_zone2_conf).publish_state(combined); + - id: check_zone3_vaild + then: + - lambda: |- + if (id(zone3_x_begin).state > id(zone3_x_end).state){ + id(tips_zone3_conf).publish_state("Err: X-Begin > X-End"); + return; + } + if (id(zone3_y_begin).state > id(zone3_y_end).state){ + id(tips_zone3_conf).publish_state("Err: Y-Begin > Y-End"); + return; + } + 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){ + id(tips_zone3_conf).publish_state("Configure below"); + return; + } + + int x_size = id(zone3_x_end).state - id(zone3_x_begin).state; + int y_size = id(zone3_y_end).state - id(zone3_y_begin).state; + + char combined[80]; + sprintf(combined, "Curr Size: %d x %d", x_size, y_size); + id(tips_zone3_conf).publish_state(combined); + - id: check_zout1_vaild + then: + - lambda: |- + if (id(zone_ex1_x_begin).state > id(zone_ex1_x_end).state){ + id(tips_zone_ex1_conf).publish_state("Err: X-Begin > X-End"); + return; + } + if (id(zone_ex1_y_begin).state > id(zone_ex1_y_end).state){ + id(tips_zone_ex1_conf).publish_state("Err: Y-Begin > Y-End"); + return; + } + id(tips_zone_ex1_conf).publish_state("Zone Exclusion 1"); + sensor: - platform: template name: "ESP CPU Speed" @@ -443,14 +631,18 @@ sensor: } if (id(bh1750_fast_update).state){ id(last_illuminance) = x; + // ESP_LOGD("custom", "Fast Update BH1850"); return x; } if (abs(id(last_illuminance) - x) > 1){ + id(last_illuminance) = x; return x; } return {}; + #-------------------------------------# + # 高级雷达数据 - platform: template name: "All Target Counts" id: all_target_count @@ -481,6 +673,8 @@ sensor: accuracy_decimals: 0 icon: mdi:account-multiple-minus-outline unit_of_measurement: "targets" + + # Target 1 - platform: template name: "Target1 X" id: target1_x @@ -509,6 +703,8 @@ sensor: unit_of_measurement: 'mm' state_class: measurement device_class: distance + + # Target 2 - platform: template name: "Target2 X" id: target2_x @@ -516,7 +712,6 @@ sensor: unit_of_measurement: 'mm' state_class: measurement device_class: distance - # update_interval: 1s - platform: template name: "Target2 Y" id: target2_y @@ -538,6 +733,8 @@ sensor: unit_of_measurement: 'mm' state_class: measurement device_class: distance + + # Target 3 - platform: template name: "Target3 X" id: target3_x @@ -559,7 +756,6 @@ sensor: unit_of_measurement: 'm/s' state_class: measurement device_class: speed - # update_interval: 1s - platform: template name: "Target3 Resolution" id: target3_resolution @@ -568,6 +764,7 @@ sensor: state_class: measurement device_class: distance + light: - platform: status_led name: sys_status @@ -589,6 +786,7 @@ output: id: board_info_ed pin: GPIO12 + switch: - platform: factory_reset name: Factory Reset @@ -614,7 +812,7 @@ button: icon: mdi:power-cycle name: "ESP Reboot" entity_category: diagnostic - + uart: id: uart_bus tx_pin: @@ -643,6 +841,7 @@ uart: }; id(last_update_ld2450) = millis(); + // p1 int16_t p1_x = (uint16_t((bytes[5] << 8) | bytes[4] )); if ((bytes[5] & 0x80) >> 7){ p1_x -= pow(2, 15); @@ -665,6 +864,7 @@ uart: } int16_t p1_distance_resolution = (uint16_t((bytes[11] << 8) | bytes[10] )); + // p2 int16_t p2_x = (uint16_t((bytes[13] << 8) | bytes[12] )); if ((bytes[13] & 0x80) >> 7){ p2_x -= pow(2, 15); @@ -687,6 +887,7 @@ uart: } int16_t p2_distance_resolution = (uint16_t((bytes[19] << 8) | bytes[18] )); + // p3 int16_t p3_x = (uint16_t((bytes[21] << 8) | bytes[20] )); if ((bytes[21] & 0x80) >> 7){ p3_x -= pow(2, 15); @@ -714,12 +915,14 @@ uart: bool p2_vaild = (p2_x != 0 || p2_y > 0); bool p3_vaild = (p3_x != 0 || p3_y > 0); + // zone exlude 1 + int16_t target_count_in_zone_ex1 = 0; - int16_t zone_ex1_x_min = id(zone_ex1_x_begin).state; // - x_ex_tolerance; - int16_t zone_ex1_x_max = id(zone_ex1_x_end).state; // + x_ex_tolerance; - int16_t zone_ex1_y_min = id(zone_ex1_y_begin).state; // - y_ex_tolerance; - int16_t zone_ex1_y_max = id(zone_ex1_y_end).state; // + y_ex_tolerance; + int16_t zone_ex1_x_min = id(zone_ex1_x_begin).state; + int16_t zone_ex1_x_max = id(zone_ex1_x_end).state; + int16_t zone_ex1_y_min = id(zone_ex1_y_begin).state; + int16_t zone_ex1_y_max = id(zone_ex1_y_end).state; bool p1_zone_ex_enter = false; bool p2_zone_ex_enter = false; @@ -761,11 +964,13 @@ uart: bool has_target_in_zone_all = (all_target_counts > 0); + // zone 1 check + int16_t target_count_in_zone1 = 0; - int16_t zone1_x_min = id(zone1_x_begin).state; // - x_tolerance; - int16_t zone1_x_max = id(zone1_x_end).state; // + x_tolerance; - int16_t zone1_y_min = id(zone1_y_begin).state; // - y_tolerance; - int16_t zone1_y_max = id(zone1_y_end).state; // + y_tolerance; + int16_t zone1_x_min = id(zone1_x_begin).state; + int16_t zone1_x_max = id(zone1_x_end).state; + int16_t zone1_y_min = id(zone1_y_begin).state; + int16_t zone1_y_max = id(zone1_y_end).state; if (p1_vaild && !p1_zone_ex_enter){ if (p1_x >= zone1_x_min && p1_x <= zone1_x_max && p1_y >= zone1_y_min && p1_y <= zone1_y_max){ @@ -784,11 +989,13 @@ uart: } bool has_target_in_zone1 = (target_count_in_zone1 > 0); + // zone 2 check + int16_t target_count_in_zone2 = 0; - int16_t zone2_x_min = id(zone2_x_begin).state; // - x_tolerance; - int16_t zone2_x_max = id(zone2_x_end).state; // + x_tolerance; - int16_t zone2_y_min = id(zone2_y_begin).state; // - y_tolerance; - int16_t zone2_y_max = id(zone2_y_end).state; // + y_tolerance; + int16_t zone2_x_min = id(zone2_x_begin).state; + int16_t zone2_x_max = id(zone2_x_end).state; + int16_t zone2_y_min = id(zone2_y_begin).state; + int16_t zone2_y_max = id(zone2_y_end).state; if (p1_vaild && !p1_zone_ex_enter){ if (p1_x >= zone2_x_min && p1_x <= zone2_x_max && p1_y >= zone2_y_min && p1_y <= zone2_y_max){ @@ -808,11 +1015,13 @@ uart: bool has_target_in_zone2 = (target_count_in_zone2 > 0); + // zone 3 check + int16_t target_count_in_zone3 = 0; - int16_t zone3_x_min = id(zone3_x_begin).state; // - x_tolerance; - int16_t zone3_x_max = id(zone3_x_end).state; // + x_tolerance; - int16_t zone3_y_min = id(zone3_y_begin).state; // - y_tolerance; - int16_t zone3_y_max = id(zone3_y_end).state; // + y_tolerance; + int16_t zone3_x_min = id(zone3_x_begin).state; + int16_t zone3_x_max = id(zone3_x_end).state; + int16_t zone3_y_min = id(zone3_y_begin).state; + int16_t zone3_y_max = id(zone3_y_end).state; if (p1_vaild && !p1_zone_ex_enter){ if (p1_x >= zone3_x_min && p1_x <= zone3_x_max && p1_y >= zone3_y_min && p1_y <= zone3_y_max){ @@ -831,6 +1040,7 @@ uart: } bool has_target_in_zone3 = (target_count_in_zone3 > 0); + // public all info if (id(target1_x).state != p1_x){ id(target1_x).publish_state(p1_x); } @@ -871,6 +1081,8 @@ uart: if (id(target3_resolution).state != p3_distance_resolution){ id(target3_resolution).publish_state(p3_distance_resolution); } + + // public target info if (id(all_target_count).state != all_target_counts){ id(all_target_count).publish_state(all_target_counts); @@ -900,6 +1112,7 @@ uart: id(zone3_target_exsits).publish_state(has_target_in_zone3); } + // zout if (id(zone_ex1_target_count).state != target_count_in_zone_ex1){ id(zone_ex1_target_count).publish_state(target_count_in_zone_ex1); } @@ -908,4 +1121,6 @@ uart: id(zone_ex1_target_exsits).publish_state(has_target_in_zone_ex1); } - id(init_zone_publish) = true; + if (!id(init_zone_publish)){ + id(init_zone_publish) = true; + }