Help thinking to how build a RFID keys management

I built a lock with RFID tags, now that everything is working, I want to expand the keys system to be more dynamic.

Right now I use input_text as holders of the values. and just read from them or assign learned tags.
I also created an automation for assigning new keys to the input_text in case I need to replace one.

But after having my 10th key… this is starting to blow up the yaml file…

What I want is to have a structure that I can assign multiple tags to it (like a person who have multiple tags) and then the automation of checking the keys can be simpler (even adding some more features like allowed time and so).

Any ideas of how to do so? maybe just put me in the right direction and ill carry on.

Thanks for any help guys.

1 Like

I think it’s easier if you post the structure you have now

it may not be quite what you are looking for, but if it was me - I’d probably store the tags in a template sensor:

template:
   sensor:
     - name: RFID Keys
       state: "OK"
       attributes:
         keys:
           - "rfid-tag-1"
           - "rfid-tag-2"
           - "rfid-tag-3"
           - "rfid-tag-4"
         people:
           - "person1"
           - "person2"
           - "person3"
           - "person4"

Then your automation only has to do:

{{ trigger.event.data.tag_id in state_attr('sensor.rfid_keys','keys') }}

in the condition, it will be true if the key exists in the template sensor.

@mobile.andrew.jones . Thanks, looks like a simple solution. but how do I dynamically change keys in a template?

@Hellis81 I am adding the structure below (I am sorry for the bulky and un-efficient way of doing this. I just tried to make the idea work first)
It is a package with input_text’s as holders for the tag values and an automation with ‘choose’ for each key just to get different message which key was used.
For the assigning part, I used an input_select with al the tag name and an input_boolean is triggering an automation which assign the last read value to the chosen tag from the list.

with the right data structure I might be able to do it in a single automation that looks for the tag value, and if it finds it, it also send a message with the appropriate tag name used.
The structure should also keep a way to dynamically assign tag values if I need to replace one without touching the code…

Did what I wrote helped? or made it worse…

template:
  - trigger:
      - platform: event
        event_type: tag_scanned
      - platform: state
        entity_id: input_select.rfid_keys
   sensor:
     - name: RFID Keys
       state: >-
         {% if trigger.platform == 'event' %}
           {% if trigger.event.data.tag_id in state_attr('sensor.rfid_keys','keys') %}
             {{ trigger.event.data.tag_id }}
           {% else %}
             {% if states('sensor.rfid_keys') in ['unknown','unavailable'] %}
               Ready
             {% else %}
               {{ states('sensor.rfid_keys') }}
             {% endif %}
           {% endif %}
         { % else %}
           {% if states('sensor.rfid_keys') in ['unknown','unavailable'] %}
             Ready
           {% else %}
             {{ states('sensor.rfid_keys') }}
           {% endif %}
         {% endif %}
       attributes:
         keys: "{{ state_attr('input_select.rfid_keys','options') }}"

OK so assuming that the input_select that contains the list of RFID tags is called input_select.rfid_keys then this template sensor will update with the keys from that list. When a tag is scanned, if it is in the list, the state of the sensor will update to the accepted tag, otherwise it will show the last accepted tag.

I don’t have a tag reader though, so I haven’t actually been able to test the template sensor.

Ohh… looks like really sophisticated stuff. I get the idea behind it, looks good but this is my first template sensor so it will take some time for me to understand the pseudo and adapt it to my config.

Ill update once it works, and share it for future users. many thanks!!