[SOLVED] Automation to conditionally send Telegram notification to multiple devices

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.

Yes, I would like message to be sent to

  • notify.parent1_telegram if state of device_tracker.parent1_phone is not_home
  • notify.parent2_telegram if state of device_tracker.parent2_phone is not_home

Thanks for mentioning python_script option - will look into it.

and what to do if both parent are not home?

Both receive message.

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):

  1. add config/python_scripts/notify_away_parents_about_returning_child.py script:
message = "Child returned home!"

def send_message(bot, text):
    hass.services.call("notify", bot, {"message": text}, False)
    logger.info("Sending to %s message '%s'", bot, text)

if hass.states.is_state('device_tracker.parent1_phone', 'not_home'):
    send_message("parent1_telegram", message)
if hass.states.is_state('device_tracker.parent2_phone', 'not_home'):
    send_message("parent2_telegram", message)
  1. add/update logger properties in config/configuration.yaml to have info level message actually appear in log file:
logger:
  default: error
  logs:
     homeassistant.components.python_script.notify_away_parents_about_returning_child.py: info
  1. reboot Home Assistant to initialize new python_script service.
  2. update automation to use new python script:
- alias: "Notify when child arrives home"
  trigger:
    - platform: state
      entity_id: device_tracker.child_phone
      to: 'home'
  action:
    - service: python_script.notify_away_parents_about_returning_child
1 Like

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?

Eagle eye :+1:.Fixed.

1 Like