Check if code exists, and if it exists, do futher check. Am I doing this completely wrong?

Hi all, I’m trying to achieve something that seemed to be simple, but turns out to be very hard for me to do…

I’ve got an RFID card reader, which sends the code to HA when a badge is scanned.
I got this value in sensor.card_id_3.

Then I wanted to have a list of users, which can be mapped to a specific code.
Also I wanted to be able to configure if they are allowed access or not.
Or even only during specific times…

So… I started making a bunch of variables with the idea of creating the automation later on.
But now I really don’t know how to make the automations… Let alone flexible…

So this is what I got so far:

A bunch of hardcoded variables for each user:

input_text:
  wieganduser1name:
    name: Naam
  wieganduser1code:
    name: Code

input_select:
  wieganduser1accesstype:
    name: Access Type
    options:
      - Nooit
      - Altijd
      - Soms

input_boolean:
  wieganduser1accessday1:
    name: Maandag
  wieganduser1accessday2:
    name: Dinsdag

input_datetime:
  wieganduser1day1from:
    name: Maandag vanaf
    has_date: false
    has_time: true
  wieganduser1day1to:
    name: Maandag tot
    has_date: false
    has_time: true

But this list of variables becomes HUGE (5 days for each user, 10 times for each user, …)
I think I am doing something wrong here already… But I’m not sure how to go about this?

Then the automations, I thought to trigger when sensor.card_id_3 changes.
Then I would need to find if the code is configured. Right now I’d need to check for every variable. But isn’t there a way to make a list to loop through or something?
And then I still need to check what’s the access type… Nooit=Never, Altijd=Always, Soms=Sometimes and then I need to go start checking and comparing the days and hours.

First thing to get right is setting up the variables in the right way I think.
Any tips on that?

I’m not quite able to decipher what you want to achieve, but for this bit, you can make a group of entities and iterate over it by expanding the group.

I agree with Pieter. If you put all the code Input Numbers into a group (in the example I have used group.user_codes) you can pull the user number using expand() and filtering with the trigger’s value. The following automation relies heavily on you maintaining the naming conventions you showed in your examples.

trigger:
  - platform: state
    entity_id: sensor.card_id_3
    to:
condition:
  - variables:
      user: >-
        {{ (expand(group.user_codes) | rejectattr('state', 'in', ['unknown','unavailable','',null]) 
        | selectattr('state', 'eq', trigger.to_state.state)) | map(attribute='object_id') 
        | list | join())[7:-4] }}
      day: '{{ now().isoweekday() | string }}'
      object: '{{ "wiegand"~user~"day"~day }}'
      access_today: '{{ states( "wiegand"~user~"accessday"~day) == "on" }}'
      access_now: >-
        {{ today_at(states("input_datetime."~object~"from")) <= now() <=
        today_at(states("input_datetime."~object~"to")) }}
  - condition: template
    value_template: '{{ trigger.to_state.state != '0' }}'
  - condition: template
    value_template: '{% access_today and access_now %}'
action:
  - service: ....

It’s not really clear how you are trying to implement the access type, so I left it out.

To have this work you will likely also need to automate a reset function for sensor.card_id_3 so this automation will trigger if the same NFC is scanned consecutively. It might be easiest to use a trigger-based template sensor for sensor.card_id_3.