Detecting if doorbell rang multiple times in few seconds

I have a esphome device attached to my old doorbell. In the bell I have a “spring thing” that acts as switch. Then I have a relay that can disconnect the doorbell push button.

I’m trying to create a automation that disables the doorbell if it has already pressed multiple times (children you know).

I cant get this to work, can anyone see whats wrong? This triggers after four presses but the deactivation part does not work.

alias: Doorbell disable if rang multiple times
description: Disable the doorbell for 30s if rang more than four times in the last 10s
trigger:
  - platform: state
    entity_id: binary_sensor.doorbell_doorbell_hammer
    to: "on"
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"
  - condition: template
    value_template: >-
      {{ as_timestamp(now()) - as_timestamp(trigger.from_state.last_changed) <
      10 }}
action:
  - condition: numeric_state
    entity_id: counter.doorbell_ring_count
    above: 4
    enabled: true
  - service: switch.turn_on
    target:
      entity_id: switch.doorbell_doorbell_deactivation
    data: {}
    enabled: true
  - delay:
      seconds: 30
  - service: switch.turn_off
    target:
      entity_id: switch.doorbell_doorbell_deactivation
    data: {}
    enabled: true
  - service: counter.reset
    entity_id: counter.doorbell_ring_count
mode: single

I also have this counter in the configuration.yaml

counter:
  doorbell_ring_count:
    initial: 0

How are you incrementing the counter? What does the automation trace show when it should have ran?

Ah, yes I also have a following file:
scripts/deactivate_doorbell.yaml

deactivate_doorbell:
  alias: Deactivate Doorbell
  sequence:
    - service: counter.increment
      entity_id: counter.doorbell_ring_count
    - condition: numeric_state
      entity_id: counter.doorbell_ring_count
      above: 4
    - service: switch.turn_on
      target:
        entity_id: switch.doorbell_doorbell_deactivation
    - delay:
        seconds: 30
    - service: switch.turn_off
      target:
        entity_id: switch.doorbell_doorbell_deactivation
    - service: counter.reset
      entity_id: counter.doorbell_ring_count

I haven’t used automation trace before and did not even know it existed. But now that I found that it might help a lot… and it seems that it does not increase the counter because it says:

result: false
state: 0
wanted_state_above: 4

…but why then the triggering works after fourth press?

And is this a stupid way of thinking it? Is there a simple way to do it?

It is a bit odd to switch on in order to disable something,. but I can imagine you don’t want to hold the relay on for that long. Since you run this automation in single mode, it might miss a door ring. I would suggest to use a restart and no additional conditions. First statement is to increase the counter. As soon as the delay between rings is large enough to make it to the next part and disable the bell. Also Make the condition in an if clause, because it will then only disable the bell if the count was more then 4 and the reset will contine always this prevents the 5th legal ring to be disabled.

alias: Doorbell disable if rang multiple times
description: Disable the doorbell for 30s if rang more than four times in the last 10s
mode: restart
trigger:
  - platform: state
    entity_id: binary_sensor.doorbell_doorbell_hammer
    to: "on"
condition: []
action:
  - service: counter.increment
    entity_id: counter.doorbell_ring_count
  - if:
      - condition: numeric_state
        entity_id: counter.doorbell_ring_count
        above: 4
    then:
      - service: switch.turn_on
        target:
          entity_id: switch.doorbell_doorbell_deactivation
  - delay:
      seconds: 30
  - service: switch.turn_off
    target:
      entity_id: switch.doorbell_doorbell_deactivation
  - service: counter.reset
    entity_id: counter.doorbell_ring_count

Thanks, this was super helpful!