Using NFC tag last scanned in templates

hmmm ok…
I found a tag. file (with hidden extension I’m assuming), in the hidden directory /config/ .storage
It has all the info I find under developer states page.

  1. Go to Developer Tools > Events
  2. Enter tag_scanned in the “Listen to events” field
  3. Click “Start Listening”
  4. Scan a tag
  5. The event’s information will be displayed. See if it data contains more than just tag_id and device_id.

Beautiness! that does have a lot more info, never messed with event listening. It was last_fired for the timestamp. Let me ask you this, this way doesn’t give static info on one tag only last scanned.
I’m trying to turn this
‘{{state_attr(’‘automation.med1_med’’, ‘‘last_triggered’’).date().strftime(’%Y-%m-%d %H:%M:%S’)}}’
into an sensor, so I will fit into an entity glance.
Do you have an idea what type of entity structure to use.
thx

Post the event data that it reported when you scanned the tag (so we can see all of the fields that are provided).

event_type: tag_scanned
data:
  tag_id: 88b98f79-c6d8-4bd7-bd1d-d74d4368ca75
  device_id: 9a97ef8100213bbf
origin: LOCAL
time_fired: "2022-10-23T21:41:15.216407+00:00"
context:
  id: 01GG3CNXAGG7HVC63M9QJNDFRH
  parent_id: null
  user_id: 65f6c158e6e44cdb8238c8942ebb718a

this is from states tab for my automation / input boolean combo
this is where I’m getting my state_attr string from for a constant date which should reset itself each day or week.
Capture.PNG

last_triggered: 2022-10-23T21:41:15.224067+00:00
mode: single
current: 0
friendly_name: NFC_med1_Med

In your previous post you said:

Maybe I misunderstood you but I thought you meant your reported event contained more information than the event example I had posted. However, it’s identical; data simply contains tag_id and device_id. So now I don’t know what you meant by “a lot more info”.

Oh sorry, you’re right just labeled different after comparing… I guess I meant vs states tab. my bad!

If you want to record the moment when a specific tag is scanned:

template:
  - trigger:
      - platform: tag
        tag_id: 88b98f79-c6d8-4bd7-bd1d-d74d4368ca75
    sensor:
      - name: Tag ca75 Scanned 
        state: '{{ now() }}'
1 Like

Oh that makes sense now… thanks so much for all your help
You’re awesome!

If you add the following option to the example I posted above:

        device_class: timestamp

the UI will display the sensor’s value relative to the current time. For example, “15 minutes ago”. The sensor’s actual value remains a datetime but the UI will display it as a relative time.

1 Like

That’s what I was trying to replicate from dev states…perfect thx

1 Like

You’re welcome!

If you believe my suggestion fulfills your Feature Request, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions (or Feature Requests).

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like

Yeah, I don’t remember the exacts, but the simple forward way didn’t work (reliably) for some reason back then.
I use a mix of external NFC readers (ESP-Home) and the build in the phone one, i always had so scan twice or three times before the template switch would update if i scanned from a different device then the last one.
anyway, good to hear the straight forward way now works.

You can name/rename tags under the config → tag tab

then the cogwheel at the end:

other than being more recognizable than the codes this doesn’t make it a usable entity…

For the last_scanned part see excellent suggestions in this thread.
You would have to create some template sensors

Yeah definitely will mark post :+1:
Have one little issue I can’t resolve is getting time format
(month, day, year hour : min) only.
Nothing seems to work, I hate to keep bugging you over this. But sometimes that little stuff gets me. :rofl:
Hey thanks for all your help… I guess walking away helped!!! got to work
Again, thanks for all your hard work.
For some reason I don’t see the solved check box

1 Like

No worries. I thought you were the author of this topic. Only the topic’s author can mark a post with the Solution tag.

OH yeah that’s right .lol
You’re awesome appreciate the hell out of it

If anyone wants to see… this is end results
Apothecary Card


This should get you where you need to go

input-boolean
  vitamins_taken:
    name: Vitamins
    icon: mdi:pill-multiple
    # icon: mdi:pill

# Automations
## Meds ##
  - alias: med1_Med
    description: 'Daily'
    trigger:
    - platform: tag
      tag_id: 88b98f79-c6d8-4bd7-bd1d-d74d4368ca75
    action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.med1_taken
## Resets ##
  - alias: Reset med1
    trigger:
    - platform: time
      at: "00:00:01"
    action: 
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.med1_taken
        
# Template
## NFC_Scraping ##
  - trigger:
      - platform: event
        event_type: tag_scanned
    sensor:
      - name: NFC_LastTag_Scanned
        state: '{{ trigger.event.data.tag_id }}'
      - name: NFC_LastTime_Scanned
        state: '{{ trigger.event.time_fired }}'
## Date ##
  - trigger:
      - platform: tag
        tag_id: 88b98f79-c6d8-4bd7-bd1d-d74d4368ca75
    sensor:
      - name: NFC_med1_Date 
        state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %B %-d, %I:%M %p') }}"
        icon: "mdi:calendar-clock"

Thank you much @123 Taras so apreci!!

2 Likes

If you’re interested, you can replace this:

{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %B %-d, %I:%M %p') }}

with this:

{{ now().strftime('%a %B %-d, %I:%M %p') }}
1 Like

Retried that string and works this time… I guess it was my formatting before
thanks makes it simpler!