Advertisement
swamp_ig

ESPHome BT-LE tag tracker with battery

Oct 7th, 2023
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 2.69 KB | Source Code | 0 0
  1. esphome:
  2.   name: "garage-presence"
  3.   friendly_name: "garage-presence"
  4.   platform: ESP32
  5.   board: esp32dev
  6.  
  7. # Enable logging
  8. logger:
  9.  
  10. # Enable Home Assistant API
  11. api:
  12.   encryption:
  13.     key: "*********"
  14.  
  15. ota:
  16.   password: "********"
  17.  
  18. wifi:
  19.   ssid: !secret wifi_ssid
  20.   password: !secret wifi_password
  21.  
  22.   manual_ip:
  23.     static_ip: 10.0.0.86
  24.     gateway: 10.0.0.1
  25.     subnet: 255.255.255.0
  26.  
  27.   # Enable fallback hotspot (captive portal) in case wifi connection fails
  28.   ap:
  29.     ssid: "Garage Presence Fallback Hotspot"
  30.     password: "*******"
  31.  
  32. captive_portal:
  33. esp32_ble_tracker:
  34.   id: ble_tracker_id
  35.   scan_parameters:
  36.     interval: 10s
  37.     window: 1100ms
  38.     active: false
  39. ## I used this bit to figure out which byte held the battery data, it was byte 1.
  40. #  on_ble_advertise:
  41. #  - mac_address: **:**:**:**:**:**
  42. #    then:
  43. #      - lambda: |-
  44. #            for (auto data : x.get_service_datas())
  45. #              ESP_LOGI("main", "Data Update UUID: %s Data: %s", data.uuid.to_string().c_str(), format_hex_pretty(data.data).c_str());
  46.   on_ble_service_data_advertise:
  47.     - mac_address: **:**:**:**:**:**
  48.       service_uuid: "5242"
  49.       then:
  50.         - lambda: |-
  51.             id(penny_mazda_battery).publish_state(x[1]);
  52.  
  53. binary_sensor:
  54.   - platform: ble_presence
  55.     mac_address: **:**:**:**:**:**
  56.     id: penny_mazda
  57.     name: "Penny Mazda"
  58.     device_class: "presence"
  59.     icon: mdi:car
  60.     filters:
  61.       - delayed_off: 30s
  62.     on_press:
  63.     - script.execute: ble_active_scan      
  64.  
  65. sensor:
  66.   - platform: ble_rssi
  67.     mac_address: **:**:**:**:**:**
  68.     id: penny_mazda_rssi
  69.     name: "Penny Mazda RSSI"
  70.     entity_category: "diagnostic"
  71.   - platform: template
  72.     id: penny_mazda_battery
  73.     name: "Penny Mazda Battery"
  74.     device_class: "battery"
  75.     unit_of_measurement: "%"
  76.     entity_category: "diagnostic"
  77.     accuracy_decimals: 0
  78.  
  79. script:
  80.   - id: ble_active_scan
  81.     mode: single
  82.     then:
  83.     - lambda: |-
  84.         id(ble_tracker_id).stop_scan();
  85.     - delay: 2s
  86.     - lambda: |-
  87.         id(ble_tracker_id).set_scan_active(true);
  88.         id(ble_tracker_id).start_scan();
  89.     - logger.log:
  90.         format: Active scan started
  91.         level: INFO
  92.     - delay: 30s
  93.     - logger.log:
  94.         format: Active scan stopped
  95.         level: INFO
  96.     - lambda: |-
  97.         id(ble_tracker_id).stop_scan();
  98.     - delay: 2s
  99.     - lambda: |-
  100.         id(ble_tracker_id).set_scan_active(false);
  101.         id(ble_tracker_id).start_scan();
  102.  
  103. interval:
  104.   - interval: 10min
  105.     then:
  106.     - script.execute: ble_active_scan
  107.  
  108. button:
  109.   - platform: template
  110.     name: "BLE Active Scan"
  111.     id: run_active_scan
  112.     on_press:
  113.     - script.execute: ble_active_scan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement