Using NFC tag last scanned in templates

Hi, I’m making a simple (I hope) feature request.

Can you create a template helper for getting the state of an NFC tag?
The functionality I’m looking for is basically states('tag_id').last_updated or in this case last_scanned. Maybe something like nfc('tag_id').last_scanned

It would be also nice if we could assign a unique entity name to each tag (which should not be visible anywhere in the system because of clutter), but that could be used to get the last updated date of an NFC tag => nfc('friendly_name').last_scanned

A practical example for this:
I replaced all my washing machine automations with a template sensor which checks if the washing machine is off and if the door has been opened after the last update of the washing machine status.

If I were to use an NFC tag instead of a door sensor (because it’s cheaper) currently I won’t be able to use it in the same way, since I don’t have a way to get the last_scanned date of a tag.

I agree, it would be real handy to display the last scan of tag next to the input boolean I created for the tag.
I found this while searching around but not sure how to get it to work

template:
  - sensor:
      - name: "LaatsteTag"
        state: >
          {%- set readers = [states.sensor.tagreader_prototype_last_tag, states.sensor.tagreader2_last_tag] %}
          {% for reader in readers %}
            {% if as_timestamp(reader.last_changed) == as_timestamp(readers | map(attribute='last_changed') | max) %}
              {{ reader.state }}
            {% endif %}
          {% endfor %}

I think the user in your linked post had difficulties with the Trigger-based Template Sensor because of a syntax error. This seems like it should work:

template:
  - trigger:
      - platform: event
        event_type: tag_scanned
    sensor:
      - name: Last Tag Scanned 
        state: '{{ trigger.event.data.tag_id }}'
1 Like

Thanks for the reply, Taras
I’m not the best at templating stuff, still learning.
I tried a couple of variations from what you posted and I get “trigger is undefined”
any ideas?
thanks

Was one of the “variations” an attempt to configure a Trigger-based Template Sensor?

Do you have any existing Template Sensors defined in modern style (i.e. defined under the template: key in configuration.yaml or in a separate file like templates.yaml)?

Oh no? I was trying to get it to work through Developer tab>Template
Honestly I was wondering (couldn’t find an easy answer) if I was suppose to create a templates.yaml or do a templates: !include_dir_merge_named templates/ like I do for future uses. Have not mess with templates much till getting into NFC’s lol

The Template Editor is for testing a Jinja2 template. That’s this part of the Trigger-based Template Sensor I had suggested:

{{ trigger.event.data.tag_id }}

However, you can’t test it in the Template Editor because it relies on information supplied by the Event Trigger (via thetrigger variable). Triggers are not evaluated nor simulated by the Template Editor.

The only way to test what I presented is to add it to your instance of Home Assistant, scan a tag, then observe the value reported by sensor.last_tag_scanned.

You aren’t obligated to do either, that’s your choice only if you want to move the configuration of Template entities out of configuration.yaml and into one or more separate files.

grateful for input, that makes more sense why I wasn’t getting what I was hoping for.
I was messing around trying to make a medication confirmation in HA, I forget sometimes if I took them…lol But I just wanted to have a timestamp of tags that reset itself everyday and every week
I’ll have to mess around later on tomorrow. thanks for help

Hey Taras,
I got template working… much thx
But now how can I scrape more info from the tag i.e. name, and last scanned.
Tried a few ways by swapping out tag_id, adding second line, etc
but not reporting anything.
Thanks again

Here’s an example of a tag_scanned event that I found in another topic:

{
    "event_type": "tag_scanned",
    "data": {
        "tag_id": "1234567",
        "device_id": "deadbeaf123456778abadf1322232222"
    },
    "origin": "LOCAL",
    "time_fired": "2021-09-22T20:10:19.465266+00:00",
    "context": {
        "id": "9b3786c6320f7c4991e6df7a8d77f16b",
        "parent_id": null,
        "user_id": null
    }
}

If you look at the information available in data you’ll see it contains only two fields:

tag_id
device_id

Where did you get the impression that it contains other fields like name and last_scanned?

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.