Throttling Home Assistant Automations

Suppose you have a Home Assistant Automation, for example one that sends a notification to your phone, that you’d only like to run at most once every four hours or so.

You might google Debouncing an automation, because that’s the word that jumps into your head first, and end up here, which suggests a template condition like this:

 condition:
    condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.doorbell_alert', 'last_triggered') | default(0)) | int > 5)}}"

But then you have to do math, and it’s awkward.

There’s a much nicer way!

 condition:
    condition: template
    value_template: "{{now() - state_attr('automation.doorbell', 'last_triggered') > timedelta(hours=4, minutes = 1)}}"