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
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:
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.
The on_value of this text sensor will only trigger when the state changed.
Handle the on_value with a condition based on the state of it. (for exmaple turn the light green when state changend to “perfect”).
Call the script to switch the light off in 20 seconds at the end of the on_value every time the state changes.
The light will go off 20 seconds after the last change.
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.
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.
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