Count changes within time frame for binary_sensor

Hi everybody,

I have a nodemcu v2 with this configuration below.

Is it possible to achieve the following without writing a custom_component? (will explain what this is for underneath the yaml code)

  1. when binary_sensor.counter (from code below) switches from off to on
  • set a temporary number to 1
  • see, how many more times this sensor switches from off to on within 500ms
  • for each time it does (within this time frame), increase the temporary value by 1
  • after this 500ms time frame
    • return this value (it will be between 1 and 5)
    • reset this value to 0
  1. depending on what this temporary number has counted to before returning to 0
  • if 1 > set input_number.current_value to 2
  • if 2 > set input_number.current_value to 1
  • if 3 > set input_number.current_value to 0.5
  • if 4 > set input_number.current_value to 0.2
  • if 5 > set input_number.current_value to 0.1
  1. whenever input_number.current_value changes
  • add its value (2 / 1 / 0.5 / 0.2 or 0.1) to input_number.total_value
  1. (I guess I can do this through Home Assistant)
  • manually reset input_number.total_value via button_press…

I know there is delay and interval, but I just cannot wrap my head around complex ESPHome code.

Thank you in advance for your help.

esphome:
  name: testcoinslot
  friendly_name: 00/Test/Coinslot

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "asdasdsad"

ota:
  password: "asdasdsa"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Testcoinslot Fallback Hotspot"
    password: "asdasdasd"

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: D4
      inverted: True
    name: "Counter"

Why?

I have a CH-926 coin slot, which will send a different amount of pulses depending on what coin has been inserted. It will send 1 pulse for a 2 EUR coin, 2 for a 1 EUR coin, etc. to 5 pulses for 10 Cents.

These pulses are just on/off switches within a very short time frame. I have tried using pulse counter etc. for this, but it didn’t seem to work.

Then I found an arduino code example, but it seems impossible to integrate it into ESPHome as a custom_component (because I just don’t know enough about it).

The workflow I listed above is an analogue way to achieve what the arduino code would do. Always check how many times the signal has been turned from low to high within a set period of time (they use 30ms, I thought I’d use 50ms instead).


EDIT:

I tried the code below as well.

sensor:
  - platform: pulse_meter
    name: "D6"
    pin: D6
    timeout: 1s
    internal_filter: 20ms

This, however, will not produce the desired output. When I expect one single pulse, I get 500.000 pulses/min. This can be filtered via multiply: 0.06, but then I get values between 9.nnn to 27.nnn.

I need to start measuring pulses when the first pulse occurs, then count them, then stop. No average per minute. If I understand correctly, the device should send 1,2,3,4, or 5 pulses. Not 9 dot something, not 18 dot something. 1 to 5. These should somehow be registered / identified. But I am just getting lost more and more here.