How to pass an NFC tag_id as variable to a script that will wait_for_trigger that tag to be scanned before continuing

Hi

I’m trying to have script that may be triggered in different ways, and the script will have a wait_for_trigger command that will wait for some NFC tag to be scanned. It will be the calling automation or script that will tell the script which tag to check for.

I’m desperately trying to get the right syntax for that but with no success. What am I doing wrong?

Here’s an example of the calling script:

alias: Asktocheckatag
sequence:
  - service: script.tagchecker
    data:
      nfctagvariable: 1%%%%%-%%%-%%%-%%%-%%%%%%%%0
mode: single

And here is the script that will take the variable and wait 10 seconds for that tag to be scanned:

alias: Tagchecker
sequence:
  - wait_for_trigger:
      - platform: tag
        tag_id: "{{ nfctagvariable }}"
        id: tag_is_scanned
    timeout: "10"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.id == 'tag_is_scanned' }}"
        sequence:
          - service: notify.mobile_app_phone
            data:
              message: The tag {{ nfctagvariable }} has been scanned on time.
    default:
      - service: notify.mobile_app_phone
        data:
          message: The tag {{ nfctagvariable }} has not been scanned on time.
mode: single

For some reason, the script never recognizes the tag. What am I doing wrong?

Probably because you didn’t define fields in the script definition.

Why do you need a script to call another script for this case?
Why don’t you use a script/service that will trigger an automation?

I checked the docs but I don’t see what fields I should define and how in this case.
When I run the calling script, the variable nfctagvariable is passed successfully because the message The tag 1%%%%%-%%%-%%%-%%%-%%%%%%%%0 has not been scanned on time. is written with the right tag id in it.
When I replace

  - wait_for_trigger:
      - platform: tag
        tag_id: "{{ nfctagvariable }}"
        id: tag_is_scanned

by

  - wait_for_trigger:
      - platform: tag
        tag_id: 1%%%%%-%%%-%%%-%%%-%%%%%%%%0
        id: tag_is_scanned

then the wait_for_trigger succeeds to detect that I scanned the NFC tag on time with the right message.

So, it appears that it’s when using the wait_for_trigger with "{{ nfctagvariable }}" that for some reason it doesn’t get the TAG id that is looked for.

The reason why I chose this way of doing it that it can be triggered in different ways, for different reasons. This is a simplified model of what I actually want to do. But at the end there will be a certain number of actions, then it will wait for certain triggers, either time or tag scanning (it could be different tags depending of what is wanted) or a combination of both, all depending on certain conditions.

So I thought that doing it this way was the best to do all kind of stuff in the most integrated way.

I tried different things, but nothing works. There is some mystery in the right syntax to use.

Or is there a way I could create a custom event I could be listening to in the wait_for_trigger to do the same job?

I’m still pretty new at all this and I don’t find it easy to progress.

Based on the documentation for Tag Trigger, it doesn’t appear to support templates for tag_id.

I’m not sure if there’s a simple workaround. An Event Trigger can listen for a tag_scanned event_type but it doesn’t support templates to listen for a specific tag_id. It would have to be tested in a condition after the Event Trigger triggered. However if the scanned tag isn’t a match then you have to repeat the Event Trigger (i.e. doable but more complicated).

1 Like

Hooray! Found a general solution that’s working! I just got my level upgraded in programming HA.

  1. An automation to handle tag scanning events, and create a custom event:
alias: Tag_handler
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
condition: []
action:
  - event: tag_custom
    event_data:
      tag_custom_id: "{{ trigger.event.data.tag_id }}"
mode: single
  1. Then, I call have some different automations/scripts that will call the script:
alias: Ask_to_check_a_tag
sequence:
  - service: script.turn_on
    target:
      entity_id: script.tag_checker
    data:
      variables:
        nfc_tag_variable: 1%%%%%-%%%-%%%-%%%-%%%%%%%%0
mode: single
  1. The script that will execute a certain number of actions, then wait 10 seconds for the tag to be scanned to continue:
alias: Tag_checker
sequence:

#some actions are done here before the wait_for_trigger, and then:

  - wait_for_trigger:
      - platform: event
        event_type: tag_custom
        event_data:
          tag_custom_id: "{{ nfc_tag_variable }}"
        id: tag_is_scanned
    timeout: "10"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.id == 'tag_is_scanned' }}"
        sequence:
          - service: notify.mobile_app_phone
            data:
              message: The tag "{{ nfc_tag_variable }}" has been scanned on time.
    default:
      - service: notify.mobile_app_phone
        data:
          message: The tag "{{ nfc_tag_variable }}" has not been scanned on time.
mode: single

I see you have employed the workaround I mentioned (Event Trigger). However, why is the first automation required? All it does is convert one event (tag_scanned) into another event (tag_custom).

It seems to me that the script’s wait_for_trigger can listen for the tag_scanned event directly and specifically for the desired tag_id (passed as a variable).

alias: Tag_checker
sequence:

#some actions are done here before the wait_for_trigger, and then:

  - wait_for_trigger:
      - platform: event
        event_type: tag_scanned
        event_data:
          tag_id: "{{ nfc_tag_variable }}"
    timeout: "10"
  - if: "{{ wait.trigger is not none }}"
    then:
      - service: notify.mobile_app_phone
        data:
          message: The tag "{{ nfc_tag_variable }}" has been scanned on time.
    else:
      - service: notify.mobile_app_phone
        data:
          message: The tag "{{ nfc_tag_variable }}" has not been scanned on time.
mode: single
1 Like

You’re right! Thank you!

1 Like