Exploring Smart Home Automation with ESPHome and Ultrasonic Sensors

Hi everyone, I’m diving into the world of smart home automation with ESPHome and ultrasonic sensors. My goal is to create a dynamic lighting system using addressable WS2812B LED strips. The idea is that as an object moves closer to one of the ultrasonic sensors, the LEDs on that side will light up red, indicating its position. This project is still in the early development phase, and I’m currently testing with two ultrasonic sensors connected to an ESP device.

Project Setup:

  • Two ultrasonic sensors are measuring the distance to an object.
  • A template sensor calculates the object’s position as a percentage based on readings from both sensors.
  • The calculated position percentage will control the number of LEDs illuminated on the Neopixel strip, visualizing the object’s location.

Challenges Faced:
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.” I’m looking for guidance on revising the configuration to achieve the desired functionality within ESPHome.

Current Configuration:
yaml
substitutions:
name: ultrasonic_sensor_project
friendly_name: Ultrasonic Sensor Project

escphome:
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:

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;
    float distance2 = id(ultrasonic_sensor_2).state * 29.2407;
    float max_threshold = 5.0;
    float wiggle_room = 0.005;

    if (fabs(distance1 - distance2) <= wiggle_room) {
    return -1;
    }

    float total_distance = distance1 + distance2;
    float position_percentage = ((distance1) / total_distance) * 100;
    position_percentage = constrain(position_percentage, 0, 100);
    return position_percentage;
    unit_of_measurement: ‘%’

light:

  • platform: neopixelbus
    type: GRB
    pin: GPIO2
    num_leds: 720
    variant: WS2812B
    name: ws2812b_strip

automation:

  • id: illuminate_object
    alias: Illuminate Object
    trigger:
    platform: homeassistant
    event: start
    action:
    • delay: 2s
    • while:
      condition:
      lambda: ‘return id(ultrasonic_sensor_1).state == 0 && id(ultrasonic_sensor_2).state == 0;’
      then:
      - delay: 1s
    • variables:
      scaled_distance_sensor_1_cm: !lambda |
      return id(ultrasonic_sensor_1).state * 29.2407;
      scaled_distance_sensor_2_cm: !lambda |
      return id(ultrasonic_sensor_2).state * 29.2407;
      length_of_strip_cm: 500
      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
      rgb_color: [255, 0, 0]
      transition: 0

I’m looking for someone who has worked on a similar project to help me understand the “automation is not a component” error and provide guidance on revising the configuration. Any insights or suggestions would be greatly appreciated!

Cheers,
[Your Name]