MQTT automation - how to limit rate of trigger?

I have the following automation and script. Sometimes I receive two mqtt ‘Capture!’ payloads in very rapid succession (less than a second) and it triggers the script twice, but I only want to run the script on the first trigger. How to limit the rate of trigger? Place a sleep condition in the automation?

Automation:

- alias: Doorbell_pressed_automation
  trigger:
    platform: mqtt
    topic: dev/test
    payload: 'Capture!'
  action:
    service: script.turn_on
    entity_id: script.doorbell_pressed_script

Script:

doorbell_pressed_script:
  sequence:
    - service: notify.Telegram
      data:
        title: Home Assistant Notification
        message: --Doorbell Pressed--
        data:
          photo:
            - url: http://192.168.0.21/images/image.jpg
              caption: Doorbell View

Error:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/notify/telegram.py", line 156, in send_photo
    chat_id=self._chat_id, photo=photo, caption=caption)
  File "/home/homeassistant/.homeassistant/deps/telegram/bot.py", line 125, in decorator
    result = func(self, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/telegram/bot.py", line 151, in decorator
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "/home/homeassistant/.homeassistant/deps/telegram/utils/request.py", line 199, in post
    result = self._request_wrapper('POST', url, body=data.to_form(), headers=data.headers)
  File "/home/homeassistant/.homeassistant/deps/telegram/utils/request.py", line 138, in _request_wrapper
    raise NetworkError('urllib3 HTTPError {0}'.format(error))
telegram.error.NetworkError: urllib3 HTTPError ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

Maybe some state condition in the script for itself?

    - condition: state
      entity_id: script.doorbell_pressed_script
      state: 'off'

Just an idea, don’t know if this works.

I limited the rate of trigger where i send the MQTT message. 5 second delay

  if ( doorbellState == LOW ) {
    Serial.println("Doorbell is pressed!");
    client.publish(doorbell_topic, "on", true);
    blink_now();

    delay( 5000 );
  }

I have prevented being spammed with updates when a button is held in by only publishing to the MQTT topic on the change of state. Something like this will do it:

 buttonstate=digitalRead(D0);
  if(buttonstate!=lastbuttonstate) {
    mqttclient.publish(CommandTopic, buttonstate );
    lastbuttonstate=buttonstate;
  }
  delay(500);

You should be able to fix this by deactivating the automation inside itself, run the actions, wait, and reactivate the automation.

- alias: Doorbell_pressed_automation
  trigger:
    platform: mqtt
    topic: dev/test
    payload: 'Capture!'
  action:
    - service: automation.turn_off
      entity_id: doorbell_pressed_automation
    - service: script.turn_on
      entity_id: script.doorbell_pressed_script
    - delay: "00:00:10"
    - service: automation.turn_on
      entity_id: doorbell_pressed_automation

Also, I would just send the message directly inside the automation actions list, but that’s just my preferences.

1 Like

If you want another example I wrote a post about it some time ago, over here: limit automation triggering.

2 Likes

Thanks @chrio this method works nicely :grinning:

1 Like

No problem, glad to see that it worked. :slight_smile: