Tri-State Binary Senor

Hello Fellow ESP-Homers,
I am busy with a gate project so that I can control and monitor my gates. I am using a Sonoff SV, using GPIO 4+5 (SDA/SCL) with a PCF8574. I have connected a 3channel relay board to the PCF8574 (P0;P1;P2 (output mode)) for controlling the gate to open/close - pedestrian open - close disable. P5;P6;P7 (input mode) are used as binary_sensors for the gate status. Let me elaborate the latter …
P7 = Status ;;; P6 = Alarm ;;; P5 = Clutch Released.

P7 is the “Tri_State” related to this query here posted.
State=0 is the state given when the gate is fully closed;
State=1 is the state given when the gate is not closed (i.e. fully open or partially open);
State=0-1-0-1-0-1-0-1 … (and so on) when the gate is in motion (moving). The toggling of this state is 0.5s on ; 0.5s off ; 0.5s on ; 0.5s off ; … and so onward until the gate comes to rest where State 0 or State 1 will be reported. I would like to have the three state as: Open ; Closed ; Moving.

I hope I have given enough info here above.
How does one write the yaml on the ESP for the state of P7?

Here is the yaml I have done currently -

### Individual inputs on PCF8574 ###
  - platform: gpio
    name: "PCF8574 InputPin #7"
    id: pin7
    internal: false
    pin:
      pcf8574: pcf8574_hub
      number: 7
      mode: input
      inverted: false

  - platform: gpio
    name: "PCF8574 InputPin #6"
    id: pin6
    internal: false
    pin:
      pcf8574: pcf8574_hub
      number: 6
      mode: input
      inverted: false

  - platform: gpio
    name: "PCF8574 InputPin #5"
    id: pin5
    internal: false
    pin:
      pcf8574: pcf8574_hub
      number: 5
      mode: input
      inverted: false

Any help will be highly appreciated. Thanks in advance.
Brian.

You could use a Global variable to hold the gate-state as an integer, for use elsewhere in your logic.
e.g. 1=open, 2=closed, 3=moving.

To get the state will require you to observe the pin’s state over time. i.e. if 0 for >0.5S, set state to 2 (closed). If 1 for >0.5S, set state to 1 (open). Since the only other state it could be in if neither of the others are true, set state to 3 (moving).

To measure those delays, you might try using an ‘Interval’ trigger, some if/then/else logic, and another global variable to remember the last time the P7 pin state changed, and what it changed to.
Then test: is P7 in the same state as I last saw, and has it been that way more than 1 second?
You probably don’t want to declare it open or closed until you’ve seen the P7 in the same state for two consecutive 0.5S intervals. And, you’ll want to account for a certain amount of slop in that interval, as the motor might run slightly faster/slower depending on weather, obstructions, etc.