Automation + using Telegram: how to prevent "Connection pool is full" and why "queued" script is not actually queued

There is an automation which sends a message via Telegram if some conditions are met.
Assume - if some device becomes unavailable.

Devices may become unavailable “almost simultaneously”.
Means - messages should be sent at “almost” one moment of time.

In these cases I usually see messages in log like:
2022-08-01 10:54:20 WARNING (SyncWorker_15) [telegram.vendor.ptb_urllib3.urllib3.connectionpool] Connection pool is full, discarding connection: api.telegram.org
2022-08-01 10:54:20 ERROR (SyncWorker_15) [homeassistant.components.telegram_bot] Error sending message: Flood control exceeded. Retry in 5.0 seconds. Args: (-1001301432273, ‘RPi-2: ping OK (startup)’), kwargs: {‘parse_mode’: ‘HTML’, ‘disable_web_page_preview’: None, ‘disable_notification’: False, ‘reply_to_message_id’: None, ‘reply_markup’: None, ‘timeout’: None}

Guess there is a problem with an “almost simultaneous” using Telegram.
What I need is a some kind of mutex (mutual exception) here - do not allow to use Telegram unless it is becomes available.

What I have now:

  1. A script “notify_send_notification” containing a “notify.xxxx” service call for telegram.
  2. A blueprint “XYZ” containing calling a script “notify_send_notification” if some template is TRUE.
  3. A few automations based on that “XYZ” blueprint.

Assume that the “XYZ” blueprint has a “mode: restart”.
The logic is that one automation is for one device.

I may specify a “mode” for the “notify_send_notification” script.
What mode should it be?
Queued?
Will it help me?

Update: “queued” does not help, even with “max: 50” (I have ~ 5 devices which may become unavailable).
I see those errors in the Log only on HA startup.
The “XYZ” blueprint checks if the device is available; on HA startup some devices are NOT available, so that “notify_send_notification” script is called several times. Seems that some calls are made simultaneously - although I asked to use the “queued” mode for the script.

I believed that “queued” script cannot be called simultaneously.

Update:
Seems that calling a “notify.xxxx” service for Telegram does not make the “notify_send_notification” script to wait for completion of this call.
Probably the next call of “notify.xxxx” service occurs when the Telegram platform is still busy with the previous call.

And probably I need to add a small delay into the “notify_send_notification” script after the call of “notify.xxxx” service… But this is probably a bad solution since the delay may be smaller than an actual duration of the “notify” operation.
What I added is:

              - service: notify.xxxxxxx
                data:
                  message: >-
                    {{....}}

              - delay: 5

Now I do not see errors on HA startup - so far. Again, this is not a good solution since the delay’s value may become less than needed…

3 Likes