Attribute entity_id with event device_tracker_new_device not working

To get a notification when a new device is discovered on my network I added the following to my Automations.

- alias: Notify when new device discovered
  trigger:
    platform: event
    event_type: device_tracker_new_device
  action:
    - service: notify.telegram
      data_template:
        message: >
          New device:
          {{trigger.event.data.host_name}}
          ({{trigger.event.data.entity_id}})
        title: New device

Unfortunately this is not working. The new device is being recognized but I do not get a notification. When I remove the {{trigger.event.data.entity_id}} it’s working correctly: I receive a message with the host name.
Is the attribute entity_id for this event broken with HA 0.75?

is this of any help?

Thanks, but I am afraid that’s not going to help. In that thread the OP is looking for the attribute to get the entity_id. I know the attribute (according to the documentation): trigger.event.data.entity_id
Unfortunately it’s not giving back any data or maybe it even generates an error somewhere because the notification isn’t being send at all.

You need to look at the event response. Everything in the data section is dependent on the device that is created. This information will change between components. If entity_id is not returned in the event, that method will not work.

I recommend you turn info on in logging and look at the event data.

I mean, you could just use this method, but you may not always get an entity_id:

- alias: Notify when new device discovered
  trigger:
    platform: event
    event_type: device_tracker_new_device
  action:
    - service: notify.telegram
      data_template:
        message: >
          New device:
          {{ trigger.event.data.host_name if trigger.event.data.host_name is defined else "NA" }}
          ({{ trigger.event.data.entity_id if trigger.event.data.entity_id is defined else "NA" }})
        title: New device

Thanks for your reply! I will try this this evening.
Actually I wonder why there should not always be an entity_id for a newly discovered device. I cannot imagine a situation in which it can be empty. trigger.event.data.entity_id is a valid attribute according to the documentation and a lot of other examples.

It’s because you are using an event trigger. Event triggers do not contain entity id’s because they aren’t always there. Almost all other triggers contain an entity_id.

Strange, because in these threads the entity_id is used succesfully with the device_tracker_new_device event.

and the source code for the device tracker specifies the entity_id as an valid attribute:
https://github.com/home-assistant/home-assistant/blob/714b5161767bc426f261835b4fc06519435076e3/homeassistant/components/device_tracker/init.py#L297-L300

Again, that is dependent on the component being used. For example, a IOS device tracker may have different event data than a ASUS router device tracker. It all depends on the component being used. Apparently the component you are using doesn’t give entity_id inside the event data.

Ah, now I get it! Thanks!

FWIW, here’s the code in method async_see (from homeassistant/components/device_tracker/__init__.py) that fires that event:

EVENT_NEW_DEVICE = 'device_tracker_new_device'
...
        self.hass.bus.async_fire(EVENT_NEW_DEVICE, {
            ATTR_ENTITY_ID: device.entity_id,
            ATTR_HOST_NAME: device.host_name,
            ATTR_MAC: device.mac,
        })

It hasn’t changed for a long time. And I don’t see that method being overridden in any device_tracker platform code. So it would appear that trigger.event.data.entity_id should be valid no matter which device_tracker platform is being used.

Of course, that doesn’t explain why it’s not working for you. You should do as @petro suggests and look in your HA log for that event and see what data is actually logged.

1 Like

Apparently the entity_id is retrieved correctly (device_tracker.laplinux). The error occurs when sending the Telegram message.

  id: Telegram_new_device
  alias: 'Bericht bij nieuw netwerkapparaat'
  hide_entity: true
  trigger:
    platform: event
    event_type: device_tracker_new_device
  action:
    - service: notify.telegram
      data_template:
        title: Netwerkapparaat
        message: >
          New device:
          {{trigger.event.data.host_name}}
          ({{trigger.event.data.entity_id}})

and the error message:

Tue Aug 21 2018 19:28:26 GMT+0200 (CEST)
Error sending message: Can't parse entities: can't find end of the entity starting at byte offset 44. Args: (12345678, 'Netwerkapparaat\nNew device: Laplinux (device_tracker.laplinux)'), kwargs: {'parse_mode': 'Markdown', 'disable_notification': False, 'disable_web_page_preview': None, 'timeout': None, 'reply_to_message_id': None, 'reply_markup': None}

Problem solved!

@pnbruckner
@petro

The entity_id could be retrieved. Problem was the parse mode for Telegram. Default is markdown but for values containing an _ it should be html

Thanks for helping!

1 Like

I know it’s old but trying to have it working with notify.pushbullet service but it just doesn’t work… the problem is with message. Do you know how should it be written to work?

  - alias: Notify for new devices
    trigger:
      platform: event
      event_type: device_tracker_new_device
    action:
      - service: notify.pushbullet
        data:
          title: "New device"
          message: "New device: something"

it works but

message: >
New device:
{{trigger.event.data.host_name}}
({{trigger.event.data.entity_id}})

doesn’t work. What do you think?

try to replace data with data_template

1 Like

thank you! it works! I would not solve it without your help!

1 Like

no worries. data is for static data. If you need variables (the stuff that goes between the curly braces) you need data_template.
Have fun