New Device Tracker Events

I have some questions about the new method being used for device_trackers. So it seems all “legacy” device trackers are being switched over to use the entity/device registry and known_devices.yaml has been deprecated… which I totally get.

I had an automation to let me know when new devices were detected on my network, but as the Integrations have been updated they stop using known_devices.yaml. The Netgear Integration seems to still use known devices but I’m sure that will be updated as well…

The old integrations seemed to use “device_tracker_new_device” which contained all relevant information needed.

device_tracker_new_device
{
    "event_type": "device_tracker_new_device",
    "data": {
        "entity_id": "device_tracker.xa_x5_c9_x4_1d_28",
        "host_name": "xA:x5:C9:x4:1D:28",
        "mac": "xA:x5:C9:x4:1D:28"
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-06T20:41:05.229232+00:00",
    "context": {
        "id": "749767934c8884a3314c8911829c95ff",
        "parent_id": null,
        "user_id": null
    }
}

So I was trying to use the events to get information when a new device_tracker entity was created but It seems that watching the events produces events that do not contain the host_name.

entity_registry_updated
{
    "event_type": "entity_registry_updated",
    "data": {
        "action": "create",
        "entity_id": "device_tracker.amys_ipad"
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-06T23:53:17.173668+00:00",
    "context": {
        "id": "2d7dcb9ef99cad949538d6eaa338001b",
        "parent_id": null,
        "user_id": null
    }
}
device_registry_updated
{
    "event_type": "device_registry_updated",
    "data": {
        "action": "update",
        "device_id": "86a972a3e45bd9d5eca7a594553fef40"
    },
    "origin": "LOCAL",
    "time_fired": "2021-03-06T23:55:48.616443+00:00",
    "context": {
        "id": "3f322e2f1098241c2fe1333763f944ae",
        "parent_id": null,
        "user_id": null
    }
}

So after those events, there are entries in core.device_registry and entity_registry which contain all the needed information but it does not get passed in either of these events.

I am wondering if I am missing something here. Is it because I may have missed something in a registry entry and haven’t COMPLETELY removed all references to said devices when testing or does it not create that “device_tracker_new_device” event any longer? If it is not using that “device_tracker_new_device” does anyone know of a way to pull the host_name and/or mac_id which the new method of creating these entities?

1 Like

I’m also interested in how to accomplish this. I’m trying to setup mobile notifications for when new devices are added via the asuswrt integration.

Looking through device_tracker, I only see events for “enters” and “leaves”, not for something like “new_device”.

https://github.com/home-assistant/core/blob/19ab7306ecd19dc69f12ad397e365ec9b086b06d/homeassistant/components/device_tracker/device_trigger.py

Got it working using node-red.

[{"id":"84a68b6f.c56eb8","type":"comment","z":"6a33d6a7.b9f7f8","name":"Send notification if new device connected to network","info":"","x":210,"y":340,"wires":[]},{"id":"2e7cbd72.da5fd2","type":"server-events","z":"6a33d6a7.b9f7f8","name":"homeassistant: entity_registry_updated","server":"94b78baa.1145f8","event_type":"entity_registry_updated","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"x":170,"y":380,"wires":[["88508a22.67d008"]]},{"id":"9c81a8d8.a477e8","type":"function","z":"6a33d6a7.b9f7f8","name":"get device","func":"\nentity_id = msg.payload.event.entity_id\nif (entity_id.indexOf(\"device_tracker.\") === 0) {\n    ha = global.get(\"homeassistant\")\n    allStates = ha.homeAssistant.states\n    \n    msg.device = allStates[entity_id]\n    \n    return [msg, null];\n}\n\nreturn [null, msg];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":750,"y":380,"wires":[["2d5f1c63.e6fb94"],[]]},{"id":"88508a22.67d008","type":"switch","z":"6a33d6a7.b9f7f8","name":"event action create","property":"payload.event.action","propertyType":"msg","rules":[{"t":"eq","v":"create","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":380,"wires":[["9c81a8d8.a477e8"]]},{"id":"56959b4a.85d864","type":"function","z":"6a33d6a7.b9f7f8","name":"format notification","func":"\nvar m = msg;\n\nvar time_fired = m.payload.time_fired;\nvar entity_id = m.device.entity_id;\nvar source_type = m.device.attributes.source_type;\nvar friendly_name = m.device.attributes.friendly_name;\n\nmsg = {\n    topic: \"HA: new device connected\",\n    sound: \"siren\",\n    priority: 1,\n    device: global.pushover_system_msg_recipient,\n    timestamp: time_fired\n}\nif (source_type === \"router\") {\n    var ip_address = m.device.attributes.ip_address;\n    var mac = m.device.attributes.mac;\n    \n    msg.payload = '';\n    msg.payload += 'source_type: ' + source_type + '\\n';\n    msg.payload += 'ip_address: ' + ip_address + '\\n';\n    msg.payload += 'mac: ' + mac + '\\n';\n    msg.payload += 'entity_id: ' + entity_id + '\\n';\n    msg.payload += 'friendly_name: ' + friendly_name + '\\n';\n    msg.payload += 'time_fired: ' + time_fired;\n}\nelse {\n    msg.payload = '';\n    msg.payload += 'source_type: ' + source_type + '\\n';\n    msg.payload += 'entity_id: ' + entity_id + '\\n';\n    msg.payload += 'friendly_name: ' + friendly_name + '\\n';\n    msg.payload += 'time_fired: ' + time_fired;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1270,"y":380,"wires":[["7fd51421.3c694c"]]},{"id":"7fd51421.3c694c","type":"pushover api","z":"6a33d6a7.b9f7f8","keys":"3edd6a51.dc3946","title":"","name":"","x":1480,"y":380,"wires":[]},{"id":"2d5f1c63.e6fb94","type":"switch","z":"6a33d6a7.b9f7f8","name":"device found","property":"device","propertyType":"msg","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":990,"y":380,"wires":[["56959b4a.85d864"],["a25649b6.317258"]]},{"id":"f392c017.60d72","type":"function","z":"6a33d6a7.b9f7f8","name":"count delay","func":"\nmsg.delay = parseInt(msg.delay);\n\nif (isNaN(msg.delay)) { msg.delay = 0; }\n\nmsg.delay = msg.delay + 5;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":460,"wires":[["9c81a8d8.a477e8"]]},{"id":"a25649b6.317258","type":"delay","z":"6a33d6a7.b9f7f8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":760,"y":460,"wires":[["f392c017.60d72"]]},{"id":"94b78baa.1145f8","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"3edd6a51.dc3946","type":"pushover-keys","name":"Pushover: Stirnas - NodeRed"}]

I’m not a Node Red user, anyone know how to make this work as a template? I guess I didn’t really think about the fact that those devices would have the device_tracker domain and I could look for that in the event… can you find a way to pull the hostname using Node Red? It didn’t seem to populate that until after the entity was created. (Although it doesn’t seem to pull that for all devices)

Here’s what I added to my automations.yml to get a notification with the new device’s hostname. It seems to work for me, but I’m new here so I’m not sure if this is a robust solution or a hack. It relies on the fact that the first state change for a new device will have an empty old_state.

- id: '123456'
  alias: New network device alert
  description: ''
  trigger:
  - platform: event
    event_type: state_changed
  condition:
    - "{{ trigger.event.data.new_state.attributes.source_type == 'router'}}"
    - "{{ trigger.event.data.old_state is none }}"
  action:
  - service: notify.mobile_app_iphone
    data:
      message: "New device on network: {{trigger.event.data.new_state.attributes.friendly_name}}"
  mode: single

Here’s the relevant documentation.

3 Likes

this works:

  - alias: New device registered
    id: New device registered
    mode: queued
    trigger:
      platform: event
      event_type: entity_registry_updated
      event_data:
        action: create
    condition: >
      {{trigger.event.data.entity_id.startswith('device_tracker')}}
    action:
      - variables:
          entity: >
            {{trigger.event.data.entity_id}}
      - service: system_log.write
        data:
          message: >
            New device registrered: {{state_attr(entity,'friendly_name')}}
          level: warning
          logger: homeassistant.components.device_tracker
      - service: persistent_notification.create
        data:
          title: >
            New device registered: {{state_attr(entity,'friendly_name')}}
          message: >
            Entity: {{entity}}
            Host: {{state_attr(entity,'host_name')}}
            Ip: {{state_attr(entity,'ip')}}
            Mac-address: {{state_attr(entity,'mac')}}
            Full data: {{trigger.event.data}}
3 Likes

I think the 2022.2 release broke the automation suggested here with the improved handling of device tracker entities item. Any suggestions on how to update it to work going forward?

Nope, ive asked and reported many a times on GitHub now, and so far we get no response at all. Its a real nuisance we can no longer use this method, as it was suggested by core team we did it this way…

I should probably rewrite the issue, as it was on another matter originally, and that has been solved.

there’s also device_tracker: bring back firing an event when a new device is found · Discussion #586 · home-assistant/architecture · GitHub to which you can chime in and add your findings

@Mariusthvdb I see you’ve invested quite a lot of time on this business of notifying new devices detected. I think it is a rather useful feature and maybe it deserves its own addon instead of depending on a feature that for other reasons wants to be discontinued. but having said that, what is the best thread on which to carry on the discussion?

well, this might be the best title.
Bartem can ‘unsolve’ it probably :wink:

otherwise, I’d suggest the #general topic on Discord, or maybe #hardware, and then see where it takes you?