New Integration: Nintendo Switch

It would be nice to be able to automate some things with the Nintendo switch, such as locking it during certain times of the day, or triggering automations based on the status of the Switch. I found a JS API that seems like it could possibly be a starting point for this work, but my JS isn’t real strong. Maybe someone could pick this up and get some basic Nintendo Switch integration?

It would have to be ported over to a python library first.

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

  1. 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.

  1. 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

  1. Install Node.js on your computer.
  2. Open a terminal window and install the CLI:
npm install --global nxapi
  1. Authenticate using your secondary account
nxapi nso auth

Follow the on-screen instructions to get a JSON file with the session token.

  1. Copy the session token to your secrets.yaml file:
NINTENDO_SESSION_TOKEN: "na ..."

Make sure to include “na” and a space before the token.

  1. Obtain your main account’s NSA ID:
nxapi nso friends

Your main account ID should be listed in the NSA ID column.

  1. 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.

4 Likes

Any idea if there’s a reasonable way to get this set up on HAOS?

I saved the “nintendo-switch-activity” file in /usr/share/ns.

When i try to install the docker container I get this error msg:

docker build -t nintendo-switch-activity .
[+] Building 0.1s (2/2) FINISHED                                                                                                                                               	 
 => [internal] load build definition from Dockerfile                                                                                                                       	0.1s
 => => transferring dockerfile: 2B                                                                                                                                         	0.0s
 => [internal] load .dockerignore                                                                                                                                          	0.1s
 => => transferring context: 2B 	

**ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount2731874456/Dockerfile: no such file or directory**

Do you know why this error might accure?

Do I need to set certain privacy settings in order to see my online presence?

Plus, when you say:

Please note that by using this method, you will be sending the token to a third-party server that’s not affiliated with Nintendo

Is nxapi library? that sends info to a 3rd-party server?

Thanks, Salvatore

Hey there! Just wanted to thank you for providing these setup instructions. I am running the Docker container separate on my Synology NAS.

For anyone who is trying to get the Dockerfile to run on their Synology:

  1. Store the dockerfile on your NAS via the usual file sharing methods.
  2. First, download the Docker image for Node from the Docker GUI in DSM. Make sure to select version 18. (I forgot this step and was pulling my hairs out. :slight_smile: )
  3. Access your NAS via SSH and navigate to the folder where you stored the Dockerfile.
  4. Run the commands as described with admin rights (sudo).
  5. The container is now running and available at the port you defined.
1 Like

Ooh, I did not know this data was accessible. I’ll take a look at this when I have time.

1 Like

HAOS gives way more flexibility to control devices. Hope something comes of this thread.

Great thing about switch is it works well offline - like car trips. Down side is i cant use my firewall gateway to block the switch when its homework time.

Has anyone had luck with other integrations first like Apple shortcuts or IFTTT?

I’ve just started working on a Nintendo Switch Parental Controls integration here:

0.0.2 has introduced support to lock / unlock a device (providing you have “suspend software” enabled in the mobile app)

8 Likes

Should anyone want to make multiple sensors, i.e. to see what others in your house are playing:

nxapi users list to show all users who’ve logged on.

nxap users set to set the account whose friends you want to check

2 Likes