I was inspired by the PS5-MQTT integration (PS5-MQTT: Control PlayStation 5 devices using MQTT) and wanted to create similar functionality for the Nintendo Switch. I discovered Samuel Elliot’s library for displaying current gaming information on Discord and utilized the HTTP proxy server included in the library and a Rest sensor.
The method works by monitoring your friend’s status on the Nintendo Switch Online account. To implement this, you first need to create a second account, befriend your main account, and use a Docker container running an HTTP server.
Docker Setup
- Create a Dockerfile with the following content:
FROM node:18
RUN npm install --global nxapi
ENV NXAPI_DATA_PATH=/data
ENV NODE_ENV=production
ENV NXAPI_USER_AGENT="ha-nso/1.0.0 (+https://www.home-assistant.io/)"
VOLUME [ "/data" ]
CMD [ "nxapi", "nso", "http-server", "--listen", "0.0.0.0:50781" ]
You can use any port number instead of 50781.
- Build and run the Docker container:
docker build -t nintendo-switch-activity .
docker run -d -p 50781:50781 --name nintendo-switch-activity nintendo-switch-activity
Home Assistant Configuration
sensor:
- platform: rest
unique_id: nintendo_switch_activity
name: "Nintendo Switch Activity"
resource: [YOUR-CONTAINER-URL]/api/znc/friend/[NSA ID]/presence
headers:
Authorization: !secret NINTENDO_SESSION_TOKEN
scan_interval: 30
json_attributes_path: $.game
json_attributes:
- name
- imageUri
- shopUri
- totalPlayTime
- firstPlayedAt
value_template: "{{ value_json.state }}"
Replace [YOUR-CONTAINER-URL] with the actual URL of the Docker container running the HTTP server and [NSA ID] with the ID of the account you want to monitor.
Obtaining Nintendo Account Session Token and NSA ID
- Install Node.js on your computer.
- Open a terminal window and install the CLI:
npm install --global nxapi
- Authenticate using your secondary account
nxapi nso auth
Follow the on-screen instructions to get a JSON file with the session token.
- Copy the session token to your secrets.yaml file:
NINTENDO_SESSION_TOKEN: "na ..."
Make sure to include “na” and a space before the token.
- Obtain your main account’s NSA ID:
nxapi nso friends
Your main account ID should be listed in the NSA ID column.
- Replace the placeholders in the Rest sensor configuration with the obtained values and restart Home Assistant.
You should now have a sensor displaying the Nintendo Switch activity like this:
Please note that by using this method, you will be sending the token to a third-party server that’s not affiliated with Nintendo. An Android device running the Nintendo Switch app is needed to complete the authentication and retrieve the tokens. On Samuel’s repo, there are instructions on how to run this yourself using a ReDroid Android emulator in a Docker container. This is likely to be more secure but also more complex. Personally, I didn’t mind it too much and opted to use the one Samuel has made available. You should make your own assessment of the risks involved before proceeding.
I thought of sharing this in case anyone finds it useful.
