Garage parking aid to park in exact spot

I have a ESPHome automation that handles the garage door, and uses a SR04 sensor coupled with a LED lights that show different colors until the car is parked in the exact spot.

Everything works properly, but the led lights remain on all the time (on green) after I park the car. My objective is to turn off the led light 20 seconds after the car is parked. Any suggestions?
I am a newbie and thus I may not have structured this approach properly.

Extract of code below:

light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: D7
    num_leds: 20
    rgb_order: GRB
    name: "Parking Light Bay center"
    id: parking_light_bay_center
    restore_mode: ALWAYS_OFF 

sensor:
  # configuration entry for sr04 sensor 
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Parking Bay center distance from optimal"
    id: parking_bay_center_distance_from_optimal
    pulse_time: 20us
    timeout: 4.0m
    update_interval: 0.25s
    on_value: 
      then:
        - if: 
            # Show Red when too far into garage
            condition:
              sensor.in_range:
                id: parking_bay_center_distance_from_optimal
                above: 0.10
                below: 1.10
            then:
              light.turn_on:
                id: parking_light_bay_center
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%
        - if: 
            # Show Green when in right spot
            condition:
              sensor.in_range:
                id: parking_bay_center_distance_from_optimal
                above: 1.15
                below: 1.20
            then:
              light.turn_on:
                id: parking_light_bay_center
                brightness: 80%
                red: 0%
                green: 100%
                blue: 0%
        - if: 
            # show yellow if getting close to correct spot 
            condition:
              sensor.in_range:
                id: parking_bay_center_distance_from_optimal
                above: 1.20
                below: 1.25
            then:
              light.turn_on:
                id: parking_light_bay_center
                brightness: 100%
                # yellow
                red: 100%
                green: 100%
                blue: 0%

#***************************************
# Garage Door Switch
#**************************************

#binary sensor to check of door open or closed
binary_sensor:
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
    name: "Garage center Bay Door Contact Sensor"
    id: contact_sensor
    internal: true

# Relay Switch to send the  the pulse to
# the garage door opener (not exposed to HA)
switch:
  - platform: gpio
    pin: D5
    name: "Garage Door Relay"
    id: relay
    internal: true
    
# This creates the actual garage door in HA. The state is based
# on the contact sensor. Opening/closing the garage door simply
# turns the relay on/off with a 0.5s delay in between.
cover:
  - platform: template
    device_class: garage
    name: "Garage Door center bay"
    id: template_cov
    lambda: |-
      if (id(contact_sensor).state) {
        return COVER_CLOSED;
      } else {
        return COVER_OPEN;
      }
    open_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    close_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
1 Like

My general idea was to add a script (Automations and Templates — ESPHome) in mode “restart” that starts with a delay of 20 seconds and then a turn_off action for your light.

It will switch the light off 20 seconds after its last run. The problem is: When/How to call it. “on_value” on the senosr is not good because you are constantly getting values.

What could work is: Add an aditional (lambda) condtion to all your exting “ifs” that makes sure that the color is not already the target before changing the color of the light. So that the light turn_on gets only called when the state changed. You can then call the script evrytime the light changes. When the light does not change for 20 seconds the script will switch it off.

Another idea that would me more clean but is a bigger change and neeeds a lambda:

  1. Create a template text sensor that reflects the state of the car/light (“far away”, “close”, “perfect”). Basicly the same logic you already have based on your sensor but instead of a conditon in a trigger as a lambda.
  2. The on_value of this text sensor will only trigger when the state changed.
  3. Handle the on_value with a condition based on the state of it. (for exmaple turn the light green when state changend to “perfect”).
  4. Call the script to switch the light off in 20 seconds at the end of the on_value every time the state changes.
  5. The light will go off 20 seconds after the last change.

Maybe there is a smarter way to do it…

Oh another idea on how to trigger the script without much change:

  1. Make it so that every three possible light states use a different brigthness. (99% 100% and 80%)
  2. Create very simple template sensor that refelects the brightness of the light.
  3. The on value of that template sensor will only trigger when the state changes. So just call the script in the on_value without any condition.

Thanks, I am going to try the last suggestion this weekend, will report back

1 Like

seems you have a garage door sensor?
Get home assistant to send the garage door closed state to the ESP, and write an automation to turn the parking light off when the garage door is closed.

Or when garage door relay is triggered a count down to light off automation

@vecchioS Did you come up with a good solution to turn off the green?

Also, thanks for posting this. I’m in the process of building the ultrasonic sensor and connecting to a wemos d1 mini. I was stuck as to how I would build the logic behind the leds until I saw this example.

G’day

Are you able to share your solution. I have made the same project and code as you. (Works really well) But I am also at an impasse on how to code the light to stop after x seconds.

Thanks in advance.

This is the code I have, it works properly

mode: restart
  trigger:
    - platform: state
      entity_id: sensor.parking_distance_from_optimal
  condition:
    - condition: template
      value_template: "{{ (trigger.to_state.state | float(0)  - trigger.from_state.state | float(0)) | abs > 0.2 }}"
    - condition: state
      entity_id: cover.garage_door_bay
      state: "open"
  action:
    - service: light.turn_on
      entity_id: light.parking_light
      data:
        brightness: 100
        color_name: >-
          {% set distance = states('sensor.parking_bay_distance_from_optimal') | float(0) %}
          {% if distance > 3 %} blue
          {% elif 1.6 < distance <= 1.7 %} yellow
          {% elif 1.5 < distance <= 1.6 %} green
          {% elif 1.4 < distance <= 1.5 %} orange
          {% elif distance <= 1.4 %} red
          {% else %} aqua
          {% endif %}
    - delay: 00:00:30
    - service: light.turn_off
      entity_id: light.parking_light

in essence I inserted a delay and then turn the light off

Thank you… I am still a bit lost, are you able to show the entire code? I am not sure where this part fits in.

It just dawned on me this is an automation, not an esphome config. Thanks again.

I’m curious where you’ve positioned the sensor…
Thanks.

In the ceiling, front of the car, thus if ni csr present the distance is from ceiling to floor

However I must say these sensors are not that accurate, they send distance data that keeps changing at random intervals (1/2 seconds, 2 seconds, then 1 second) and the measurements are not that accurate

I have a low tech solution to the same problem

:grinning:

This is the low tech solution I use…


:grinning_face_with_smiling_eyes:

Here’s mMy new D1 Mini solution in ESPHome