Doorbell condition not working as expected

Using the following code I would expect to be able to only get notified of a doorbell press every 6 seconds at most:

- id: doorbell_alert
  alias: 'Doorbell Alert' 
  trigger:
    entity_id: binary_sensor.doorbell
    platform: state
    to: 'on'
  condition:
    condition: template # only trigger once every 6 seconds at most
    value_template: "{{ ((as_timestamp(now()) - as_timestamp(state_attr('automation.doorbell_alert', 'last_triggered'))) | int | default(1)) > 5 }}"
  action:
  - service: switch.turn_on
    entity_id: switch.downstairs_doorbell
  - service: switch.turn_on
    entity_id: switch.upstairs_doorbell
  - service: camera.snapshot
    data:
      entity_id: camera.front
      filename: '/config/www/doorbell.jpg'
  - service: switch.turn_off
    entity_id: switch.downstairs_doorbell
  - service: switch.turn_off
    entity_id: switch.upstairs_doorbell
  - service: notify.ios_iphonex
    data:
      message: Someone has pressed the doorbell.
      data:
        push:
          sound: "Doorbell.wav"
        attachment:
          url: !secret doorbell_url
          content-type: jpg
          hide-thumbnail: false

However I can press the doorbell multiple times quickly without the condition kicking in and I get multiple notifications. This is what I want to avoid.

The template evaluates as expected in the dev tools.

Is it something to do with the automation being re-triggered while it is running?

If I wait a second or so between presses I only get one notification every 6 seconds.

Would moving the condition to the actions (before the notification service) help?

I dont mind id the bell rings repeatedly, I just don’t want multiple notifications.

Could it be that your automation takes slightly too long to go through all of it and as a result the last_triggered is only updated after like a sec or so?
What if you move your actions in a script and have a single action call the script? Would the condition work then?

Could be. I’ll give it a go.

EDIT: Yep putting the actions in a script did the trick. Thanks.

1 Like