NFC tag : how to check who scanned it

How can I check who’s mobile scanned a tag ?

take a look at this doc:

it has this example which shows you the user_id is in the trigger context.

automation 4:
  trigger:
    # When an NFC tag is scanned by Home Assistant...
    - platform: event
      event_type: tag_scanned
      # ...By certain people
      context:
        user_id:
          - 06cbf6deafc54cf0b2ffa49552a396ba
          - 2df8a2a6e0be4d5d962aad2d39ed4c9c

and if you want to use the user in the actions, you’d use the this object, which is a state object:

putting those together you have access to {{ this.context.user_id }}

more details in that doc above

1 Like

thanks, this works. A whole other solution as what I had in mind, but I can follow.

What I don’t understand…

now I check if person 1 scans a tag. if that tag is xxx then do this

but I want to use if/else structure.if person1 scans tagxxx then do…, else if person2 scans tagxxx then do …
how do I achieve that ?

sure. use condition (‘choose’ in the ui)
and have the template be {{ this.context.user_id == xxxxxxxxxx }
do this for each user that you want to do different actions on.

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ this.context.user_id == 'put_the_user_id_of_person1' }}"
        sequence:
          - service: notify.notify_person1
      - conditions:
          - condition: template
            value_template: "{{ this.context.user_id == 'put_the_user_id_of_person2' }}"
        sequence:
          - service: notify.notify_person2

1 Like

It seems that I am not smart enough to get the programming by myself. I am thinking totally different, and not the good way :slight_smile: when someone gives me the solution I always think “oh yea, that was easy”. I always put conditions or actions in a different way. strange

thanks anyway

choose:
  - conditions:
      - condition: template
        value_template: "{{ trigger.context.user_id == '63ae2011ba54413ea0df17a78f6b0f7a' }}"
    sequence:
      - service: notify.mobile_app_gsm_X
        data:
          title: test
          message: X heeft de tag in ruimte 1 gescanned

are you sure the code trigger.context.user_id is OK ? because it is not working, the condition does not get true

shoot, I didn’t listen to my earlier self…(See my post way above… or even in the same post just before the code. Sheesh). try this.context.user_id

Notify name:

service: notify.notify
data:
  title: NFC TAG
  message: >-
    Test NFC attivato da {% set person = states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}{{ person.attributes.friendly_name }}
  data:
    ttl: 0
    priority: high
    actions:
      - action: CLOSE
        title: Segna come letto

If Davide pass:

condition: template
value_template: >-
  {% set person = states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}
  {{ 'Davide' in person.attributes.friendly_name }}
enabled: true

Device name:

{% set dev_name =  expand(device_entities(trigger.event.data.device_id)) |
  selectattr('domain', 'eq', 'device_tracker')  | map(attribute='name') | join 
  %} Test NFC - device: {{ dev_name }} 

it looks like the “choose” is not working

that does not work for me either. same result. It does not choose

why do you have your condition? It defeats the purpose by restricting to only one person… and they person isn’t the one in the choose… so it will never work…

1 Like

it looks like I copied it wrong…

alias: Ruimte1_nfctag
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
    context: {}
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.tag_id == '0b3128d7-a0fc-4f56-a7fa-ad6041ea207c'}}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ this.context.user_id == '63ae2011ba54413ea0df17a78f6b0f7a' }}"
        sequence:
          - service: notify.mobile_app_gsm_x
            data:
              title: test
              message: x heeft de tag in ruimte 1 gescanned
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.context.user_id == '7ed612d37b704e1cb33004322b946082'
              }}
        sequence:
          - service: notify.mobile_app_gsm_x
...

the condition is to know what tag is scanned…

trigger:

  • platform: tag
    tag_id: " Tag id "

Why into condition?

In fact, that is not my problem. That is working.
The problem is the choose …

It’s not clear what you want.

user is 63ae2011ba54413ea0df17a78f6b0f7a, why isn’t it a normal name?

what is the difficulty?

description: Who tag
mode: single
trigger:
  - platform: tag
    tag_id: ac83XXXXX-XX-XXXXX-XXXXXXXXXXX
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set person =
              states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}
              {{ 'Davide' in person.attributes.friendly_name }}
            enabled: true
        sequence:
          - service: notify.notify
            data:
              title: Centralina Allarme
              message: Davide scan tag
              data:
                ttl: 0
                priority: high
                actions:
                  - action: CLOSE
                    title: Segna come letto
      - conditions:
          - condition: template
            value_template: >-
              {% set person =
              states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}
              {{ 'Sara' in person.attributes.friendly_name }}
            enabled: true
        sequence:
          - service: notify.notify
            data:
              title: Centralina Allarme
              message: Sara scan tag
              data:
                ttl: 0
                priority: high
                actions:
                  - action: CLOSE
                    title: Segna come letto