Using value_template with regex and case (or if) in automation

I am struggling extracting the data I need from MQTT.
I have a sensor, which receives the mqtt payload:

- sensor:
    state_topic: "nuki/Lock_ID/lockActionEvent"
    name: "Letzter Haustürschloss Benutzer (MQTT roh)"
    icon: mdi:raw
    unique_id: sensor_letzter_hausturschloss_benutzer_mqtt_raw

The result string is e.g.


The last digit is the user ID which I need to extract.
I did this for testing (successful for a single result check) within the template sensor above with (where 1234 sometimes changes):

value_template: '{% if value|regex_search(''3,0,111111,\\d{4},2'', ignorecase=FALSE)%}User{% endif %}"

Because I prefer having the logic in the automation (and combining of if for multiple cases) I am trying to translate the MQTT payload into users with a template like and set a dropdown (input_select) accordingly:

condition: template
value_template: >-
  {{ regex_search('3,0,111111,\\d{4},2') in states('sensor.letzter_hausturschloss_benutzer_mqtt_roh') }

but I get:

Message malformed: invalid template (TemplateSyntaxError: expected token ',', got 'integer') for dictionary value @ data['action'][0]['if'][0]['value_template']

I guess this means that HA transforms the MQTT string into an integer (even though I don’t know how with all these commas).
I also would prefer to use a text_sensor or something, but that’s not possible I guess.

So how do I check the raw string for specific sub-strings to set the input_select accordingly?

Thanks in advance.

I just found the mqtt text option and will try this.
Any help in the meantime would still be appreciated :slight_smile:

That’s not working either

If it is the last digit and (!) there are always 5 elements, then why not use a


or I beleive this will work too

| list | last

That’s a good idea, but does not work here, because I need to check the entire string.


This MQTT message is always the same for unlocking my door with a fingerprint (3,0,111111,\\d{4},2), where just the \\d{4} changes and the last digit of course.
If the Lock is opened via MQTT from HA it looks different:


Because I don’t know what else might be coming through this topic, I would like to check the entire string.

Confused… you stated to only need the last digit above, that is not the case?

Yeah, sorry.
I need the last digit to check which user triggered the lock to open (User 1-4).
The other part of the string possibly contains information about the source (fingerprint sensor on the keypad), which is also needed to know which kind of message arrived.

So what DO you need then…if you use split(‘,’) then you can pick all elements using 0 to 4 … maybe I donot see it but it is not clear for me

Sorry for being confusing :smiley:

I thought there must be an easy way of REGEX’ing the string 3,0,111111,\\d{4},2 within a template.
However, I will try to extract parts of this string with your method and see whether I can achieve what I am after.

Thank you for your immediate help!

Your template is missing the final } so it is being interpreted as a string instead of a template.

You have left out a lot of significant information… does the mapping of user name to user id need to be handled within the template itself or will you be setting up variables for that?

Here’s an example you can play around with… but you need to figure out where/how you want to define the allow list.

condition: template
value_template: |
  {% set input = states('sensor.letzter_hausturschloss_benutzer_mqtt_roh') %}
  {% set user_id = input.split(',')[-1] if input is match('3,0,111111,') else 100 %}
  {{ false if user_id == 100 else user_id in allow_list }}

Thank you for pointing this out.
I am currently trying to move from the sensor template to an automation.
That seems to be the better approach (for me).
I will set a dropdown (input_select) according to the sensor content.