Hi everyone! I’ve been diving into optimizing my Home Assistant automations lately, and I wanted to share my journey with you. One of the areas I focused on was improving how I send images to Telegram, specifically multiple photos in a single message.
For a while, I had an automation set up to take multiple snapshots and send them individually to Telegram. While it worked, maintaining it was a bit of a hassle, especially when I wanted to tweak anything. I noticed that each service call was separate, which made scaling or modifying the setup quite cumbersome.
Here’s what my old setup looked like:
yaml
- service: camera.snapshot
data:
filename: /media/door_opened_1.jpg
target:
device_id: aaa - service: camera.snapshot
data:
filename: /media/door_opened_2.jpg
target:
device_id: aaa
… and so on for each image
- service: telegram_bot.send_photo
data:
authentication: digest
caption: ‘1’
file: /media/door_opened_1.jpg, /media/door_opened_2.jpg
… more service calls for each image
I stumbled upon the telegram_bot.send_photo service and wondered if it could handle multiple files in one go. The documentation wasn’t entirely clear, but I decided to give it a shot. After some experimentation, I found that while the service doesn’t natively support multiple files, there’s a workaround using templates and lists.
Here’s the improved version of my automation:
yaml
- alias: Take and Send Door Photos
trigger:
platform: time
at: ‘08:00’
action:- service: camera.snapshot
data:
filenames: [‘/media/door_opened_1.jpg’, ‘/media/door_opened_2.jpg’, ‘/media/door_opened_3.jpg’, ‘/media/door_opened_4.jpg’, ‘/media/door_opened_5.jpg’]
target:
device_id: aaa - service: telegram_bot.send_photo
data:
authentication: digest
caption: ‘Door Check’
file: >-
{{
states(‘input_list.door_photos’)
| map(‘regex_replace’, ‘^/media/’, ‘’)
| join(', ')
}}
- service: camera.snapshot
This setup uses an input_list to manage the list of filenames, making it easier to add or remove images without rewriting the entire automation. The file parameter now dynamically constructs the list of files using a template.
If you’re looking to streamline your Telegram image notifications, I highly recommend exploring this approach. It’s not only cleaner but also more maintainable in the long run. Let me know if you have any questions or tips to share!
Cheers!
P.S. If you’re curious about how I handle other aspects of my Home Assistant setup, feel free to ask. I’d love to hear about your projects too!