I am struggling to make automation for sending Telegram messages to several accounts depending on some state.
Historically had automation (with sensitive data stripped) for single telegram bot:
- alias: "Notify when child arrives home"
trigger:
- platform: state
entity_id: device_tracker.child_phone
to: 'home'
condition:
- condition: state
entity_id: device_tracker.parent1_phone
state: 'not_home'
action:
- service: notify.parent1_telegram
data_template:
message: "Child returned home!"
Now I have to incorporate second Telegram account. I could simply just add separate automation but that would not be best.
The problem is condition to send message only to parent (or both) who is away must be handled in action: block and I cannot figure out proper structure for service_template: if that is proper way to do this at all.
Would gladly hear you thoughts how to implement this.
Looks like you want to send a message to several recipients, i.e call notify.parent1_telegram and say, notify.parent2_telegram? Unfortunately, you haven’t specified the magic
so we could help you further.
To me it does not seem like a service_template problem.
I’d suggest firing an event with your message and create as many automations as accounts you have, check your state and call notify service.
Alternatively, call a python_script and do all the logic there.
Solved the issue with help of python_script. Result is elegantly short.
Thanks @AhmadK for the hint - python scripts appears to be very cool functionality to use with automation but it somehow slipped unnoticed for me previously.
For anyone interested solution I ended up is following (sensitive data replaced with generics):
that’s great. they are useful because you don’t need to restart HA, just don’t forget not everything that python allows is available in them as it’s RestrictedPython
Very detailed writeup, I like it.
An extra space in your generic 'device_tracker. parent2_phone' - hope there is nothing like that in the real code?