Binary Sensor Template with Wait Until

I have two sensors (ToF VL53L0X) and I’m incrementing a counter based on which sensor triggers first, and then waiting to see if the second one triggers within a short time period. Is this the best way to do this, or is there a better way using lambda? Thank you!

binary_sensor:
  - platform: template
    name: "Breakbeam Sensor"
    id: breakbeam_sensor
    device_class: motion
    publish_initial_state: True
    on_state:
      - if:
          condition:
            - lambda: return (id(breakbeam_sensor).state == true && id(breakbeam_sensor2).state == false);
          then:
            - wait_until:
                condition:
                  - lambda: return id(breakbeam_sensor2).state == true;
                timeout: 1s
            - if:
                condition:
                  - lambda: return id(breakbeam_sensor2).state == true;
                then:
                  - number.decrement:
                      id: people_counter
                      cycle: false
  - platform: template
    name: "Breakbeam Sensor2"
    id: breakbeam_sensor2
    device_class: motion
    publish_initial_state: True
    on_state:
      - if:
          condition:
            - lambda: return (id(breakbeam_sensor2).state == true && id(breakbeam_sensor).state == false);
          then:
            - wait_until:
                condition:
                  - lambda: return id(breakbeam_sensor).state == true;
                timeout: 1s
            - if:
                condition:
                  - lambda: return id(breakbeam_sensor).state == true;
                then:
                  - number.increment:
                      id: people_counter
                      cycle: false

The 2 conditions in a row are redundant. It will never get to if unless the wait until condition is satisfied. It can be reduced to.

          then:
            - wait_until:
                condition:
                  - lambda: return id(breakbeam_sensor2).state == true;
                timeout: 1s
            - number.decrement:
                      id: people_counter
                      cycle: false

Define “best way”. Why would lambda make it better? Probably opposite, since blocking code (like wait until) is not allowed with lambdas.
Depending on your actual setup and use case, there are multiple ways of getting what you are after, the question is, which suites your case the best.

There were other forums about this needing to be a feature request, since the wait_until will timeout, and then continue on with the code and there was no way to verify that the condition was actually met. I was certain that the code was acting this way, so I added in the “extra” check to see that it hit after the while condition. I also added in the timeout because I fully expect the second sensor to trigger within 1s, otherwise I didn’t want it sitting there forever waiting.

Thanks! I have two ToF sensors on either side of my ESP board. Essentially I’m using two sensors to sense directionality based on which sensor triggers first, and then increase a counter. When counter > 0, light turns on. When counter < 1, light turns off. It works great now with the code above. I have a comparison right now between this internal counter, and an automation using a helper counter. Both seem to be fairly stable, but just wanted a sanity check. Thanks again!

I personally would consider that as “best way”. Working great and already done… :wink:

1 Like