Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Break beam sensor using VL53L0X for Home Assistant using ESPHome
##
## A standard "break beam sensor" uses a laser or LED with a device on both
## sides of an opening and is triggered when something breaks the beam
## (such as a customer walking through the beam). Some garage doors use them.
## Stores often use them at the entrace to ring a chime when a customer enters
## or leaves.
##
## This is my attempt at using a VL53L0X i2c Time of Flight sensor
## as a break beam sensor in Home Assistant using ESPHome.
##
## The binary sensor 'breakbeam_sensor' is
## * A synthetic break beam that is controlled by changes
## to 'breakbeam_sensor_dist'.
## * Should only send changes when the 'beam is broken':
## * 'breakbeam_sensor_dist' becomes < 'minTripDistance' (or)
## * 'breakbeam_sensor_dist' becomes to greater than 'minTripDistance'
##
## The sensor 'breakbeam_sensor_dist' is the actual VL53L0X sensor.
## * The maximum reliable "open" distance seems to be to about 1.5 meters.
## * This sensor is marked intnernal as to not send distance data to HA.
## * When distance first falls less that 'minTripDistance',
## 'breakbeam_sensor' will turned on.
## * When the distance is first greater than 'minTripDistance', the
## 'breakbeam_sensor' will be turned off.
## * This will poll at 0.1s. This seems fast enough, but 1s was not.
##
esphome:
name: distance-sensor-0
esp8266:
board: esp01_1m
# Logging (DEBUG is the standard level)
logger:
level: INFO
# Enable Home Assistant API
api:
ota:
password: "XXXXX"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Distance-Sensor-0"
password: "XXXXX"
captive_portal:
# the vl53l0x is i2c
i2c:
scan: true
##
## The virutal break beam sensor.
## The state of this will be controlled
## by breakbeam_sensor_dist's lambda as necessary.
##
binary_sensor:
- name: "Breakbeam Sensor"
id: breakbeam_sensor
platform: template
device_class: motion
##
## The actual sensor.
## This sensor's lambda will control the state of our
## virtual break beam sensor, breakbeam_sensor.
##
sensor:
- name: "Breakbeam Sensor Dist"
id: breakbeam_sensor_dist
platform: vl53l0x
address: 0x29
update_interval: 0.1s
long_range: true
internal: true
#
# Only send a value back if breakbeam_sensor changes.
#
filters:
- lambda: !lambda |-
/**
* YOU MIGHT WANT TO CONFIGURE THIS.
* Distance below which to trip the virtual break beam sensor.
* '0.5' meters is about 20 inches.
*/
static double minTripDistance = 0.5;
if (x <= minTripDistance) {
if (id(breakbeam_sensor).state == true) {
// Beam was already broken
return {};
}
// Beam was just broken
id(breakbeam_sensor).publish_state(true);
ESP_LOGI("breakbeam_sensor_dist", "Set breakbeam_sensor to Detected");
return {};
}
else {
if (id(breakbeam_sensor).state == false) {
// Beam was already un-broken
return {};
}
// Beam was just un-broken
id(breakbeam_sensor).publish_state(false);
ESP_LOGI("breakbeam_sensor_dist", "Set breakbeam_sensor to Cleared");
return {};
}
@blakeseufert
Copy link

blakeseufert commented Sep 22, 2022

This is super helpful thank you. Quick question, does setting the poll rate .1ms catch even small movements? Im hoping to use this in the mailbox to detect when even a small leaflet is dropped. Look forward to your thoughts. Also, i'm guessing this is not sending data to Home Assistant every .1s? Just on the binary_sensor change?

@ZixZeven
Copy link

ZixZeven commented Nov 7, 2022

I am using this code for my project. It works great as-is on my D1 mini. I am new to YAML and trying to mod the code to add an LED to light-on on detect and off on cleared, but can't figure it out. Any ideas?

@kdorff
Copy link
Author

kdorff commented Nov 7, 2022

Do some testing. 0.1s worked well enough for my needs, but I imagine you can poll more often if it seems necessary.

Do some testing. 0.1s worked well enough for my needs, but I imagine you can poll more often if it seems necessary.

@kdorff
Copy link
Author

kdorff commented Nov 7, 2022

I am using this code for my project. It works great as-is on my D1 mini. I am new to YAML and trying to mod the code to add an LED to light-on on detect and off on cleared, but can't figure it out. Any ideas?

I think you'd use something like a GPIO output https://esphome.io/components/output/gpio.html
or possibly a GPIO switch https://esphome.io/components/switch/gpio.html

And then turn the switch/output on and off when there is motion / motion stops. If you name your output light_1, you should be able to turn on with id(light_1).turn_on(); or with YAML - output.turn_on: light_1.

My recommendation is get the LED working independently in the ESPHome project, verifying that you know how to turn it on and off. After you are confident with that, then apply what you've learned about using the LED to the motion lambda.

Good luck!
Kevin

@ZixZeven
Copy link

ZixZeven commented Nov 8, 2022

Kevin, thanks you very much. You have provided me the hint and made it worked. Attached is the added code in bold.

...
if (x <= minTripDistance) {
if (id(breakbeam_sensor).state == true) {
id(gpio_d7).turn_on();
return {};
}
id(breakbeam_sensor).publish_state(true);
ESP_LOGI("breakbeam_sensor_dist", "Set breakbeam_sensor to Detected");
return {};
}
else {
if (id(breakbeam_sensor).state == false) {
id(gpio_d7).turn_off();
return {};
}
id(breakbeam_sensor).publish_state(false);
ESP_LOGI("breakbeam_sensor_dist", "Set breakbeam_sensor to Cleared");
return {};
}

output:
- platform: gpio
pin: D7
id: gpio_d7

@kdorff
Copy link
Author

kdorff commented Nov 8, 2022

Kevin, thanks you very much. You have provided me the hint and made it worked. ...

So glad to hear it!! Have fun on your ESPHome adventure! You can do lots of cool things with it.

@rcientista
Copy link

rcientista commented Nov 28, 2022

Hi,

I have set up this and it is working. Thank you.

However, I would like to add a max distance also for the sensor to be triggered.

For example, it would only trigger if between 0.2 and 0.5.
I am not a coder or anything of the sort and was wondering if you could help with this.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment