How to write a message to tell who came or went from which location?

I might be trying to cram too much into one automation, but here goes: I want to determine which of two device trackers arrives or leaves which of of two locations. I wrote a super long automation that successfully figures this out, and now I want to send the result in a Telegram message. I need a variable for which person triggered the message, for whether they arrived or left, and for which location. I came from Hubitat where variables like %device% and %value% work, but now I need to know how to accomplish it in Telegram. The code for my triggers is below.

- id: '1629262345220'
  alias: Who arrived/left?
  description: ''
  trigger:
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.shop
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.shop
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.shop
    event: leave
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.shop
    event: leave  
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.bar
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.bar
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.bar
    event: leave
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.bar
    event: leave
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.crib
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.crib
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_buster
    zone: zone.crib
    event: leave
  - platform: zone
    entity_id: device_tracker.life360_trixie
    zone: zone.crib
    event: leave
  condition: []
  action:
  - service: telegram_bot.send_message
    data:
      message: *I need to know how to write this*
  mode: single

Try this:

  action:
  - service: telegram_bot.send_message
    data:
      title: "🌎 *Location Updated*"
      message: "{{ trigger.to_state.name }} {{'entered' if trigger.event == 'enter' else 'departed'}} {{trigger.zone}}"

You can see more about the available trigger variables here, https://www.home-assistant.io/docs/automation/templating/#zone

And more about templating here: https://www.home-assistant.io/docs/configuration/templating/

Thanks for setting me in the right direction! I tweaked and tested it for my specific case, and now it’s working great!

1 Like