Get information about device that triggered automation

Hi.
I have 5 mijia door sensors (zigbee2mqtt) monitoring all of my windows. I created an automation that sends a notification via telegram whenever one of them opens.
Currently, it just prints out the state of all of the windows.
I am now trying to make it so that it only sends the name of the device that triggered it, but i can’t get this to work. Searched the internet and found several posts about the same issue. But each time when i try using their solution, it doesn’t work. Guessing the syntax might have changed in the meantime.

closest i got was using

data:
  message: {{trigger.from_state.attributes.friendly_name}}
service: telegram_bot.send_message

that got me an error, but it did show a device id in the error logs… no idea how to get it normally though…

Can anyone tell me how i am supposed to get the info?

Replace data: with data_template: whenever you use templates

so like this?

data_template:
  message: >- {{trigger.from_state.attributes.friendly_name}}
service: telegram_bot.send_message

Still doesn’t work.

Btw, what qualifies as a template? Because i have this automation that works without data_template:

data:
  message: >-
    A window or door has been Opened! Bedroom Balcony-
    {{state_attr('binary_sensor.0x00158d00034d6d2d_contact', 'contact')}}
    ---------- Bedroom Windows -
    {{state_attr('binary_sensor.0x00158d00040c4f0b_contact', 'contact')}} 
   .... <<some more ids just like above>>.....
service: telegram_bot.send_message

you have not provided the full automation…
so as long as you have data_template as part of your main service, you’ll be fine. You will probably need data later on for your message.

message: {{trigger.from_state.attributes.friendly_name}} will not work, you need to enclose the template in single or double quotes:
message: '{{trigger.from_state.attributes.friendly_name}}'

message: >- {{trigger.from_state.attributes.friendly_name}} will not work either, you need to set your template on the next line, indented:

message: >-
  {{trigger.from_state.attributes.friendly_name}}
1 Like

here this is what I do

- id: 'notify stephan phone Door Change'
  alias: "notify Stephan phone Door Change"
  initial_state: true
  trigger:
  - entity_id: 
    - binary_sensor.garage_side_door
    - binary_sensor.garage_door
    - binary_sensor.front_door
    - binary_sensor.cupboard_door
    - binary_sensor.linen_door
    - binary_sensor.washing_door
    - binary_sensor.dryer_door
    platform: state
  action:
  - data_template:
      title: "Home Assistant"
      message: "{{ trigger.to_state.attributes.friendly_name }} was {% if trigger.to_state.state == 'on' %} Open {% else %} Closed {% endif %}"
    service: notify.stephan_phone
1 Like

Hmm, i think i might be hitting a telegram issue now.
This is my automation:

- id: '1591182611573'
  alias: exp
  description: ''
  trigger:
  - device_id: 2e055d5b22014d82aa014dc1100f10ba
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d0003ce34ab_contact
    platform: device
    type: opened
  condition: []
  action:
  - data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} blabla"
    service: telegram_bot.send_message

It doesn’t work, but i am getting this error in the logs:
“Error sending message: Can’t parse entities: can’t find end of the entity starting at byte offset 15. Args: (1008895627, ‘Dishwasher Door_contact blabla’), kwargs: {‘parse_mode’: ‘Markdown’, ‘disable_notification’: False, ‘disable_web_page_preview’: None, ‘reply_to_message_id’: None, ‘reply_markup’: None, ‘timeout’: None}”

Edit: Looks like changing the service to send a notification to my phone app works fine. So it’s the telegram addon that doesn’t like the message format now… any ideas why?

Btw, @lolouk44, for this working automation:

- id: '1577478983895'
  alias: Notify Door or Window has been Opened
  description: ''
  trigger:
  - entity_id: binary_sensor.0x00158d00034d6d2d_contact
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00040c4f0b_contact
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00040c2704_contact
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00040c271b_contact
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00040c27b6_contact
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      message: 'A window or door has been Opened! Bedroom Balcony- {{state_attr(''binary_sensor.0x00158d00034d6d2d_contact'',
        ''contact'')}} ---------- Bedroom Windows - {{state_attr(''binary_sensor.0x00158d00040c4f0b_contact'',
        ''contact'')}}  ---------- Bathroom - {{state_attr(''binary_sensor.0x00158d00040c2704_contact'',
        ''contact'')}}   ---------------  LivingRoom - {{state_attr(''binary_sensor.0x00158d00040c271b_contact'',
        ''contact'')}} ------------- Kitchen - {{state_attr(''binary_sensor.0x00158d00040c27b6_contact'',
        ''contact'')}} '
    service: telegram_bot.send_message

you mentioned " as long as you have data_template as part of your main service, you’ll be fine". What do you mean by ‘main service’? Not sure why this automation works without ‘>-’ or without ‘data_template’… Trying to understand templating a bit better :stuck_out_tongue:

PPS: Yes, it was a telegram issue. It doesn’t like “_” characters by default. As a quick fix replacing the string for now like this:

data_template:
  message: blablabla '{{ trigger.to_state.attributes.friendly_name.replace("_", "\\_") }}' blabla
service: telegram_bot.send_message

Thanks for the help!

  - data: #<<- this is the data, which should be a data_template
      message: 'A window or door has been Opened! Bedroom Balcony- {{state_attr(''binary_sensor.0x00158d00034d6d2d_contact'',
        ''contact'')}} ---------- Bedroom Windows - {{state_attr(''binary_sensor.0x00158d00040c4f0b_contact'',
        ''contact'')}}  ---------- Bathroom - {{state_attr(''binary_sensor.0x00158d00040c2704_contact'',
        ''contact'')}}   ---------------  LivingRoom - {{state_attr(''binary_sensor.0x00158d00040c271b_contact'',
        ''contact'')}} ------------- Kitchen - {{state_attr(''binary_sensor.0x00158d00040c27b6_contact'',
        ''contact'')}} '
    service: telegram_bot.send_message #<<- this is the service

also in your above automation, if you use a single quote to enclose your complete message, you need double quotes around your entities.
Double quote is ", not 2 single quotes…

2 Likes

Only multi line templates use > . Also single line templates must be enclosed in quotes.

data_template:
  message:  "{{trigger.from_state.attributes.friendly_name}}"
service: telegram_bot.send_message
1 Like

regarding “this is the data, which should be a data_template”: but it does work with just ‘data’, that is what i am trying to figure out why…

regarding " Double quote is " , not 2 single quotes" yeah, i think homeassistant changes it… i did the automation from the UI, not via automations.yaml (that is why initially i only posted the ‘actions’ part, since i pushed ‘edit as yaml’ for that).

So the automation code that i posted and is complete is taken from automations.yaml, but i never touched that…

if i post it how it looks in the UI, it looks like this:

message: >-
  A window or door has been Opened! Bedroom Balcony-
  {{state_attr('binary_sensor.0x00158d00034d6d2d_contact', 'contact')}}
  ---------- Bedroom Windows -
  {{state_attr('binary_sensor.0x00158d00040c4f0b_contact', 'contact')}} 
  ---------- Bathroom - {{state_attr('binary_sensor.0x00158d00040c2704_contact',
  'contact')}}   ---------------  LivingRoom -
  {{state_attr('binary_sensor.0x00158d00040c271b_contact', 'contact')}}
  ------------- Kitchen -
  {{state_attr('binary_sensor.0x00158d00040c27b6_contact', 'contact')}} 

However i still don’t understand why that works… from what you said it shouldn’t…

to be honest I don’t use the GUI editor. There’s a chance that in the background it automatically changes / handles data as data_template…

could be, but i expected it to show properly in automations.yaml…
IIRC, when i did the automation (a few months ago, when i just installed HA for the 1st time) it did change what i wrote in the UI after hitting ‘save’ but can’t remember exactly what… but i do think it changed ‘data_template’ to ‘data’… not 100% sure though :stuck_out_tongue:
Anyway, thanks again for the help!

1 Like