Sensor data is not received at random from HA either via the API or MQTT

Hello!

My current issue is with the frequency and inconsistency of the received sensor data in my instance. This happens regardless if I use the API or MQTT.
The idea behind the sensor is every time a metal part moves in front of a laser sensor it will count the time between the last and this.

The code that is running:

esphome:
  name: esp-sensor4
  friendly_name: esp-sensor4

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

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

# Over-the-Air updates
ota:
  - platform: esphome
    password: "xxxx"

# WiFi Configuration
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp-Sensor4 Fallback Hotspot"
    password: "xxxx"

captive_portal:

# MQTT Configuration
mqtt:
  broker: 192.168.0.2
  username: !secret mqtt_username
  password: !secret mqtt_password

# Binary Sensor Configuration
binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO4
      inverted: False
      mode:
        input: True
        pullup: true
    name: "Sensor 4"
    qos: 2
    retain: true
    on_press:
      then:
        - lambda: |-
            static uint32_t last_press = 0;
            uint32_t now = millis();
            if (last_press != 0) {
                uint32_t diff = now - last_press;
                ESP_LOGD("main", "Time between button presses: %u ms", diff);
                if (diff > 60000) {  // 60 seconds in milliseconds
                    id(time_between_presses_1).publish_state(0);
                } else {
                    id(time_between_presses_1).publish_state(diff / 1000.0);
                }
            }
            last_press = now;

# Sensor Configuration
sensor:
  - platform: template
    name: "Sensor 4 - time between"
    id: time_between_presses_1
    unit_of_measurement: 's'
    accuracy_decimals: 2
    update_interval: never
    qos: 2
    retain: true

  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 15s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"
    device_class: ""

Usually, the time it takes between pulses is around 9 seconds. I want to have all the data and it bugs me why some of them are missing.
My esp is properly powered up (constant 5V), there is a direct line of sight between my router and the esp and the machine, that is hosting HA is sufficiently powerful.

Below you can see what I’m talking about with the randomness.

And here is the log from Esphome:

[13:49:43][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:49:43][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:49:44][D][main:060]: Time between button presses: 9057 ms
[13:49:44][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.05700 s with 2 decimals of accuracy
[13:49:52][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:49:53][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:49:53][D][main:060]: Time between button presses: 9078 ms
[13:49:53][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.07800 s with 2 decimals of accuracy
[13:49:56][D][sensor:094]: 'WiFi Signal dB': Sending state -62.00000 dBm with 0 decimals of accuracy
[13:49:56][D][sensor:094]: 'WiFi Signal Percent': Sending state 76.00000 Signal % with 0 decimals of accuracy
[13:50:01][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:02][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:02][D][main:060]: Time between button presses: 9189 ms
[13:50:02][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.18900 s with 2 decimals of accuracy
[13:50:10][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:11][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:11][D][main:060]: Time between button presses: 9115 ms
[13:50:11][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.11500 s with 2 decimals of accuracy
[13:50:11][D][sensor:094]: 'WiFi Signal dB': Sending state -57.00000 dBm with 0 decimals of accuracy
[13:50:11][D][sensor:094]: 'WiFi Signal Percent': Sending state 86.00000 Signal % with 0 decimals of accuracy
[13:50:19][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:20][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:20][D][main:060]: Time between button presses: 9099 ms
[13:50:20][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.09900 s with 2 decimals of accuracy
[13:50:26][D][sensor:094]: 'WiFi Signal dB': Sending state -58.00000 dBm with 0 decimals of accuracy
[13:50:26][D][sensor:094]: 'WiFi Signal Percent': Sending state 84.00000 Signal % with 0 decimals of accuracy
[13:50:28][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:29][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:29][D][main:060]: Time between button presses: 9101 ms
[13:50:29][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.10100 s with 2 decimals of accuracy
[13:50:38][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:38][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:38][D][main:060]: Time between button presses: 9087 ms
[13:50:38][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.08700 s with 2 decimals of accuracy
[13:50:41][D][sensor:094]: 'WiFi Signal dB': Sending state -60.00000 dBm with 0 decimals of accuracy
[13:50:41][D][sensor:094]: 'WiFi Signal Percent': Sending state 80.00000 Signal % with 0 decimals of accuracy
[13:50:47][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:47][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:47][D][main:060]: Time between button presses: 9092 ms
[13:50:47][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.09200 s with 2 decimals of accuracy
[13:50:56][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:50:56][D][sensor:094]: 'WiFi Signal dB': Sending state -60.00000 dBm with 0 decimals of accuracy
[13:50:56][D][sensor:094]: 'WiFi Signal Percent': Sending state 80.00000 Signal % with 0 decimals of accuracy
[13:50:56][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:50:56][D][main:060]: Time between button presses: 9090 ms
[13:50:56][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.09000 s with 2 decimals of accuracy
[13:51:05][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:51:05][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:51:05][D][main:060]: Time between button presses: 9073 ms
[13:51:05][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.07300 s with 2 decimals of accuracy
[13:51:11][D][sensor:094]: 'WiFi Signal dB': Sending state -58.00000 dBm with 0 decimals of accuracy
[13:51:11][D][sensor:094]: 'WiFi Signal Percent': Sending state 84.00000 Signal % with 0 decimals of accuracy
[13:51:14][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:51:15][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:51:15][D][main:060]: Time between button presses: 9196 ms
[13:51:15][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.19600 s with 2 decimals of accuracy
[13:51:23][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:51:24][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:51:24][D][main:060]: Time between button presses: 9101 ms
[13:51:24][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.10100 s with 2 decimals of accuracy
[13:51:26][D][sensor:094]: 'WiFi Signal dB': Sending state -57.00000 dBm with 0 decimals of accuracy
[13:51:26][D][sensor:094]: 'WiFi Signal Percent': Sending state 86.00000 Signal % with 0 decimals of accuracy
[13:51:32][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:51:33][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:51:33][D][main:060]: Time between button presses: 9194 ms
[13:51:33][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.19400 s with 2 decimals of accuracy
[13:51:41][D][sensor:094]: 'WiFi Signal dB': Sending state -58.00000 dBm with 0 decimals of accuracy
[13:51:41][D][sensor:094]: 'WiFi Signal Percent': Sending state 84.00000 Signal % with 0 decimals of accuracy
[13:51:41][D][binary_sensor:036]: 'Sensor 4': Sending state OFF
[13:51:42][D][binary_sensor:036]: 'Sensor 4': Sending state ON
[13:51:42][D][main:060]: Time between button presses: 9088 ms
[13:51:42][D][sensor:094]: 'Sensor 4 - time between': Sending state 9.08800 s with 2 decimals of accuracy

I have switched to MQTT to see if the issue was related to dropped packets or something, but even with QoS=2 and retain=true there are still missed entries.
Also, in a moment of desparation I’ve enabled both API and mqtt and there was an entry missing with both of them.

I would be more than happy is someone point me somewhere else to look.