Moving automation from HA to ESPHome

Hello,
I have an ESP32 with ESPHome where I have two PIRs connected.
I now have an automation in HA that detects the walking direction of a person by the PIRs, of which one is mounted on the outside of a room door and the other on the inside.

It does this by detecting which of the PIRs triggered first, and then waiting for the other one to trigger within 5 seconds, if it does, it increments (if the outer sensor triggered first) or decrements (if the inner one triggered first) an input_number (which I would replace with a template number on the ESP).

HA Automation code

I currently have the following Home assistant Automation code:

alias: PZ Ergänzung
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_personcounter_bwm_aussen
    id: Aussen
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.door_personcounter_bwm_innen
    id: Innen
    to: "on"
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.door_personcounter_bwm_innen
        state: "on"
      - condition: state
        state: "on"
        entity_id: binary_sensor.door_personcounter_bwm_aussen
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Aussen
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: binary_sensor.door_personcounter_bwm_innen
                    state: "off"
                sequence:
                  - wait_for_trigger:
                      - platform: state
                        entity_id:
                          - binary_sensor.door_personcounter_bwm_innen
                        for:
                          hours: 0
                          minutes: 0
                          seconds: 0
                        from: "off"
                        to: "on"
                    continue_on_timeout: false
                    timeout:
                      hours: 0
                      minutes: 0
                      seconds: 10
                      milliseconds: 0
                  - service: input_number.increment
                    data: {}
                    target:
                      entity_id: input_number.pz_pirs
      - conditions:
          - condition: trigger
            id: Innen
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: binary_sensor.door_personcounter_bwm_aussen
                    state: "off"
                sequence:
                  - wait_for_trigger:
                      - platform: state
                        entity_id:
                          - binary_sensor.door_personcounter_bwm_aussen
                        for:
                          hours: 0
                          minutes: 0
                          seconds: 0
                        from: "off"
                        to: "on"
                    continue_on_timeout: false
                    timeout:
                      hours: 0
                      minutes: 0
                      seconds: 10
                      milliseconds: 0
                  - service: input_number.decrement
                    data: {}
                    target:
                      entity_id: input_number.pz_pirs
mode: restart

Could you please help me with how to do that in ESPHome (directly on the ESP with the sensors) as I don’t quite understand the ESPHome way of creating automations like this?

Best regards
Aaron

Possibly the dodgiest way, but I think this would work:

binary_sensor:
  - platform: gpio
    pin: GPIO4
    id: door_personcounter_bwm_innen
    device_class: motion
    on_press:
      - script.execute: entry
      - if:
          condition:
            - script.is_running: exit
          then:
            - number.decrement: personcounter
            - script.stop: exit

  - platform: gpio
    pin: GPIO5
    id: door_personcounter_bwm_aussen
    device_class: motion
    on_press:
      - script.execute: exit
      - if:
          condition:
            - script.is_running: entry
          then:
            - number.increment: personcounter
            - script.stop: entry

number:
  - platform: template
    name: "People Inside"
    id: personcounter
    optimistic: true
    min_value: 0
    max_value: 9999
    step: 1

script:
  - id: entry
    mode: restart
    then:
      - delay: 5s

  - id: exit
    mode: restart
    then:
      - delay: 5s

I think I have the logic right.

Note that this really doesn’t handle more than one person at a time. Not even sure how you would code that logic.

Could I also use the wait_until action?

Ok - so that has a timeout. But how do you tell if the sensor triggered within the 5 seconds or it just timed out? Either way it just continues to the next step. Somehow you need to test if it times out or was triggered by the sensor. You will need to code it up and show the logic you would use.

The example they give sort of illustrates the problem:

  - wait_until:
      condition:
        binary_sensor.is_on: some_binary_sensor
      timeout: 8s
  - logger.log: "Binary sensor might be ready"

“Binary sensor might be ready”

@zoogara I would place an if block after the wait block, so that it only continues (and thus increments/decements the counter) if the sensor is triggered

Something like this People counter in a room that 3ATIVE did?

Yes, something like that, except that my setup uses a combined approach using the double-PIR method and a ToF sensor which both independently count people and give me an alert if one of them counted wrongly…
This looks quite messy, but it works really well…