Hello
I am not sure if what I am trying to do is possible, I am trying to control the Sonoff Basic Relay (flashed with ESPhome) automatically via un ultrasonic sensor with external 5V power and both the trigger and echo pins using GPIO 1 and GPIO 3, there is no problem up to this moment and the YAML file validates, compiles and uploads fine. this is the successful YAML file under ESPhome
esphome:
name: right_garage_door_motor
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret home_wifi_ssid
password: !secret home_wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Right Garage Door Motor"
password: "***********"
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: "Right Garage Door Motor Safety Switch"
on_press:
- switch.toggle: fakebutton
- platform: template
name: "Right Door Obstacle"
device_class: presence
lambda: |-
if (id(ultrasonic_sensor2).state < 1.5) {
// Obstacle is in the way
return true;
} else {
// no obstacle
return false;
}
switch:
- platform: template
name: "Right Garage Door Motor"
optimistic: true
id: fakebutton
turn_on_action:
- switch.turn_on: relay
- light.turn_off: led
turn_off_action:
- switch.turn_off: relay
- light.turn_on: led
- platform: gpio
id: relay
pin: GPIO12
output:
- platform: esp8266_pwm
id: basic_green_led
pin:
number: GPIO13
inverted: True
light:
- platform: monochromatic
name: "Right Garage Door Safety LED"
output: basic_green_led
id: led
default_transition_length: 10ms
sensor:
- platform: ultrasonic
name: "Right Door USonic"
id: ultrasonic_sensor2
trigger_pin: GPIO01
echo_pin: GPIO03
update_interval: 1s
filters:
filter_out: nan
timeout: 9m
icon: mdi:arrow-expand-horizontal
What I want to do is simply control the relay automatically (in addition to the manual control above) based on the distance returned by the ultrasonic sensor, see this section below
- platform: template
name: "Right Door Obstacle"
device_class: presence
lambda: |-
if (id(ultrasonic_sensor2).state < 1.5) {
// Obstacle is in the way
return true;
// I want to turn the RELAY OFF and the LED ON in here //
} else {
// no obstacle
return false;
// I want to turn the RELAY ON and the LED OFF in here //
}
I tried many things but all fail to compile, not strangely due to my lack of this language
Can you help me out please with the commands I need to insert in the function above to achieve that
Your help will be very much appreciated
Cheers