This post will cover how I use NFC tags with Home Assistant to trigger automations. I’ve laid out all the information below on how you can use NFC Tags to automate other apps and devices as well!
I really like using Near Field Communication (NFC) for home or device based automations because it is really cheap and the possibilities are endless as you can place them in any environmental condition and on almost any physical object.
Here are some of the ways you could use NFC Tags to trigger home automations:
- Place a tag next to your bed to activate good morning/night routines.
- Place a tag next to a door to automatically unlock a door (presence detection is hard).
- Place a tag on the lawn mower and have an automation to open the Garage Door.
- Place a tag under a bed to trigger an emergency action like trigger alarm.
- Place a tag outside next to patio furniture to turn on lights or music.
- Place a tag next to a lamp with a smart bulb to turn it on or off.
- Place a tag next to the cat litter to trigger a future reminder to change it.
Here are some of the ways you could use NFC Tags to trigger device automations:
- Sharing Guest Wifi information!
- Place under a bed to trigger an emergency action like text emergency services.
- Place a tag by your relaxation place and automatically play relaxing music.
- Put one on your bag and tap it to trigger airplane mode or silence your phone.
A huge shout out to Ryan Hoffman who inspired me to look into using NFC after sharing his experiences with it. I’m not sure I would have stumbled upon NFC Tags otherwise.
What Tag Should I Get?
There are many different types of NFC tags that have different form factors, features (write locking, password protection, number of writes, etc).
I really like these heavy duty Type 2 NFC Tags as they are waterproof, safe for outdoor use and supports all of the NFC features you’d expect (locking, multi-write, password protection). Additionally, they come with 3M adhesive backing so you can literally stick them anywhere!
I could have done more research and found a tag that blended in better with my door, please keep this in mind when picking out where you want to place these.
You can get NFC Tags in sticker form as well. Just be sure it supports your all of your requirements. Here are some factors you might want to consider:
- Location: Some tags are not waterproof and some don’t work on metal surfaces.
- Device support: Will this work with the devices I want to use it with (e.g., iPhone).
- Updatable: Do you need to update this tag once installed (eg., Can I lock it, or write to it multiple times)?
- Form Factor: Does this need to be a sticker?
- Color: Does this blend in with the surrounding?
NFC Tag Tips
It’s important to note that you should associate a name or identifier to a tag
or to the event name you trigger. I’d strongly recommend creating tags with
sequentially or with random characters like nfc_tag_1
and not with a specific
name like office_lights_tag
. The reason behind this is this because you can:
- Potentially leak information publicly as it can be read by anyone with an NFC device.
- Prevents you from reusing tags at a later date.
Finally, don’t store anything you don’t publicly want exposed (e.g., api keys, IP addresses, …). Tags can be copied / cloned with enough work. I’ve yet to figure out how to encrypt the payload of a tag but I’ve seen encrypted tags mentioned around in my various searches. Even still I wouldn’t put anything sensitive on a tag!
Triggering events when NFC is detected.
I’m pretty new to NFC but in my adventures I’ve only come across two ways to trigger Home Assistant events when an NFC Tag is detected. You can do it via the Shortcuts App or via deep/universal linking.
This article primarily focuses on iOS support as I don’t have any android devices to try on. The following links may need to be slightly tweaked. Please leave a comment below on what worked for you and I’ll update this article.
Please note that there are deep links for a wide range of applications that you could use NFC tags on.
Skip to the Conclusion if you are looking for my recommendations on what solution to go with.
Tags
Since writing this article Home Assistant has added the support for Tags. Home Assistant will manage reading and writing tags for you which is really nice. However, it still requires you to click on a banner to fire an event when you scan a tag.
Deep/Universal links
For simplicity sake a deep/universal link will open an application with some arguments contained in the link of which can trigger an action. You can read more about the differences here.
You can do a wide range of actions with links like Call Services, Fire Events or Send a Location.
The advantage of using Deep/Universal links is it allows any authorized user to execute a Home Assistant action with no configuration defined on device.
Deep link
The following Deep link homeassistant://fire_event/nfc_tag_1
will launch the Home Assistant application if it’s installed, fire an event
called nfc_tag_1
and show an alert dialog saying the nfc_tag_1
event has
been called.
This is less than ideal as you don’t really want a notification interrupting you. The application opening is jarring enough.
Deep link without notification
Luckily we can take advantage of a feature called
x-callback-url
.
This allows us to trigger application actions without a notification. We simply
need to modify the url to
homeassistant://x-callback-url/fire_event?eventName=nfc_tag_1
Universal link
Deep links work great as long as the application you are linking to is installed. If it isn’t, than the link will do nothing. This is one of the problems Universal links solve by redirecting you to the App Store if the application isn’t currently installed.
NOTE: Universal links like https://www.home-assistant.io/ios/nfc/ are registered when you install the application and works offline!
Here is the Universal link you’d need to register
https://www.home-assistant.io/ios/nfc/?url=homeassistant://x-callback-url/fire_event?eventName=nfc_tag_1
to fire the action without a notification.
Writing links to NFC Tag
I used the free application called NFC Tools which is also available on the Google Play Store.
Open the application and follow the steps below:
- Select the
Write
button. - Select the
Add a record
button. - Select the
URL / URI
option. - Enter in your Deep/Universal link and select
OK
. - Select the
Write / xx Bytes
button to write the link to the NFC Tag.
When your phone gets close to the NFC tag it will launch a banner prompting you to click on it to activate the event.
This isn’t ideal but it’s in place for security reasons so your phone doesn’t automatically execute tasks.
Shortcuts
Shortcuts is an iOS application made by apple that allows you do device based tasks or automations much like Tasker on Android.
Shortcuts define tasks and automations in the app that can get triggered several ways. Shortcut tasks can be shared across devices easily. However Shortcuts Automations cannot be shared and is currently the only way to trigger an action when an NFC tag is detected without any prompts.
The nice thing about Shortcuts is you can have it automate anything (e.g., texting, application actions, video calling and more).
Shortcut
There are two kind of Shortcuts you can create. The first is referred to a Shortcut and can be shared or triggered from automations or by NFC Tags. The second is called Shortcut Automations and those get triggered automatically based on some kind of action or state like coming into proximity to a NFC Tag. Shortcut Automations cannot currently be shared as of iOS 13.
Creating a new Shortcut
We first need to create a new Shortcut in order for a NFC tag to trigger it.
Open the Shortcuts application and follow the steps below:
- Select the
Create Shortcut
button. - Select the
Add Action
button. - Select the
Apps
button and selectHome Assistant
. - Select the
Fire Event
option. - Enter in the event name you wish to trigger (e.g.,
nfc_tag_1
). - Select
Show More
and enter in{}
for theEvent Data
. - Select the
Next
button. - Enter in the event name as the Shortcut name (e.g.,
nfc_tag_1
). - Select the
Done
button to save the Shortcut.
You can now select on the Shortcut button you just added to fire an event! We can now write to an NFC Tag to tell it to run the Shortcut we just added when the tag is read by our device.
Writing Shortcut to NFC Tag
I used the free application called NFC Tools to write the Shortcut to the NFC Tag.
Open the application and follow the steps below:
- Select the
Write
button. - Select the
Add a record
button. - Select the
Shortcut
option. - Enter in the name of the Shortcut and select
OK
. - Select the
Write / xx Bytes
button to write the link to the NFC Tag.
When your phone gets close to the NFC tag it will launch a banner prompting you to click on it to activate the event.
This isn’t ideal but it’s in place for security reasons so your phone doesn’t automatically execute tasks.
Shortcuts Automations
As stated previously, Automations are setup in the Shortcut apps and cannot be shared across devices. Which means this will become a lot of work to maintain if you have more than one user who will be using NFC Tag for automations. The plus side is you don’t need to write anything to the NFC Tag to use these automations and they run without any user interaction. I think this is the biggest win of this approach.
Open the Shortcuts application and follow the steps below:
- Select the Automation tab.
- Select the
Add
icon button. - Select the
Create Personal Automation
button. - Select the
NFC
option under theSettings Category
. - Select the
Scan
button and scan your NFC Tag. - Enter in the event name as the Shortcut name (e.g.,
nfc_tag_1
). - Select the
Next
button. - Select the
Apps
button and selectHome Assistant
. - Select the
Fire Event
option. - Enter in the event name you wish to trigger (e.g.,
nfc_tag_1
). - Select
Show More
and enter in{}
for theEvent Data
. - Select the
Next
button. - Turn off
Ask Before Running
, unless you wish to be prompted. - Select the
Done
button to save the Automation.
When your phone gets close to the NFC tag it will automatically fire the event.
Home Assistant Automation
Here is a quick little Home Assistant Automation you can use that will get
triggered when your NFC Tag is read and the event is fired (e.g., nfc_tag_1
).
- alias: Toggle light when event is nfc_tag_1 event is triggered
trigger:
- event_data: {}
event_type: nfc_tag_1
platform: event
action:
- service: light.turn_on
data:
entity_id:
- light.office
Note: this automation can be created very easily in Home Assistant Automations User Interface (UI).
Future Plans
Here are some of the things I want to try todo in the future that I think we may need to address in Home Assistant if it’s not already available.
I’d like to find a way to ensure my tag can run from any device without leaking implementation details on how to execute. I think this comes down to potentially adding shared universal deep link that works on various platforms.
Taking this a step further, it would be really nice to have a named access token that any user can access to preform an action. I think of this as a named short link of which I define the action that it can perform server side (e.g., turn on this light or fire this action). These could even be used to anonymously share state as well! But this would expose your Home Assistance instance in some way.
Some further research is needed to see if I can stack NFC Tag actions to have multiple run at the same time or if only supported ones will be executed by a specific device (e.g., If I put Shortcuts first, will it be skipped on Android). This could be used to stack events that get fired or services called.
Finally, it would be nice to figure out how to encrypt my tags just so I can learn more!
Conclusion
UPDATE: Since writing this article Home Assistant has added the support for Tags. I’d recommend using Tags but it still requires you to click on a notification banner to to fire an event. You may want to use both Tags and Shortcut Automations together by first configuring Tags and then setting up the Shortcut Automation.
I’m going to be using the Universal Link route with all my tags as I define the configuration once on what the tag should do and anyone authenticated with the Home Assistant app will be able to fire events.
I do think that Shortcut Automations has the best user experience by automatically triggering actions when NFC Tags are detected. But they are not sharable/synced across devices or accounts makes this a non starter unless you will always own a single Apple Device or you just love maintaining automations long term. I do think I may use Shortcut Automations for some automations of which extra interaction is not ideal like activating good night routines without remembering to press on the banner.
NFC Tags can be used for Home Automation or even device specific tasks so go wild!
Extra reading
Here are some useful links that I came across in my NFC adventures:
Share this post
Twitter
Facebook
Reddit
LinkedIn
Email