Binary Sensor for a Wire contact for roller shutter

I have a contact like this

this is my config

i2c:
  sda: 14
  scl: 13
  scan: true

pcf8574:
  - id: 'pcf8574_hub'
      address: 0x20
      pcf8575: true

binary_sensor:
  - platform: gpio
    name: "Shutter"
    device_class: moving
    icon: mdi:window-shutter-alert
    pin:
      pcf8574: pcf8574_hub
      number: 4
      mode:
        input: true
      inverted: false
    internal: false

[18:58:34][D][binary_sensor:036]: ‘Shutter’: Sending state ON
[18:58:34][D][binary_sensor:036]: ‘Shutter’: Sending state OFF
[18:58:35][D][binary_sensor:036]: ‘Shutter’: Sending state ON
[18:58:35][D][binary_sensor:036]: ‘Shutter’: Sending state OFF

How I can configure the sensor to remain ON for some seconds (e.g. 5s) only if the contact “counts” 3 ON/OFF cycles (aka 3 “clicks”)

or there is a better way to achive my goal?

I have no idea what that contact sensor actually is nor do you have a wiring diagram/schematic, but I’d guess that you likely need a pullup (or down) resistor.

Also, you don’t have your YAML quite formatted correctly, so it’s hard to read (and tell if you’re doing anything wrong). Start and end it with ```

Sorry, I fixed the code, this is the datasheet

I connected one wire to GND and one to the GPIO

The on_multi_click action should do what you want.

For a tracking the number of pulses from a NC micro-switch?

Wouldnt pulse counter be better here? This is basically the type of scenario it was added for and it easily distinguishes between directions of movement. How would you track motion in 2 directions (clockwise/counterclockwise) with binary_sensor?

It explains this exact use case in the copy from the data sheet OP shared. Idk why no one reads those things and instead run away like data sheets are scary.

The pulse counter accumulates the total number of pulses, the OP was asking about detecting a sequence of 3 pulses, presumably within a given time window. You could probably do that with pulse_counter and some filters, but it’s not the intended use case.

As far as direction goes, there is no way you can determine direction from a single microswitch, you would need a rotary encoder for that.