NFC Automation - One automation handle all NFC Tags

I am working on a project to provide a sensory room for my autistic grandson. The basic idea is for him to be able to put an NFC tag (disguised as a coin) on an NFC tag reader and have it kickoff a combination of music, LED lights, laser lights, etc.

I’ve got the general flow and integration working but I’m looking for a more elegant way of kicking off the “show”. Currently I have a script for the lights, music , etc associated with each NFC tag. These scripts are orchestrated by an automation and the automation is kicked off by an NFC tag read. Right now I’ve got one automation associated with each NFC tag.

TO reduce the amount of effort coordinating tags, automations and scripts I would like to consolidate all the automations into a single tag read triggered automation. The automation would then determine which tag was read and kick off the appropriate script.

Does anyone have any suggestions or pointers on how to do something like I’ve described?

The following example will run a script associated with a specific tag. For example, if tag_id “A7-6B-90-5F” is scanned, the automation will execute script.play_music

alias: example
trigger:
  - id: play_music
    platform: tag
    tag_id: A7-6B-90-5F
  - id: flash_lights
    platform: tag
    tag_id: C5-2E-40-9F
  - id: something_else
    platform: tag
    tag_id: D3-5A-E3-78
condition: []
action:
 - service: script.turn_on
   target:
     entity_id: "script.{{ trigger.id }}"
mode: queued

Thank You. Greatly appreciated.

I have two scripts.

  • Test1 turns the LED lights Red
  • Test2 turns the LED lights Blue

I have two NFC Tags

  • Test 1
  • Test 2

I am running into an error. The automation runs to the point of entity_id: script.{{trigger-id}}"

Then I get the error:

Stopped because an error was encountered at May 3, 2023 at 5:37:35 PM (runtime: 0.18 seconds) 'int' object is not iterable

When I look at the trace for the trigger I get this:

entity_id: automation.testharness
state: 'on'
attributes:
last_triggered: '2023-05-03T21:34:00.105498+00:00'
mode: single
current: 0
id: '1682701170767'
friendly_name: TestHarness
last_changed: '2023-05-03T21:37:16.647596+00:00'
last_updated: '2023-05-03T21:37:16.647596+00:00'
context:
id: 01GZHRRMB703RXX5WKPGXJ69T9
parent_id: null
user_id: null
trigger:
id: '1'
idx: '1'
alias: null
platform: tag
event:
event_type: tag_scanned
data:
tag_id: 04-53-5D-01-D7-05-03
device_id: 83b47e593fd6c5f30c61e93f9e13469e
origin: LOCAL
time_fired: '2023-05-03T21:37:35.117800+00:00'
context:
id: 01GZHRS6CDJ068Z493PTC7BNRZ
parent_id: null
user_id: null
description: Tag scanned

Do I have to use the numeric value for the tag id as the script name ? I’m using the tag friendly name as the script name (eg Tag Named Test1 kicks of script Test1).

In your version of the automation, does it contain trigger-id with a hyphen or trigger.id with a period (like in my version)?

ah. hyphen. thanks

just checked. here’s my yaml:

alias: TestHarness
description: ""
trigger:
- platform: tag
tag_id: 04-43-D9-01-B6-05-03
- platform: tag
tag_id: 04-53-5D-01-D7-05-03
condition: []
action:
- service: script.new_script
data: {}
- service: script.turn_on
target:
entity_id: "{{trigger.id}}"
data: {}
mode: single

If we compare that to what I had suggested in my previous post, I think we can both agree that there’s only a passing resemblance.

Try this version:

alias: example
trigger:
  - id: play_music
    platform: tag
    tag_id: 04-43-D9-01-B6-05-03
  - id: flash_lights
    platform: tag
    tag_id: 04-53-5D-01-D7-05-03
condition: []
action:
 - service: script.turn_on
   target:
     entity_id: "script.{{ trigger.id }}"
mode: queued

Replace play_music and flash_lights with the actual names of the scripts you are using.

Ah, now I understand. Thank you

After you modify the example I posted above, post it so I can confirm it’s correct.

Sorry for the delay, I was out of town for a few days

Here’s what I ended up with:

alias: StartScripts when tag scanned
description: ""
trigger:
- id: KidsRock
platform: tag
tag_id: 04-53-73-01-99-05-03
- id: Disney
platform: tag
tag_id: 04-43-BB-01-77-05-03
- id: Halloween
platform: tag
tag_id: 04-53-61-01-DF-05-03
condition: []
action:
- service: script.new_script
data: {}
- service: script.turn_on
target:
entity_id: script.lighting_{{trigger.id}}
data: {}
- service: script.turn_on
target:
entity_id: script.playlist_{{trigger.id}}
data: {}
mode: single

Not sure why your automation’s indentation is all wrong or why it contains superfluous and duplicated service calls, but here’s the corrected version:

alias: StartScripts when tag scanned
description: ""
trigger:
  - id: kidsrock
    platform: tag
    tag_id: 04-53-73-01-99-05-03
  - id: disney
    platform: tag
    tag_id: 04-43-BB-01-77-05-03
  - id: halloween
    platform: tag
    tag_id: 04-53-61-01-DF-05-03
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: 'script.lighting_{{trigger.id}}'
mode: single

If you have a script named script.kidsrock then you can’t refer to it as script.KidsRock. That’s why I made all of the trigger.id values lowercase.

I have a similar problem. Currently, I want to merge 20 automations into one. It’s an automation that, after scanning an NFC tag that is located on the spice jar’s cap, plays the name of the spice that is in the jar through the speakers in the kitchen. The automations work in versions where one tag equals one spice. The automation combining them into one is problematic. I kindly ask for help. What could possibly be wrong?

alias: Kuchnia P2 - Tag NFC - TTS - Przyprawy
description: >-
  Po zeskanowaniu tagu NFC (znajduje się on na nakrętce słoiczka) w głośnikach w
  Kuchni P2 odtwarzana jest nazwa przyprawy znajdującej się w danym słoiczku.
trigger:
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_bazylia
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_cukier_wanilinowy
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_curry
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_cynamon
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_czosnek_granulowany
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_galka_muszkatolowa
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_imbir
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_lubczyk
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_majeranek
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_oregano
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_papryka_ostra
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_papryka_slodka
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_pieprz_cayenne
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_pieprz_czarny_mielony
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_pieprz_czarny_ziarnisty
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_pieprz_ziolowy
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_przyprawa_do_piernika
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_tymianek
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_ziele_angielskie
  - platform: tag
    tag_id: tag_nfc_kuchnia_tts_ziola_prowansalskie
condition: []
action:
  - service: tts.cloud_say
    data:
      entity_id: media_player.glosniki_w_kuchni_p2
      message: |
        {% if tag_id == 'tag_nfc_kuchnia_tts_bazylia' %}
          Bazylia
        {% elif tag_id == 'tag_nfc_kuchnia_tts_cukier_wanilinowy' %}
          Cukier Wanilinowy
        {% elif tag_id == 'tag_nfc_kuchnia_tts_curry' %}
          Curry
        {% elif tag_id == 'tag_nfc_kuchnia_tts_cynamon' %}
          Cynamon
        {% elif tag_id == 'tag_nfc_kuchnia_tts_czosnek_granulowany' %}
          Czosnek Granulowany
        {% elif tag_id == 'tag_nfc_kuchnia_tts_galka_muszkatolowa' %}
          Gałka Muszkatołowa
        {% elif tag_id == 'tag_nfc_kuchnia_tts_imbir' %}
          Imbir
        {% elif tag_id == 'tag_nfc_kuchnia_tts_lubczyk' %}
          Lubczyk
        {% elif tag_id == 'tag_nfc_kuchnia_tts_majeranek' %}
          Majeranek
        {% elif tag_id == 'tag_nfc_kuchnia_tts_oregano' %}
          Oregano
        {% elif tag_id == 'tag_nfc_kuchnia_tts_papryka_ostra' %}
          Papryka Ostra
        {% elif tag_id == 'tag_nfc_kuchnia_tts_papryka_slodka' %}
          Papryka Słodka
        {% elif tag_id == 'tag_nfc_kuchnia_tts_pieprz_cayenne' %}
          Pieprz Cayenne
        {% elif tag_id == 'tag_nfc_kuchnia_tts_pieprz_czarny_mielony' %}
          Pieprz Czarny Mielony
        {% elif tag_id == 'tag_nfc_kuchnia_tts_pieprz_czarny_ziarnisty' %}
          Pieprz Czarny Ziarnisty
        {% elif tag_id == 'tag_nfc_kuchnia_tts_pieprz_ziolowy' %}
          Pieprz Ziołowy
        {% elif tag_id == 'tag_nfc_kuchnia_tts_przyprawa_do_piernika' %}
          Przyprawa Do Piernika
        {% elif tag_id == 'tag_nfc_kuchnia_tts_tymianek' %}
          Tymianek
        {% elif tag_id == 'tag_nfc_kuchnia_tts_ziele_angielskie' %}
          Ziele Angielskie
        {% elif tag_id == 'tag_nfc_kuchnia_tts_ziola_prowansalskie' %}
          Zioła Prowansalskie
        {% endif %}
mode: single

try adding variables to each tag; like this

- id: Video-Freeze
    platform: tag
    tag_id: 04-43-EB-01-74-05-03
    variables:
      content_type: video
      playlist_id: null
      media_id: SnU6YUhSQdg

then you can use the variables to drive other logic

is this only usable if you are directly scanning the NFC tag via the native mobile HA app?
I’m on iOS and want to get into using more NFC tags once I upgrade my ancient phone. But my wifes supports background scanning but seems laborious to have to script everything in ios/share per phone…etc.
If the tag id is possible to pass to HA and then u can just switch case the auotmations based on Tag that would be huge.