{"id":337,"date":"2020-10-25T16:20:01","date_gmt":"2020-10-25T14:20:01","guid":{"rendered":"https:\/\/dev.drun.net\/?p=337"},"modified":"2020-10-25T16:20:01","modified_gmt":"2020-10-25T14:20:01","slug":"bluetooth-low-energy-ble-sensors-into-home-assistant","status":"publish","type":"post","link":"https:\/\/dev.krasi.net\/?p=337","title":{"rendered":"Bluetooth Low Energy ( BLE ) sensors into Home Assistant (or any other platform, actually)"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">General<\/h5>\n\n\n\n<p>In this tutorial we will use ESP32 board as a gateway between Bluetooth sensors ( Xiaomi LYWSD03MMC LCD configured here, but should work with anything else, too ) and Home Assistant&#8217;s MQTT server ( You can use any other platform instead of Home Assistant, of course).<\/p>\n\n\n\n<p>You can check some of the <a href=\"https:\/\/esphome.io\/components\/sensor\/xiaomi_ble.html\">supported BLE sensors here<\/a>.<\/p>\n\n\n\n<p>Prerequisites:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.aliexpress.com\/wholesale?SearchText=esp32\">ESP32 development board<\/a> ( you can buy any on aliexpress.com ). This is needed in order to directly access BLE sensors.<br><br><br><img decoding=\"async\" class=\"wp-image-340\" style=\"width: 200px;\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2020\/10\/esp32.jpg\" alt=\"\"><br><br><\/li><li><a href=\"https:\/\/dev.drun.net\/2020\/10\/15\/home-assistant-docker-installation\/\">Home assistant and MQTT configured<\/a>.<\/li><li><a href=\"https:\/\/esphome.io\/guides\/getting_started_command_line.html\">Installed esphome console tools<\/a> ( Linux or Mac OS X preferred, can&#8217;t help a lot with Windows, sorry ).<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">The yaml file (configuration file for esphome)<\/h5>\n\n\n\n<p>You need this file to be used as configuration basis for esphome. Tweak as you see fit and make sure your MQTT credentials are correct. Save as <code>esp32-ble.yaml<\/code> or anything you want.<\/p>\n\n\n\n<p>Note: You might have to replace &#8220;nodemcu-32s&#8221; with the board you have, check esphome documentation. In general, it should work with almost all ESP32 boards.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome:\n  name: ble_mqtt\n  platform: ESP32\n  board: nodemcu-32s\n\nwifi:\n  ssid: \"my-wifi-2ghz-network\"\n  password: \"my-wifi-password\"\n\n  ap:\n    ssid: \"ble_mqtt hotspot\"\n    password: \"my-ap-password\"\n\ncaptive_portal:\n\nlogger:\n\nota:\n  password: \"ota-password\"\n\nmqtt:\n  broker: IP_ADDRESS_OF_BROKER\n  port: 1883\n  username: USER\n  password: PASSWORD\n  topic_prefix: ble-sensors\n\nesp32_ble_tracker:\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">First installation<\/h5>\n\n\n\n<p>It must be done via cable, afterwards we can use OTA updates. Connect the ESP32 to the computer and execute<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome esp32-ble.yaml run<\/code><\/pre>\n\n\n\n<p>It will start compiliing the file, downloading required packages. When it&#8217;s done it will ask you how to install. Select the USB port where it&#8217;s connected. If you don&#8217;t see it, check your connection.<\/p>\n\n\n\n<p>When esphome tries to access the port, press the boot ( or flash ) button on the ESP32 board, wait a second or two and depress it.<\/p>\n\n\n\n<p>Now it should start writing. If it still doesn&#8217;t, please check how to properly enter flash mode on your ESP32 variant.<\/p>\n\n\n\n<p>When it&#8217;s done, you should see a lot of logs, similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INFO Waiting for result...\nINFO OTA successful\nINFO Successfully uploaded program.\nINFO Starting log output from ble-sensors\/debug\nINFO Connected to MQTT broker!\n&#91;15:12:57]&#91;I]&#91;ota:269]: OTA update finished!\n&#91;15:13:02]&#91;I]&#91;mqtt:202]: MQTT Connected!\n&#91;15:13:02]&#91;I]&#91;app:059]: setup() finished successfully!\n&#91;15:13:02]&#91;I]&#91;app:105]: ESPHome version 1.15.2 compiled on Oct 25 2020, 15:09:54\n&#91;15:13:02]&#91;C]&#91;wifi:443]: WiFi:\n&#91;15:13:02]&#91;C]&#91;wifi:303]:   SSID: 'my-wifi-2ghz-network'\n&#91;15:13:02]&#91;C]&#91;wifi:304]:   IP Address: 192.168.88.51\n&#91;15:13:02]&#91;C]&#91;wifi:306]:   BSSID: 64:D1:54:A7:7C:43\n&#91;15:13:02]&#91;C]&#91;wifi:307]:   Hostname: 'ble_mqtt'\n&#91;15:13:02]&#91;C]&#91;wifi:311]:   Signal strength: -61 dB \u2582\u2584\u2586\u2588\n&#91;15:13:02]&#91;C]&#91;wifi:315]:   Channel: 6\n&#91;15:13:02]&#91;C]&#91;wifi:316]:   Subnet: 255.255.255.0\n&#91;15:13:02]&#91;C]&#91;wifi:317]:   Gateway: 192.168.88.1\n&#91;15:13:02]&#91;C]&#91;wifi:318]:   DNS1: 192.168.88.1\n&#91;15:13:02]&#91;C]&#91;wifi:319]:   DNS2: 192.168.1.1\n&#91;15:13:02]&#91;C]&#91;logger:185]: Logger:\n&#91;15:13:02]&#91;C]&#91;logger:186]:   Level: DEBUG\n&#91;15:13:02]&#91;C]&#91;logger:187]:   Log Baud Rate: 115200\n&#91;15:13:02]&#91;C]&#91;logger:188]:   Hardware UART: UART0\n&#91;15:13:02]&#91;C]&#91;esp32_ble_tracker:530]: BLE Tracker:\n&#91;15:13:02]&#91;C]&#91;esp32_ble_tracker:531]:   Scan Duration: 300 s\n&#91;15:13:02]&#91;C]&#91;esp32_ble_tracker:532]:   Scan Interval: 320.0 ms\n&#91;15:13:02]&#91;C]&#91;esp32_ble_tracker:533]:   Scan Window: 30.0 ms\n&#91;15:13:02]&#91;C]&#91;esp32_ble_tracker:534]:   Scan Type: ACTIVE\n&#91;15:13:08]&#91;D]&#91;esp32_ble_tracker:544]: Found device 18:BE:D0:6A:85:55 RSSI=-91\n&#91;15:13:08]&#91;D]&#91;esp32_ble_tracker:565]:   Address Type: RANDOM\n&#91;15:13:40]&#91;D]&#91;esp32_ble_tracker:544]: Found device A4:C1:38:F6:CA:AF RSSI=-66\n&#91;15:13:40]&#91;D]&#91;esp32_ble_tracker:565]:   Address Type: PUBLIC\n&#91;15:13:40]&#91;D]&#91;esp32_ble_tracker:567]:   Name: 'LYWSD03MMC'\n&#91;15:14:12]&#91;D]&#91;esp32_ble_tracker:544]: Found device A4:C1:38:86:F3:2F RSSI=-72\n&#91;15:14:12]&#91;D]&#91;esp32_ble_tracker:565]:   Address Type: PUBLIC\n&#91;15:14:12]&#91;D]&#91;esp32_ble_tracker:567]:   Name: 'LYWSD03MMC'<\/code><\/pre>\n\n\n\n<p>Note the Found device lines. In my case it discovered several 3 BLE devices, two of which are my Xiaomi thermometers.<\/p>\n\n\n\n<p>Write down the MAC addresses of all devices you want to add ( mine are <em>A4:C1:38:F6:CA:AF<\/em> and <em>A4:C1:38:86:F3:2F<\/em> )<\/p>\n\n\n\n<p>Add the following to the yaml file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sensor:\n - platform: xiaomi_lywsd03mmc\n   id: \"ble_01\"\n   mac_address: \"A4:C1:38:86:F3:2F\"\n   temperature:\n      name: \"ble_01_temp\"\n   humidity:\n      name: \"ble_01_humidity\"\n   battery_level:\n      name: \"ble_01_battery\"<\/code><\/pre>\n\n\n\n<p>Name them as you wish and set your mac addresses from before. If you have different types of BLE sensors, please check esphome documentation on the platform and how to properly add it.<\/p>\n\n\n\n<p>For some devices, this is not enough, sadly. For my case, I have to add additional parameter, bindkey , which is used for payload decryption, so that we can find out the values ( obviously it&#8217;s encrypted ).<\/p>\n\n\n\n<p>The easiest way is to use a phone ( I use Android, can&#8217;t really tell if ti works with iPhone ) and navigate to <a href=\"https:\/\/atc1441.github.io\/TelinkFlasher.html\">https:\/\/atc1441.github.io\/TelinkFlasher.html<\/a> ( also tested and works on Mac OS X ).<\/p>\n\n\n\n<p class=\"has-text-align-left\"><br><img decoding=\"async\" class=\"wp-image-341\" style=\"width: 400px;\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2020\/10\/Screenshot-2020-10-25-at-16.09.51.png\" alt=\"\"><\/p>\n\n\n\n<p>Click on <strong>Connect<\/strong> button, a popup should appear with all bluetooth devices.<\/p>\n\n\n\n<p>Select the sensor that you want to find it&#8217;s bindkey. <\/p>\n\n\n\n<p>Tap <strong>Do activation<\/strong> button afterwards, wait 1-3 seconds and copy the <strong>Mi Bind Key<\/strong> field, when it&#8217;s prefilled.<\/p>\n\n\n\n<p>In the yaml file, change it to look like ( replacing BINDKEYHERE with the actual bind key from above ):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sensor:\n - platform: xiaomi_lywsd03mmc\n   id: \"ble_01\"\n   mac_address: \"A4:C1:38:86:F3:2F\"\n   bindkey: \"BINDKEYHERE\"\n   temperature:\n      name: \"ble_01_temp\"\n   humidity:\n      name: \"ble_01_humidity\"\n   battery_level:\n      name: \"ble_01_battery\"<\/code><\/pre>\n\n\n\n<p>When you are done making changes, save the file and run again<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome esp32-ble.yaml run <\/code><\/pre>\n\n\n\n<p>Now you don&#8217;t need to be connected anymore via cable. If you are powering it from USB charger, for example, then esphome will discover it and upload via OTA.<\/p>\n\n\n\n<p>You should now see something like this in the logs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:012]: Xiaomi LYWSD03MMC\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:013]:   Bindkey: 2A.53.55.E8.3F.98.B8.0D.D2.6F.CA.DB.86.19.F6.07 (16)\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:014]:   Temperature 'ble_01_temp'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:014]:     Unit of Measurement: '\u00b0C'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:014]:     Accuracy Decimals: 1\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:014]:     Icon: 'mdi:thermometer'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:015]:   Humidity 'ble_01_humidity'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:015]:     Unit of Measurement: '%'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:015]:     Accuracy Decimals: 0\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:015]:     Icon: 'mdi:water-percent'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:016]:   Battery Level 'ble_01_battery'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:016]:     Unit of Measurement: '%'\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:016]:     Accuracy Decimals: 0\n&#91;15:13:02]&#91;C]&#91;xiaomi_lywsd03mmc:016]:     Icon: 'mdi:battery'<\/code><\/pre>\n\n\n\n<p>You soon should start getting some readings in the mqtt topics ( you can check with external application, I use MQTT Explorer for this ).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Home Assistant configuration<\/h5>\n\n\n\n<p>In order for Home assistant to find the new sensors, they need to be configured manually.<\/p>\n\n\n\n<p>Open <code>configuration.yaml<\/code> of HASS and add the following to your <code>sensor:<\/code> section. If it does not exist, create it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sensor:<\/code><\/pre>\n\n\n\n<p>Add your configuration, replacing my example ids and names with yours:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># xiaomi temperature 1\n  - platform: mqtt\n    name: \"ble_01_temp\"\n    state_topic: \"ble-sensors\/sensor\/ble_01_temp\/state\"\n    unit_of_measurement: '\u00b0C'\n    value_template: \"{{ value }}\"\n    availability_topic: \"zigbee2mqtt\/bridge\/state\"\n    device_class: \"temperature\"\n\n  - platform: mqtt\n    name: \"ble_01_humidity\"\n    state_topic: \"ble-sensors\/sensor\/ble_01_humidity\/state\"\n    unit_of_measurement: '%'\n    value_template: \"{{ value }}\"\n    availability_topic: \"zigbee2mqtt\/bridge\/state\"\n    device_class: \"humidity\"\n\n  - platform: mqtt\n    name: \"ble_01_battery\"\n    state_topic: \"ble-sensors\/sensor\/ble_01_battery\/state\"\n    unit_of_measurement: '%'\n    value_template: \"{{ value }}\"\n    availability_topic: \"zigbee2mqtt\/bridge\/state\"\n    device_class: \"battery\"<\/code><\/pre>\n\n\n\n<p>Save the file, check it via <strong>Configuration<\/strong> -&gt; <strong>Server controls<\/strong> -&gt; <strong>Check configuration<\/strong> and if it&#8217;s valid, restart it from the same page.<\/p>\n\n\n\n<p>Now you should be able to add the sensor to your Home Assistant ( there will be another customization post very soon ).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Adding more sensors<\/h5>\n\n\n\n<p>You should save your yaml file for updating the BLE gateway somewhere safe, otherwise you will need to run all the steps again.<\/p>\n\n\n\n<p>When you want to add a new BLE sensor, first execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome esp32-ble.yaml logs<\/code><\/pre>\n\n\n\n<p>in order to find the sensor&#8217;s MAC address in the logs on the screen.<\/p>\n\n\n\n<p>After you&#8217;ve found the MAC address, add the configuration in your <code>sensor:<\/code> section, similar to above. When done, execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome esp32-ble.yaml run<\/code><\/pre>\n\n\n\n<p>In order to compile and upload the new configuration. If everything was correct, you soon should start receiving updates from the new sensor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>General In this tutorial we will use ESP32 board as a gateway between Bluetooth sensors ( Xiaomi LYWSD03MMC LCD configured here, but should work with anything else, too ) and Home Assistant&#8217;s MQTT server ( You can use any other platform instead of Home Assistant, of course). You can check some of the supported BLE sensors here. Prerequisites: ESP32 development board ( you can buy any on aliexpress.com ). This is needed in order to directly access BLE sensors. Home assistant and MQTT configured. Installed esphome console tools ( Linux or Mac OS X preferred, can&#8217;t help a lot with Windows, sorry ). The yaml file (configuration file for esphome) You need this file to be used as configuration basis for esphome. Tweak as you see fit and make sure your MQTT credentials are correct. Save as esp32-ble.yaml or anything you want. Note: You might have to replace &#8220;nodemcu-32s&#8221; with the board you have, check esphome documentation. In general, it should work with almost all ESP32 boards. First installation It must be done via cable, afterwards we can use OTA updates. Connect the ESP32 to the computer and execute It will start compiliing the file, downloading required packages. When it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,5],"tags":[7,12,15],"class_list":["post-337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hardware","category-home-assistant","category-software","tag-bluetooth","tag-esp32","tag-hass"],"_links":{"self":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=337"}],"version-history":[{"count":0,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}