Skip to content

Use NFC-1 events and actions in automations

Difficulty: Level 2

🌱 New here? Try these first:

Run automations from tag scans Difficulty: Level 1

Your Apollo NFC-1 tells Home Assistant about everything it does, from a tag scan to a failed write, and it adds actions that let automations and scripts drive it. By the end of this guide, you'll know every event the NFC-1 fires and every action it offers, and you'll have six ready-made automations and scripts to copy and adapt.

Before you start

How events and actions work

The NFC-1 talks to Home Assistant in two directions:

  • Events come from the NFC-1. The NFC Event entity fires each time something happens, such as a tag scan, a phone tap, or a finished write. Use them as triggers.
  • Actions go to the NFC-1. They arm a write, clean, or format, set the card emulation link, or play a tune. Use them in the actions of an automation or script.

Most automations also read the NFC-1's other entities, such as Last Tag, Last Operation, Phone Present, and Tag Present. See sensor definitions for the full list.

NFC Event types

The NFC Event entity fires one of these event types. Type them exactly as shown, in lowercase with underscores.

Event type When it fires
tag_scanned A tag was scanned in Read mode. This includes cards the NFC-1 can only read the ID (UID) of, such as bank or transit cards. It fires at the same moment the tag shows up in Settings → Tags.
tag_removed The last scanned tag was taken off the reader.
phone_detected A phone was put on the NFC-1. It fires once per visit. Lifting the phone and tapping again within about 5 seconds counts as the same visit. It also fires when a clean or format hits a phone.
emulated_tag_scanned A phone read the link the NFC-1 shares with Card Emulation.
armed A write, clean, or format was armed and is waiting for a tag.
written The NFC-1 wrote the tag and is about to read it back to check it.
cleaned The NFC-1 cleaned the tag and is about to read it back to check it.
formatted The NFC-1 formatted the tag and is about to read it back to check it.
write_verified The NFC-1 read the tag back and it holds exactly what was written.
write_mismatch The NFC-1 read the tag back and it doesn't hold what was written, for example because the message didn't fit on the tag.
write_failed The write didn't happen, for example because there was nothing to write, the text was over 800 bytes, or a phone or unsupported tag was on the reader. Last Operation says which.
clean_verified The NFC-1 read the tag back and it's empty.
format_verified The NFC-1 read the tag back and it's empty.
clean_failed Data was still on the tag, or the clean was refused (a phone or an unsupported tag on the reader), or the tag read back was a phone or an unsupported tag.
format_failed Data was still on the tag, or the format was refused (a phone or an unsupported tag on the reader), or the tag read back was a phone or an unsupported tag.
unverified The tag was lifted before the NFC-1 could read it back, or the task was canceled while it was checking.
timed_out No tag was presented before Mode Timeout ran out.
cancelled An armed task was canceled with Cancel Tag Operation, by setting Mode to Read, or with the cancel_tag_operation action. It doesn't fire if nothing was armed.

A successful write fires armed, then written, then write_verified. A clean fires armed, cleaned, then clean_verified, clean_failed, or unverified if the tag was lifted too soon. Last Operation shows the same result in words. See the write tags guide for every message.

Trigger on an event type

The event entity changes each time it fires, so trigger on the entity and check the event type in a condition. Every NFC Event example on this page uses this pattern:

triggers:
  - trigger: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    not_from: unavailable
conditions:
  - condition: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    attribute: event_type
    state: tag_removed

not_from: unavailable stops the automation from running when the NFC-1 reconnects to Home Assistant after a restart. To match several event types, list them under state:.

Use actions in automations

The NFC-1 adds these actions to Home Assistant. A write, clean, or format action arms the task the same way the buttons on the device page do: the LED pulses (if LED Feedback is on), and the NFC-1 waits up to Mode Timeout (30 seconds by default) for a tag.

Action Field What it does
write_url url Arms a write of a link to the next tag. Up to 800 bytes (accented letters and emoji count as 2 to 4). If you leave off https://, the NFC-1 adds it. Doesn't change the Write URL entity.
write_text text_str Arms a write of plain text to the next tag. Up to 800 bytes (accented letters and emoji count as 2 to 4). Empty text is refused.
write_ha_tag tag_id Arms a write of a Home Assistant tag. Leave tag_id empty for a new random tag. Give an existing tag ID to make a second tag that runs the same automations. The ID is used as typed, apart from trimming spaces. Unlike the New Tag ID box, it isn't turned into lowercase words with dashes, so Front Door stays Front Door.
clean_tag none Arms a clean of the next tag, the same as Clean Tag.
format_tag none Arms a format of the next tag, the same as Format Tag.
cancel_tag_operation none Cancels an armed task, the same as Cancel Tag Operation.
set_emulation_url url Sets Emulation URL, which holds up to 120 characters. If Card Emulation is on, phones get the new link right away. It doesn't turn Card Emulation on. Links longer than 120 characters are ignored, and the old link stays. An empty link turns Card Emulation off.
play_buzzer song_str Plays an RTTTL tune (a short text format for ringtone melodies, such as hello:d=8,o=6,b=180:c,e,g) at the Volume you set. It stops any tune already playing, and plays even when Buzzer is off.

Only one task can be armed at a time. Arming a new one replaces the one that's waiting. While a phone sits on the reader, writes, cleans, and formats are refused.

To try an action before you use it in an automation, go to Settings → Tools → Actions, pick the action, fill in its field, and click Perform action.

The Actions tool in Home Assistant with the NFC-1 write_url action selected, https://wiki.apolloautomation.com typed in the url field, and the Perform action button

Find your entity IDs and action names

Every entity ID and action name includes your NFC-1's device name, which ends with part of its MAC address. The examples on this page use xxxxxx in its place.

1. Go to Settings → Tools → Actions and type nfc in the action box.

Home Assistant action search for nfc listing all eight NFC-1 actions, from esphome.apollo_nfc_1_19b1b4_cancel_tag_operation to esphome.apollo_nfc_1_19b1b4_write_url

Open your Home Assistant instance and show your action tools.

2. Pick any NFC-1 action, then click Go to YAML mode. The action: line shows its full name, such as esphome.apollo_nfc_1_a1b2c3_write_url. The part between apollo_nfc_1_ and _write_url is what replaces xxxxxx.

3. Check an entity ID: go to Settings → Devices & services → ESPHome, open your NFC-1, click an entity such as NFC Event, and click the settings cog. The Entity ID field shows something like event.apollo_nfc_1_a1b2c3_nfc_event.

Open your Home Assistant instance and show an integration.

Add an example to Home Assistant

Each example below is a complete automation or script. Add an automation like this:

1. Go to Settings → Automations & scenes and click Create automation, then Create new automation.

Open your Home Assistant instance and show your automations.

2. Click the three dots in the top right and pick Edit in YAML.

3. Paste the example over what's there.

4. Replace xxxxxx with the part of your NFC-1's name you found above, and swap in your own lights, phones, and tags.

5. Click Save and give the automation a name.

The automation shows up in the list and runs the next time its trigger happens. The script example is added under Scripts instead, as described in that example.

Examples

Get a notification when a write fails

When a write fails or the tag doesn't hold what was written, your phone gets a message with the result from Last Operation.

Notify me when a tag write fails
alias: Notify me when a tag write fails
description: Sends a notification when the NFC-1 can't write a tag or the check doesn't match.
triggers:
  - trigger: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    not_from: unavailable
conditions:
  - condition: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    attribute: event_type
    state:
      - write_failed
      - write_mismatch
actions:
  - action: notify.mobile_app_your_phone
    data:
      title: NFC-1 write failed
      message: "{{ states('sensor.apollo_nfc_1_xxxxxx_last_operation') }}"
mode: queued

Replace notify.mobile_app_your_phone with your phone's notify action. Your phone needs the Home Assistant Companion app. To find its action, go to Settings → Tools → Actions and type notify.mobile_app. To test it, press Write URL Tag with Write URL empty. Your phone gets a notification that the URL is empty.

Write a URL from a script

This script asks for a link when you run it, then arms the NFC-1 to write it to the next tag. Use it from a dashboard button or call it from other automations.

1. Go to Settings → Automations & scenes → Scripts and click Add script, then Create new script.

Open your Home Assistant instance and show your scripts.

2. Click the three dots in the top right, pick Edit in YAML, and paste the script below.

Write a URL to a tag
alias: Write a URL to a tag
description: Arms the NFC-1 to write the link you enter to the next tag.
fields:
  url:
    name: URL
    description: The link to write. https:// is added if you leave it off.
    required: true
    example: www.apolloautomation.com
    selector:
      text:
sequence:
  - action: esphome.apollo_nfc_1_xxxxxx_write_url
    data:
      url: "{{ url }}"
mode: single

3. Replace xxxxxx, then click Save.

Go to Settings → Tools → Actions, pick Write a URL to a tag, type a link in the URL box, and click Perform action. The NFC-1's LED pulses blue if LED Feedback is on. Hold a tag flat on the reader until Last Operation reads Write verified.

To call the script from another automation, use its entity ID, such as script.write_a_url_to_a_tag, with url in its data.

Play a tune and flash a color for one tag

When one specific tag is scanned, the NFC-1 plays a short tune and its LED glows yellow for two seconds. Replace your-tag-id with the tag's ID from Settings → Tags.

Celebrate the guest tag
alias: Celebrate the guest tag
description: Plays a tune and flashes the NFC-1 yellow when the guest tag is scanned.
triggers:
  - trigger: tag
    tag_id: your-tag-id
actions:
  - action: esphome.apollo_nfc_1_xxxxxx_play_buzzer
    data:
      song_str: "hello:d=8,o=6,b=180:c,e,g,8p,c7"
  # Let the scan flash finish first
  - delay:
      milliseconds: 500
  - action: light.turn_on
    target:
      entity_id: light.apollo_nfc_1_xxxxxx_rgb_light
    data:
      rgb_color: [255, 200, 0]
      brightness_pct: 100
  - delay:
      seconds: 2
  - action: light.turn_off
    target:
      entity_id: light.apollo_nfc_1_xxxxxx_rgb_light
mode: single

Scan the tag. The NFC-1 gives its usual green flash and beep, then plays the tune and glows yellow. To hear only your tune, turn off Buzzer under Configuration. play_buzzer still plays. See What the LED and buzzer mean for more on tunes and colors.

A text helper on your dashboard lets anyone in the house change the link phones get, without opening the NFC-1's device page.

1. Go to Settings → Devices & services → Helpers, click Create helper, and pick Text.

Open your Home Assistant instance and show your helper entities.

2. Name it Guest link, set Maximum length to 120, and click Create.

3. Add the automation below, as in Add an example to Home Assistant. If you named the helper something other than Guest link, change input_text.guest_link to match.

Share the guest link
alias: Share the guest link
description: Sends the Guest link helper to the NFC-1 as its card emulation link.
triggers:
  - trigger: state
    entity_id: input_text.guest_link
    not_to:
      - unavailable
      - unknown
actions:
  - action: esphome.apollo_nfc_1_xxxxxx_set_emulation_url
    data:
      url: "{{ trigger.to_state.state }}"
mode: restart

Type a new link into Guest link. Emulation URL on the NFC-1 changes to match, and with Card Emulation on, the next phone that taps gets the new link. Clearing Guest link turns Card Emulation off.

The action doesn't turn Card Emulation on. To do that too, add these lines at the end of actions::

  - action: switch.turn_on
    target:
      entity_id: switch.apollo_nfc_1_xxxxxx_card_emulation

See Let phones tap the NFC-1.

Turn on a light while a phone is on the reader

Phone Present is on while a phone sits on the NFC-1. This automation keeps a light on for as long as it is.

Light on while a phone is present
alias: Light on while a phone is present
description: Turns a light on when a phone is on the NFC-1, and off when it leaves.
triggers:
  - trigger: state
    entity_id: binary_sensor.apollo_nfc_1_xxxxxx_phone_present
    from: "off"
    to: "on"
    id: phone_on
  - trigger: state
    entity_id: binary_sensor.apollo_nfc_1_xxxxxx_phone_present
    from: "on"
    to: "off"
    id: phone_off
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: phone_on
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.hallway
      - conditions:
          - condition: trigger
            id: phone_off
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.hallway
mode: queued

Hold a phone on the NFC-1 and the light turns on. Take it away and the light turns off about 5 seconds later, once the NFC-1 decides the phone has left. With Card Emulation on, it can take a little longer, because the NFC-1 waits for the phone to finish reading the link. See Use phones with the NFC-1.

Pause music when a tag is lifted

Rest a "music" tag on the NFC-1 while you listen, and lift it to pause. tag_removed fires for the last scanned tag, so the condition checks Last Tag to make sure it was this one.

Pause music when the music tag is lifted
alias: Pause music when the music tag is lifted
description: Pauses the living room speaker when the music tag is taken off the NFC-1.
triggers:
  - trigger: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    not_from: unavailable
conditions:
  - condition: state
    entity_id: event.apollo_nfc_1_xxxxxx_nfc_event
    attribute: event_type
    state: tag_removed
  - condition: state
    entity_id: sensor.apollo_nfc_1_xxxxxx_last_tag
    state: your-tag-id
actions:
  - action: media_player.media_pause
    target:
      entity_id: media_player.living_room
mode: single

Replace your-tag-id with the tag's ID from Settings → Tags, which is the same value Last Tag shows. Scan the tag, then lift it. The speaker pauses. Pair it with a tag automation that starts the music when the tag is scanned.

Troubleshooting

Problem What to do
The automation never runs Check that every xxxxxx was replaced, and that the event type is spelled exactly as in the NFC Event types table. Open the automation's Traces to see whether the trigger or a condition stopped it.
The automation runs when the NFC-1 restarts Add not_from: unavailable to the NFC Event trigger.
Home Assistant says the action isn't found Look up the exact action name under Settings → Tools → Actions, as in Find your entity IDs and action names.
Last Operation shows Write refused: URL is empty (set Write URL; Emulation URL is only for Card Emulation) Write URL Tag writes Write URL, not Emulation URL. Fill in Write URL, or use the write_url action with a url.
Last Operation shows Write refused: longer than 800 characters The limit is 800 bytes, not letters: plain letters and numbers count as 1, but accented letters and emoji count as 2 to 4. Shorten the text or link. Smaller tags such as NTAG213 hold about 130 characters, so a long message can also end in Write mismatch.
Last Operation shows Write refused: remove the phone and present an NFC tag A phone was on the reader when the task was armed. Take the phone away and run the action again.
set_emulation_url runs, but phones don't get the link Turn on Card Emulation under Configuration. The action sets the link but doesn't turn emulation on.
Phone Present never turns on Check that Tag Polling under Configuration is on. Tag Polling is disabled by default, so select it on the device page and turn on Enabled in its settings to see it. With it off, the NFC-1 doesn't look for tags or phones. If Card Emulation is on, it only acts as a tag.
play_buzzer is silent Raise Volume under Configuration. Check that song_str is a complete RTTTL tune, with a name, settings, and notes separated by colons.

Next step: learn the lights and sounds

Your automations now react to everything the NFC-1 does. Next, learn what each LED color and sound means, and change the feedback to suit your room.

What the LED and buzzer mean

Join our Discord if you need more help!