Need help with nested If statements and state conditions

Hi. I’m building a parking aid for my garage which consists of a nodemcuv2, hc-sr04 ultrasonic sensor, and a strip of ws2812b LEDs.

I’ve managed to get it working but I have 2 remaining issues.

I currently have a lambda which contains multiple “if” statements to trigger the led strip to light up different colors depending on the ultrasonic reading. However, I only want this block of code to be triggered if the garage door is open. In other words nothing should happen if the garage door is closed. I believe the best way to accomplish this would be nested if statements but I’m having a hard time trying to figure out the proper way to code it. I’m beginning to question if this is even the best way to handle this task altogether. The garage door sensor is a Smartthings multipurpose sensor and is a binary “opening” type sensor that appears to be “off” when closed and “on” when open. My code compiles just fine but it doesn’t seem to respond to the garage door opening or closing.

The 2nd issue I’m having is with the strobe effect. Whenever I try implementing the effect the LED strip gets stuck on flashing red at all distance levels. I kept the effect info at the bottom of my code but removed the command to call it due to the issues. Does the effect code look fine?

Thanks

binary_sensor:
 - platform: status
   name: "Car Sensor 1 Status"
   
 - platform: homeassistant
   entity_id: binary_sensor.sheree_garage_door_sensor_contact
   id: doorsensor1

 - platform: template
   name: "Vehicle1"
   device_class: presence
   lambda: |-
     if ((id(car_sensor1).state < 1.2)){
        // vehichle is in the garage
        return true;
     } else {
         //no vehicle
         return false;
     }

sensor:
  - platform: ultrasonic
    id: car_sensor1
    trigger_pin: D4
    echo_pin: D3
    update_interval: 0.5s
    timeout: 4.0m
    accuracy_decimals: 1
    name: "Car Sensor 1"
    filters:
      filter_out: nan
      
    on_value:
      then:
        - if:
            condition:
              binary_sensor.is_off: doorsensor1
            then:
              - lambda: |-
                  if ((id(car_sensor1).state >= 4.0)) {
                    id(parkingled1).turn_off();
                  }
                  else if ((id(car_sensor1).state >= 1.7) & (id(car_sensor1).state < 4.0)) {
                    auto call = id(parkingled1).turn_on();
                    call.set_rgb(0,1.0,0);
                    call.perform();
                  }
                  else if ((id(car_sensor1).state >= 1.0) & (id(car_sensor1).state < 1.7)) {
                    auto call = id(parkingled1).turn_on();
                    call.set_rgb(1.0,0.75,0);
                    call.perform();
                  }
                  else if ((id(car_sensor1).state >= 0.7) & (id(car_sensor1).state < 1.0)) {
                    auto call = id(parkingled1).turn_on();
                    call.set_rgb(0,0,1.0);
                    call.perform();
                  }
                  else if ((id(car_sensor1).state < 0.7)) {
                    auto call = id(parkingled1).turn_on();
                    call.set_rgb(1.0,0,0);
                    call.perform();
                  }


  - platform: wifi_signal
    name: "Garage Vehicle Sensor 1 WiFi"
    update_interval: 60s
    


light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: GPIO12
    num_leds: 8
    rgb_order: GRB
    name: "Car Sensor 1 LED"
    restore_mode: ALWAYS OFF
    default_transition_length: 0s
    id: parkingled1
    effects:
      - strobe:
      - strobe:
          name: Red Strobe
          colors:
            - state: True
              brightness: 100%
              red: 100%
              green: 0%
              blue: 0%
              duration: 100ms
            - state: False
              duration: 100ms
1 Like

Anybody got any ideas?

To simplify what I’m asking…
How do I import the “binary_sensor.sheree.garage_door_sensor_contact” sensor state into an if statement so that the whole automation is triggered by the state of the garage door being “open/on”?

if you go to your Developer Tools and look down the state’s list does binary_sensor.sheree.garage_door_sensor_contact / binary_sensor.doorsensor show up? and if so when you activate it does the state change?

I got it! I just needed two if statements. I still haven’t fixed the strobe issue but I figured out the main problem.

binary_sensor:
 - platform: status
   name: "Car Sensor 2 Status"
   
 - platform: homeassistant
   entity_id: binary_sensor.chris_garage_door_sensor_contact
   id: doorsensor2
   
 - platform: template
   name: "Chris Vehicle"
   device_class: presence
   lambda: |-
     if ((id(car_sensor2).state < 1.2)){
        // vehichle is in the garage
        return true;
     } else {
         //no vehicle
         return false;
     }

sensor:
  - platform: ultrasonic
    id: car_sensor2
    trigger_pin: D4
    echo_pin: D3
    update_interval: 0.5s
    timeout: 4.0m
    name: "Car Sensor 2"
    filters:
      filter_out: nan
      
    on_value:
      then:
        - if:
            condition:
              binary_sensor.is_on: doorsensor2
            then:
              - lambda: |-
                  if ((id(car_sensor2).state >= 2.00)) {
                    id(parkingled2).turn_off();
                  }
                  else if ((id(car_sensor2).state >= 1.00) & (id(car_sensor2).state < 2.00)) {
                    auto call = id(parkingled2).turn_on();
                    call.set_rgb(0,1.0,0);
                    call.perform();
                  }
                  else if ((id(car_sensor2).state >= 0.28) & (id(car_sensor2).state < 1.00)) {
                    auto call = id(parkingled2).turn_on();
                    call.set_rgb(1.0,0.75,0);
                    call.perform();
                  }
                  else if ((id(car_sensor2).state >= 0.18) & (id(car_sensor2).state < 0.28)) {
                    auto call = id(parkingled2).turn_on();
                    call.set_rgb(0,0,1.0);
                    call.perform();
                  }
                  else if ((id(car_sensor2).state < 0.18)) {
                    auto call = id(parkingled2).turn_on();
                    call.set_rgb(1.0,0,0);
                    call.perform();
                  }
                  else {
                  id(parkingled2).turn_off();
                  }
        - if:
            condition:
              binary_sensor.is_off: doorsensor2
            then:
              - lambda: |-
                  id(parkingled2).turn_off();

this could be widely wrong, but does && work? i prefer to use appdaemon for the python as yaml is a pain to do anything

I’m still very new to this and coding in general. & seems to work but I had issues trying to code the additional if statements based on an external sensor. It’s incredibly difficult to find coding examples for esphome yaml files.

Try reviewing the ESPHome Cookbook page.

1 Like

did you figure out the two if conditions issue? i am struggling for the same …can you help please