Garage Parking aid YAML

I have been trying to use some of the posts on the forums to get this working for a while now and I can’t seem to get the YAML right. I corrected some alignment issues but to be honest I am at a bit of a loss right now. ESPHome has a issue with line 56, but I don’t see the problem based on the other examples in the forum.

esphome:
  name: esp55

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

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

ota:
  password: "##############"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
# Optional manual IP
  manual_ip:
    static_ip: 172.16.7.11
    gateway: 172.16.7.1
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fallback Hotspot"
    password: "XXXX"

captive_portal:
    
output:
  - platform: gpio
    id: red_led_van_sensor
    pin: GPIO18    
    name: "Red LED"

  - platform: gpio
    pin: GPIO19
    id: yellow_led_van_sensor
    name: "Yellow LED"

sensor:
  - platform: ultrasonic
    name: parking_sensor
    trigger_pin: GPIO27
    echo_pin: GPIO26
    accuracy_decimals: 3
    update_interval: 1ms
    timeout: 4.0m
    pulse_time: 10us
    on_value:
      then: 
        - if: 
            # Turn On Yellow from 3m away until 0.10m away 
            condition:
              sensor.in_range:
                id: parking_sensor
                above: 0.10
                below: 3
            then:
              output.turn_on:
                id: yellow_led_van_sensor
        - if:
            # Turn On Red for anything beleow 0.10m
            condition:
              sensor.in_range:
                id: parking_sensor
                below: 0.10
            then:
              output.turn_on:
                id: red_led_van_sensor

# Turn off LEDs when outside range 
       - if: 
            # Turn Off Red if more than 0.1m
            condition: 
              sensor.in_range: 
                id: parking_sensor
                above: 0.1
            then:
              output.turn_off:
                id: red_led_van_sensor
        
        - if: 
            #Turn off Yellow if less than 1.1m
            condition: 
              sensor.in_range: 
                id: parking_sensor
                below: 1.1
            then:
              output.turn_off:
                id: yellow_led_van_sensor
 
# Turn off LEDs when car leaves
       - if: 
            #Turn off Yellow if more than 3m
            condition: 
              sensor.in_range: 
                id: parking_sensor
                above: 3
            then:
              output.turn_off:
                id: yellow_led_van_sensor
        - if: 
            #Turn off Red if more than 0.1m
            condition: 
              sensor.in_range: 
                id: parking_sensor
                above: 0.1
            then:
              output.turn_off:
                id: red_led_van_sensor				

What issue?

Which line is 56?

ESPHome says “while parsing a block mapping”. The line it complains about is the first then: statement below.

sensor:
  - platform: ultrasonic
    name: parking_sensor
    trigger_pin: GPIO27
    echo_pin: GPIO26
    accuracy_decimals: 3
    update_interval: 1ms
    timeout: 4.0m
    pulse_time: 10us
    on_value:
      then: 
        - if: 
            # Turn On Yellow from 3m away until 0.10m away 
            condition:
              sensor.in_range:
                id: parking_sensor
                above: 0.10
                below: 3
            then:
              output.turn_on:
                id: yellow_led_van_sensor
        - if:
            # Turn On Red for anything beleow 0.10m
            condition:
              sensor.in_range:
                id: parking_sensor
                below: 0.10
            then:
              output.turn_on:
                id: red_led_van_sensor

Pretty sure you need to indent everything from on_value to the right by 2 spaces.

From the docs:

sensor:
  - platform: dht
    humidity:
      name: "Living Room Humidity"
      on_value_range:
        - above: 65.0
          then:
            - switch.turn_on: dehumidifier1
        - below: 50.0
          then:
            - switch.turn_off: dehumidifier1

I am still missing something. I shifted everything to the right and even installed VS to get the indentations right but no dice. Is there any way to simplify the code for what I am trying to do? Seems like a lot of code for 2 LEDs.

What is the error now? Same?

You have two -if statements misaligned for starters - the ones after the “Turn off LEDS” comments.

Before:

# Turn off LEDs when outside range 
       - if: 
            # Turn Off Red if more than 0.1m
            condition: 

Fixed:

# Turn off LEDs when outside range 
        - if: 
            # Turn Off Red if more than 0.1m
            condition: 

Same for the other one

Pretty sure you can’t have two if: statements in the same automation block in ESPhome.

It’s a case of:

if: do something
else: do something

not
if: do something
if: do something different
else: do some other thing

You may be right - all mine are nested if - else blocks.

How are these 2 code snippets different? I understand what you are saying about the alignment but it looks like there are 4 spaces indented on the condition statement on both?