I am building an automated trash can (lid) using EspHome. I am using an HC-SR04 ultrasonic sensor and a servo. I have everything working except that I want to have the automation of opening the lid inside EspHome. Esphome devices sometimes loose connectivity and this really doesn’t need to depend on internet connection to work or not. I want to open the lid for 5 seconds when the ultrasonic sensor senses a distance below 50 cm. After that, I want to close the lid. Below the code. It opens the lid when the distance is below 50 cm but it does not close the lid. I can manually control the servo using HA.
script:
- id: activate_lid
then:
- logger.log: "Script exeduted"
- servo.write:
id: ${devicename}_servo
level: -100%
- lambda: 'id(servo_value).publish_state(-100);'
- logger.log: "Servo at -100"
- delay: 5s
- logger.log: "passed delay"
- servo.write:
id: ${devicename}_servo
level: 100%
- lambda: 'id(servo_value).publish_state(100);'
- logger.log: "Servo at 100"
- servo.detach: ${devicename}_servo
- logger.log: "Servo detached"
- platform: template
name: "${devicename_friendly} Servo Value"
id: servo_value
update_interval: 500ms
- platform: ultrasonic
id: ultrasonic_sensor5
trigger_pin: GPIO12 #D6
echo_pin: GPIO04 #D2
name: ${devicename_friendly} Distance
update_interval: 1s
filters:
filter_out: nan
timeout: 9m
on_value_range:
- below: 0.5
then:
- if:
condition:
not:
script.is_running: activate_lid
then:
- script.execute: activate_lid
Thanks in advance for your help