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?
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?
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: 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.
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).
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