Detect a blinking LED using adc platform and a light dependent resistor - avoid flapping state

Hi all,
given an existing non-smart doorbell (Busch Welcome), I’d like to sense using ESPHome in HA. I cannot solder anything on the doorbell, but using a light dependent resistor (LDR) in front of the doorbell’s LED that blinks when a visitor rings and using the following sensor config works ok. By default the indicating LED of the doorbell is off, only when someone rings, it blinks for about 5-6 seconds.

sensor:
  - platform: adc
    pin: GPIO34  # Use an ADC pin on the ESP32 (e.g., GPIO34)
    name: "doorbell LED Light Intensity"
    update_interval: 0.5s
    filters:
      - lambda: |-
          if (x > 0.7) {
            return 1.0;
          } else {
            return 0.0;
          }
    unit_of_measurement: "state"
    icon: "mdi:lightbulb"

When someone rings, the monitored LED blinks at approx. 1-2 Hertz (~1 second on, ~1 second off) which is why I sense with an update_interval of 0.5. Of course, as a result, the state is flapping between 0 and 1, depending on whether the LED is currently on or off when sensed, as reflected in the log:

[22:03:59][D][sensor:094]: 'doorbell LED Light Intensity': Sending state 0.00000 state with 2 decimals of accuracy
[22:04:00][D][sensor:094]: 'doorbell LED Light Intensity': Sending state 1.00000 state with 2 decimals of accuracy
[22:04:00][D][sensor:094]: 'doorbell LED Light Intensity': Sending state 0.00000 state with 2 decimals of accuracy
[22:04:01][D][sensor:094]: 'doorbell LED Light Intensity': Sending state 1.00000 state with 2 decimals of accuracy

Ideally, I’d like the following: When at least one blink is sensed, have the state at 1 for say 5 seconds, and go back to 0 if no more blink came in.

How can this be achieved?

Thanks in advance,
Chris

If you make a separate binary sensor and make it’s state reference your original sensor, you could simply use the delayed off filter:

Better yet, use this instead of your existing sensor:

1 Like

Thanks a lot! This pointed me in the right direction. Just fyi, the final solution looks as follows:

sensor:
  - platform: adc
    pin: GPIO34  # Use an ADC pin on the ESP32 (e.g., GPIO34)
    name: "doorbell LED Light Intensity"
    id: doorbell_led_light
    update_interval: 0.5s
    filters:
      - delta: 0.2  # Only yield values if the change is greater than 0.2
    icon: "mdi:lightbulb"

binary_sensor:
  - platform: analog_threshold
    name: "doorbell-switch"
    id: doorbell_switch
    sensor_id: doorbell_led_light
    threshold: 0.7   # Consider ON when the LDR level exceeds 0.7
    filters:
      - delayed_off: 5s   # Keep the ON state for up to 5 seconds