I’m using on a binary_sensor for motion, and it can can trigger many times per second. Is there a way for the notification action to be rate limited so that notifications are only sent so many times per time period?
Here’s my workaround:
automation.yaml:
- alias: Notify on motion during workday
trigger:
platform: state
entity_id: binary_sensor.garage_motion, binary_sensor.office_motion
to: 'on'
condition:
condition: time
after: '8:00:00'
before: '17:00:00'
weekday:
- mon
- tue
- wed
- thu
- fri
action:
service: notify.motion_detected
notify.yaml:
- platform: command_line
name: motion_detected
command: "/etc/hass/scripts/notify_motion"
/etc/hass/scripts/notify_motion:
#!/bin/sh
delayseconds=600
nextnotify=$(cat /etc/hass/scripts/notify_motion.dat 2>/dev/null)
timenow=$(date +"%s")
if [ "${nextnotify}" = "" ]; then
echo ${timenow} > /etc/hass/scripts/notify_motion.dat
exit
fi
if [ "${timenow}" -gt "${nextnotify}" ]; then
nextnotify=$((timenow+delayseconds))
echo ${nextnotify} > /etc/hass/scripts/notify_motion.dat
subject="Motion detected"
message=${subject}
echo "${message}" | mail -s "${subject}" [email protected]
echo "Notifying..."
else
echo "Next notification in $((nextnotify-timenow)) seconds..."
fi