RFID tag name from id in template

Hi,
Do someone have idea how to get RFID tag name form its id in template?
I have automation which is triggered by tag_scanned event.
This event gives me tag_id in trigger.event.data.tag_id.
I would like to get tag name from this, name which is defined in Configuration → Tags.
Is this even possible?
Thanks in advance!

2 Likes

Tags are defined in .homeassistant/.storage/tag json file in form:

{
    "version": 1,
    "key": "tag",
    "data": {
        "items": [
            {
                "tag_id": "14d45030-fa0d-41d3-9229-123456789",
                "name": "Test kartica",
                "id": "14d45030-fa0d-41d3-9229-123456789",
                "last_scanned": "2021-08-22T06:54:17.660239+00:00"
            },
            {
                "tag_id": "4b8befc0-a045-484b-9387-123456789",
                "name": "Privjesak crveni prazni",
                "id": "4b8befc0-a045-484b-9387-123456789",
                "last_scanned": "2021-08-22T06:54:04.709980+00:00"
            },
            {
                "tag_id": "dd60fbb6-6e90-429b-9d74-123456789",
                "name": "Privjesak zeleni prazni",
                "id": "dd60fbb6-6e90-429b-9d74-123456789",
                "last_scanned": "2021-08-19T09:22:21.794129+00:00"
            },
            {
                "tag_id": "efcde410-cd09-4c6c-123456789",
                "name": "Privjesak plavi prazni",
                "id": "efcde410-cd09-4c6c-8800-123456789",
                "last_scanned": "2021-08-19T09:23:46.427270+00:00"
            }
        ]
    }
}

It is obviously data base structure of some kind. In this directory is many more files in same structure, some of them are:

core.area_registry
core.config
core.config_entries
core.device_registry
core.entity_registry
core.restore_state

so let me ask more generic question, is it possible to read somehow this keys and theirs values from templates?

Did you try:

{{ trigger.event.data.tag_id.name }}

Edit: no that wont work.

it’s json and yes you can read it. But probably not natively.
I used Node red to read one of the core files and get what I wanted.

Use file in node and follow it with a json node. Then you should have an array type structure of the contents and it’s just a matter of looping to get what you need.

Thanks @Hellis81,
yes, of course, I recognized JSON format and I have no doubt that it can be readen and interpreted via Node red, but this brings more problems than solutions in aspect of existing automation. My hope was (is) that this can (should) be possible somehow from template, because those files are HA data/config and they are interpreted and imported in HA anyway.

Did you try this?

Going to try right now.

Unfortunatelly, not working.

If you want to give node red a try then I suggest you pass the values to an entity where entity name is the id, and state is the name.
That way you can easily template it as you like.

Having an inject that injects once an hour to create new tags will probably be enough.

Well,
this can be question / feature request for integration developer. Why we can define name of tag if we can’t get it.

Use the dev tools events page to see what data is returned by a tag_scanned event.

EDIT: I just tried with one of my tags. The data does not include the name (some data redacted):

{
    "event_type": "tag_scanned",
    "data": {
        "tag_id": "00Exxxxx",
        "device_id": "xxxx96a73962fxxxxxxx862e2a8dexxxxxxx"
    },
    "origin": "LOCAL",
    "time_fired": "2021-08-22T08:27:04.187126+00:00",
    "context": {
        "id": "2b52a44303xxxxxxxxxxxxxxd6b",
        "parent_id": null,
        "user_id": null
    }
}

Seems the easiest way to do this is going to be a template that maps id’s to names in your automation.

Thanks a lot,
I just did the same testing in dev tools and as documentation says too, tag_id amd device_id are returned only.
Maintaing some kind of ‘local’ name table is a bit impractilal, so way to get tag name by id or field name in event data could be a feature request.
If this is not possible, than maintaining tag names from gui is meaningless.

Definitely worth asking for:

Edit: someone already has, vote here:

https://community.home-assistant.io/t/include-name-field-in-tag-scanned-event-data/253720

1 Like

Well,
two years without the vote… It is not likely that something will happen :frowning:

WTH month might be coming next month. Keep it in mind.

1 Like

If you use this sequence and connect that to the event node when you scan a tag then you should be able to get the name of the tag and all that.
But that means your automation will be running in node red.

[{"id":"907645a9.63a318","type":"file in","z":"48ef5c6b.b38b24","name":"","filename":"/config/.storage/tag","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":410,"y":1240,"wires":[["8d13f7ee.92c858"]]},{"id":"ea6a6141.d6cb6","type":"inject","z":"48ef5c6b.b38b24","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":1080,"wires":[["907645a9.63a318"]]},{"id":"8d13f7ee.92c858","type":"json","z":"48ef5c6b.b38b24","name":"","property":"payload","action":"","pretty":false,"x":590,"y":1200,"wires":[["3c8f4821.07a408"]]},{"id":"3c8f4821.07a408","type":"function","z":"48ef5c6b.b38b24","name":"","func":"\nfor (let value of msg.payload.data.items) {\n    msg.tag = value.tag_id;\n    msg.name = value.name;\n    node.send(msg)\n\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":1260,"wires":[["c165a8fa.6281c8"]]},{"id":"c165a8fa.6281c8","type":"debug","z":"48ef5c6b.b38b24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":1080,"wires":[]}]

+1 on this…also voted for the feature.

This is the reason why I stopped bothering with the tags feature in Home Assistant. My idea was to have rfid stickers on my grocery boxes and after a scan the grocery names would be added to a shopping list. But it was too laborious to maintain two huge separate lists of mappings.
Added a vote too.

1 Like

This is a much needed feature - it would remove the need to hand-code every automation in terms of “play media”, “add item to grocery list”, etc.

As it stand you need to make a translation_function for each tag_id to a name, but you already maintain the tag_name in the Tags_section.

I also voted on the feature, it would seem like a small thing to transfer.

1 Like

Using a variation of the node red sequence I posted earlier you should be able to get a complete translation table.
Here is my broadlink commands.

You should be able to create an entity with attributes like “RFID tag”: “friendly name of tag”.
And in automations that could be used like {{ state_attr('sensor.tag_names', [scanned value]) }} to get the friendly name.

Hi, thanks for getting back to me.

I’m unable to quite decode if what you’re suggesting is using a node-red intermediary to get the card name?