Is there any way to know what user scanned a NFC tag with home assistant?


I would like to know if there is any way to know what device scanned the NFC tag.
for example:
A user scans the tag with their phone, i want that to then trigger an event that says "welcom home ‘user’ " but then if another user scans it i would like it to say their name.

I should also add i am relitivly new to HASS

1 Like

tag_scanned event provides output similar to following:

    "event_type": "tag_scanned",
    "data": {
        "tag_id": "93182830-f2c9-4557-9ffa-94ebce875b19",
        "device_id": "E6Axxxxxxxxxxxxxxxxxxx0D2"
    "origin": "LOCAL",
    "time_fired": "2021-11-07T08:05:51.119693+00:00",
    "context": {
        "id": "645a4fb060f5155b7ef658e0c3f8f97c",
        "parent_id": null,
        "user_id": "3df1eccaaa654858b72e6d942ecc39e0"

You test trigger for for either device_id to identify specific device that was used to scan NFC or user_id to identify user (though this would be user associated with the device, so result woul dbe the same…

Here is sample automation that can be triggered only by specific tag and specific device (so effectively associates this automation to specific user):

- alias: 'Toggle lights on NFC'
  initial_state: true
    platform: event
    event_type: tag_scanned
      tag_id: 93182830-f2c9-4557-9ffa-94ebce875b19
      device_id: E6Axxxxxxxxxxxxxxxxxxx0D2
    service: light.toggle
    entity_id: light.mirek_office
1 Like

this template will print the friendly name for the user from the event

{% set person = states.person|selectattr('attributes.user_id', 'eq', trigger.event.context.user_id)|first %}{{ person.attributes.friendly_name }}

I’m stuck. How do I find the device_id of the phone that triggered the NFC event? Using an iPhone with iOS companion app.

The device_id is in the event-data as shown in the second post in this thread.

The template from dshokouhi does not even require any device_id as the userh_id is also part of the event.

You can set an automation with an event as trigger, instead of setting it with a tag trigger, then you can get the user who triggered the event. Try something like this:

- alias: scanned_tag_example
  description: ''
  - platform: event
    event_type: tag_scanned
  condition: []
  - service: mqtt.publish
      topic: tags/userFName
      payload_template: '{% set person = states.person|selectattr(''attributes.user_id'',
        ''eq'', trigger.event.context.user_id)|first %}{{ person.attributes.friendly_name
  mode: single

This will publish a MQTT message on the topic tags/userFName with the user friendly name as payload.
Bear in mind that there should be a person assigned to the user. Users without person don’t have friendly names.
For sure, you can change the action according to your needs.

I hope this helps.