Hi, this has been asked (and answered) multiple times but I couldn’t find some sort of step by step tutorial for implementing notification throttling so here’s mine.
I’m using clicksend integration (ClickSend SMS - Home Assistant) to send out SMS for specific critical events and since I’m billed for every text message sent I want to be sure it is not triggered too many times within a given timeframe. The approach is simple:
- create a counter for counting how many notifications are sent
- before notifying, check if still below the threshould. If so send the notification and increase the counter, otherwise do nothing
- every X minutes reset the counter
In more details:
1. create a counter for counting how many notifications are sent
Go to “Settings”, “Devices & Services”, “Helpers” and create a new Helper, type “Counter”. Let’s call it “sms_counter”
2. before notifying, check if still below the threshould. If so send the notification and increase the counter, otherwise do nothing-
Go to “Settings”, “Automation & Scenes”, “Script” and create a new Script (*):
(*): in my HA, every automation generating a notification will call a script for each notification channel - SMS, mobile app, etc. - so to centralize the logic but the same approach can be implemented directly in an automation without using a script.
The script will check first if the counter is below a threshold (e.g. 5) and only if so will call the notification service (clicksend in this case) and increase the counter.
Here’s the YAML code:
alias: "Helper: Notifications - channel sms"
sequence:
- condition: numeric_state
entity_id: counter.sms_counter
below: 5
- service: notify.sms_user
data:
message: "[House] {{ message }}"
enabled: true
- service: counter.increment
data: {}
target:
entity_id: counter.sms_counter
mode: queued
icon: mdi:message-processing
max: 10
3) every X minutes reset the counter
Go to “Settings”, “Automation & Scenes” and create a new automation:
This automation runs every 10 minutes and if the counter is more than 0, than just resets it.
Here’s the YAML code:
alias: Notification - reset rate limit
description: ""
trigger:
- platform: time_pattern
minutes: /10
condition:
- condition: numeric_state
entity_id: counter.sms_counter
above: 0
action:
- service: counter.reset
data: {}
target:
entity_id: counter.sms_counter
mode: single
This is it. In the example above are basically allowed no more than 5 SMS every 10 minutes. At the end of the 10 minutes, the counter is reset and additional 5 SMS can be sent for the next 10 minutes.
Hope it can help.
Thanks