Two Output Control within ESPHome

I am building an ultrasonic garage parking sensor. It will serve as a guide to aid in parking. I am using a Wemos D1 Mini and the HC-SR04 Sensor. I want to also have 3 LEDs Red, Yellow, Green.

The code works great with one LED. I started with the Red LED and programed it to come on when the distance is less than 1m.

I’m working on connecting the Yelllow and Green LED, but encountered an issue when programming. The code won’t allow for two different outputs.

esphome:
  name: van-parking-sensor

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "redacted"

ota:
  password: "redacted"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Parking-Sensor-Test4"
    password: "redacted"

captive_portal:

#RED LED CREATION

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

sensor:
  - platform: ultrasonic
    name: 'van_parking_sensor'
    id: van_parking_sensor
    trigger_pin: D1
    echo_pin: D2
    accuracy_decimals: 1
    update_interval: .25s
    timeout: 4.0m
    pulse_time: 10us
    on_value:
      then: 
        - if:
            #Light Red LED when too close- Turn off Yellow and Green
            condition:
              sensor.in_range:
                id: van_parking_sensor
                above: .1
                below: 1
            then:
              output.turn_on:
                id: red_led_van_sensor
        - if: 
            condition: 
              sensor.in_range: 
                id: van_parking_sensor
                above: 1
                below: 1.5
            then:
              output.turn_off:
                id: red_led_van_sensor


Do you have any suggestions on how to add a second LED that can be controlled independently of the first LED?

output: 
  - platform: gpio
    id: red_led_van_sensor
    pin: D7
  - platform: gpio
    id: yellow_led_van_sensor
    pin: Dwhatever

I deleted my earlier post out of sheer shame - it was just a bit idiotic… :slight_smile:

@jebsimmons - mind sharing what you use as a trigger and possibly the full config as well?

@nickrout I thought I tried that and it was giving me the ole “Pin D6 isn’t available for GPIO”. But I tried it again and it is working. Thanks for the help!

I’m configuring everything on the wemos d1 mini node in ESP Home. Not triggering anything within HA. Just adding the sensors to HA after the config is done in ESP Home.

That config in ESPHome is what would be helpful, if you don’t mind sharing, of course.

@berniedp

Here is my complete yaml for the garage parking sensor with 3 colored LEDs. Is this what you are looking for?

esphome:
  name: van-parking-sensor

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "wnWSgINc6J8S4+Bj1J88ORBAHPxlz2ck98P/zEx7kY8="

ota:
  password: "73db87eb8807d0ecd68c54aaf3f79eaf"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Parking-Sensor-Test4"
    password: "vzgvMxZN6tEu"

captive_portal:

#RED LED CREATION

output: 
  - platform: gpio
    id: red_led_van_sensor
    pin: D7
  - platform: gpio
    id: yellow_led_van_sensor
    pin: D6
  - platform: gpio
    id: green_led_van_sensor
    pin: D5

sensor:
  - platform: ultrasonic
    name: 'van_parking_sensor'
    id: van_parking_sensor
    trigger_pin: D1
    echo_pin: D2
    unit_of_measurement: "m"
    accuracy_decimals: 1
    update_interval: .25s
    timeout: 5.0m
    pulse_time: 10us
    on_value:
      then: 
        - if: 
            # Turn On Green when 1.3 m away
            condition:
              sensor.in_range:
                id: van_parking_sensor
                above: 1.3
            then:
              output.turn_on:
                id: green_led_van_sensor
        - if: 
            # Turn On Yellow from 1.3 away until 1.1m away (45")
            condition:
              sensor.in_range:
                id: van_parking_sensor
                above: 1.1
                below: 1.3
            then:
              output.turn_on:
                id: yellow_led_van_sensor
        - if:
            # Turn On Red for anything beleow 1.1m or 45"
            condition:
              sensor.in_range:
                id: van_parking_sensor
                below: 1.1
            then:
              output.turn_on:
                id: red_led_van_sensor
        - if: 
            # Turn Off Red if more than 1.1m 
            condition: 
              sensor.in_range: 
                id: van_parking_sensor
                above: 1.1
            then:
              output.turn_off:
                id: red_led_van_sensor
        - if: 
            # Turn Off Green if Less than 1.3m
            condition: 
              sensor.in_range: 
                id: van_parking_sensor
                below: 1.3
            then:
              output.turn_off:
                id: green_led_van_sensor
        - if: 
            #Turn off Yellow if less than 1.1m
            condition: 
              sensor.in_range: 
                id: van_parking_sensor
                below: 1.1
            then:
              output.turn_off:
                id: yellow_led_van_sensor
        - if: 
            #Turn off Yellow if less than 1.1m
            condition: 
              sensor.in_range: 
                id: van_parking_sensor
                above: 1.3
            then:
              output.turn_off:
                id: yellow_led_van_sensor

Grand - thanks for that!

What do you have connected to pin D1 on your ultrasonic sensor? I’m trying to figure out how I would trigger the ultrasonic to switch on…

@berniedp

Wemos D1 Mini: Pin D1 → HC-SR04 Trigger Pin
Wemos D1 Mini: Pin D2 → HC-SR04 Echo Pin
Wemos D1 Mini: Pin 5v → HC-SR04 VCC
Wemos D1 Mini: Pin GRD → HC-SR04 GRD

LEDs
Red → Wemos D1 Pin D7 and Ground
Yellow–> Wemos D1 Pin D6 and Ground
Green -->Wemos D1 Pin D5 and Ground

Hope this helps.

Here’s my new D1 Mini solution in ESPHome