I’m building a project using ESPHome and ultrasonic sensors to control an addressable WS2812B LED strip dynamically. As an object moves closer to one sensor, the LEDs on that side will light up red, indicating its position.
Project Setup:
I’m currently in the development phase, using two ultrasonic sensors connected to an ESP device for initial testing.
Desired Functionality:
- Ultrasonic Sensors: Two ultrasonic sensors will measure the distance to an object.
- Object Position: A template sensor will calculate the object’s position as a percentage based on readings from both sensors (higher percentage closer to sensor 1).
- Object Size (Optional): Another template sensor can estimate the object’s size based on the combined distance readings.
- Dynamic Light Control: The calculated object position percentage will control the number of LEDs illuminated on the Neopixel strip, visualizing the object’s location.
Challenge:
I initially used ChatGPT to generate an ESPHome configuration for this project. While the code seemed comprehensive, uploading it to ESPHome resulted in an error message stating “automation is not a component.”
Provided Configuration:
substitutions:
name: ultrasonic_sensor_project
friendly_name: Ultrasonic Sensor Project
esphome:
name: ${name}
friendly_name: ${friendly_name}
name_add_mac_suffix: false
project:
name: esphome.ultrasonic
version: '1.0'
esp8266:
board: esp01_1m
logger:
api:
ota:
improv_serial:
wifi:
ap: {}
captive_portal:
dashboard_import:
package_import_url: github://esphome/example-configs/esphome-web/esp8266.yaml@main
import_full_config: true
web_server:
sensor:
- platform: ultrasonic
trigger_pin: GPIO4
echo_pin: GPIO5
name: "Ultrasonic Sensor 1"
id: ultrasonic_sensor_1
update_interval: 1s
- platform: ultrasonic
trigger_pin: GPIO14
echo_pin: GPIO12
name: "Ultrasonic Sensor 2"
id: ultrasonic_sensor_2
update_interval: 1s
- platform: template
name: "Object Position Percentage"
update_interval: 1s
lambda: |-
float distance1 = id(ultrasonic_sensor_1).state * 29.2407; // Scale up the measured distance
float distance2 = id(ultrasonic_sensor_2).state * 29.2407; // Scale up the measured distance
float max_threshold = 5.0; // 5 meters threshold
float wiggle_room = 0.005; // Adjust this value as needed
if (fabs(distance1 - distance2) <= wiggle_room) {
return -1; // No object detected between the sensors
}
// Calculate the total distance between the sensors
float total_distance = distance1 + distance2;
// Calculate the percentage value
float position_percentage = ((distance1) / total_distance) * 100;
// Ensure the position percentage is within [0, 100]
position_percentage = constrain(position_percentage, 0, 100);
return position_percentage;
unit_of_measurement: '%'
- platform: template
name: "Object Size"
update_interval: 1s
lambda: |-
float distance1 = id(ultrasonic_sensor_1).state * 29.2407; // Scale up the measured distance
float distance2 = id(ultrasonic_sensor_2).state * 29.2407; // Scale up the measured distance
float max_threshold = 5.0; // 5 meters threshold
float wiggle_room = 0.005; // Adjust this value as needed
if (fabs(distance1 - distance2) <= wiggle_room) {
return 0; // No object detected between the sensors
}
// Calculate the object size
float object_size = max_threshold - (distance1 + distance2);
object_size = object_size * 100; // Convert to centimeters
return object_size;
unit_of_measurement: 'cm'
light:
- platform: neopixelbus
type: GRB # Color order
pin: GPIO2 # Pin connected to the LED strip
num_leds: 720 # Number of LEDs in your strip
variant: WS2812B
name: ws2812b_strip # Name of the light entity
automation:
- id: illuminate_object
alias: Illuminate Object
trigger:
platform: homeassistant
event: start
action:
- delay: 2s # Wait for the sensors to initialize
- while:
condition:
lambda: 'return id(ultrasonic_sensor_1).state == 0 && id(ultrasonic_sensor_2).state == 0;' # Wait until both sensors have valid readings
then:
- delay: 1s # Wait for sensors to stabilize
- variables:
scaled_distance_sensor_1_cm: !lambda |-
return id(ultrasonic_sensor_1).state * 29.2407; // Scale up the measured distance
scaled_distance_sensor_2_cm: !lambda |-
return id(ultrasonic_sensor_2).state * 29.2407; // Scale up the measured distance
length_of_strip_cm: 500 # Length of the strip in centimeters
number_of_leds_to_light_up: !lambda |-
return ((720 * (scaled_distance_sensor_1_cm - scaled_distance_sensor_2_cm)) / length_of_strip_cm) | int;
- service: light.turn_off
target:
entity_id: light.ws2812b_strip
- service: light.turn_on
data_template:
entity_id: light.ws2812b_strip
brightness: 255 # Full brightness for illumination
rgb_color: [255, 0, 0] # Red color for illumination
transition: 0
I’m looking for someone who worked on similar project to :
- Help in understanding the “automation is not a component” error.
- Give guidance on revising the configuration to achieve the desired functionality within ESPHome.
Thanks in advance for your help!