Skip to main content

IOT button remote / Spotify control integration with MQTT

Workflow preview

Workflow preview
100%
IOT button remote / Spotify control integration with MQTT preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

This template integrates an IOT multi button switch (meant for controlling a dimmable light) with Spotify playback functions, via MQTT messages. This isn't likely to work without some tinkering, bu...

Best for

  • Miscellaneous automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.spotify, n8n-nodes-base.stickynote, n8n-nodes-base.httprequest, n8n-nodes-base.splitout, n8n-nodes-base.filter, n8n-nodes-base.switch, n8n-nodes-base.set, n8n-nodes-base.noop

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Hubschrauber.

Original n8n.io source

1.1 Workflow description

Title
IOT button remote / Spotify control integration with MQTT
Workflow name
IOT button remote / Spotify control integration with MQTT

Overview

This template integrates an IOT multi-button switch (meant for controlling a dimmable light) with Spotify playback functions, via MQTT messages. This isn't likely to work without some tinkering, but should be a good head start on receiving/routing IOT/MQTT messages and hooking up to a Spotify-like API.

Requirements

  • An IOT device capable of generating events that can be delivered as MQTT messages through an MQTT Broker
  • An MQTT Broker to which n8n can connect and consume messages
    • e.g. Zigbee2MQTT in HomeAssistant
  • A Spotify developer-account (which provides access to API functions via OAuth2 authorization)
  • A Spotify user-account (which provides access to Spotify streamed content, user settings, etc.)

Setup

  1. Create an MQTT Credential item in n8n and assign it to the MQTT Trigger node
  2. Modify the MQTT trigger node to match the topic for your IOT device messages
  3. Modify the switch/router nodes to map to the message text from your IOT button (e.g. arrow_left_click, brightness_up_click, etc.)
  4. Create a Spotify developer-account (or use the login for a user-account)
  5. Create an "App" in the developer-account to represent the n8n workflow
    • Chicken/Egg ALERT: The n8n Spotify Credentials dialog box will display the "OAuth Redirect URL" required to create the App in Spotify, but the n8n Credential item itself cannot be created until AFTER the App has been created.
  6. Create a Spotify Credentials item in n8n
    • Open the Settings on the Spotify App to find the required Client ID and Client Secret information.
    • ALERT: Save this before proceeding to the Connect step.
  7. Connect the n8n Spotify Credential item to the Spotify user-account
    • ALERT: Expect n8n to open a separate OAuth2 window on authorization.spotify.com here, which may require a login to the Spotify user-account
  8. Open each of the HTTP and Spotify nodes, one by one, and re-assign to your Spotify Credential (try not to miss any).
  9. Modify the variable values in the Globals node to match your own environment.
    • target_spotify_playback_device_name - The name of a playback device available to the Spotify user-account
    • favorite_playlist_name - The name of a playlist to start when one of the button actions is indicated in the MQTT message. Used in example "Custom Function 2" sequence.

Notes

  • You're on your own for getting the multi-button remote switch talking to MQTT, figuring out what the exact MQTT topic name is, and mapping the message parts to the workflow (actions, etc.).
  • The next / previous actions are wired up to not transfer control to the target device. This alternative routing just illustrates a different behavior than the remaining actions/functions, which include activation of the target device when required.
  • Some of the Spotify API interactions use the Spotify node in n8n, but many of the available Spotify API functions are limited or not implemented at all in the Spotify node. So, in other cases, a regular HTTP node is used with the Spotify OAuth2 API credential instead. By modifying one of the examples included in the template, it should be possible to call nearly anything the Spotify API has to offer.

Spotify+n8n OAuth Mini-Tutorial

Definitions

  • The developer-account is the Spotify login for creating a spotify-app which will be associated with a client id and client secret.
  • The user-account is the Spotify login that has permission to stream songs, set up playback devices, etc.
  • ++A spotify-login allows access to a Spotify user-account, or a Spotify developer-account, OR BOTH++
  • The spotify-app, which has a client id and client secret, is an object created in the developer-account.
  • The app-implementation (in this case, an ++n8n workflow++) uses the spotify-app's credentials (client id / client secret) to call Spotify API endpoints on behalf of a user-account.

Using One Spotify Login as Both User and Developer

When an n8n Spotify-node or HTTP-node (i.e. an app-implementation) calls a Spotify API endpoint, the Credentials item may be using the client id and client secret from a spotify-app, which was created in a developer-account that is ++one and the same spotify-login as the user-account++. However, it helps to remind yourself that from the Spotify API server's perspective, the developer-account + spotify-app, and the user-account, are ++two independent entities++.

n8n Spotify-OAuth2-API Credential Authorization Process

The 2 layers/steps, in the process of authorizing an n8n Spotify-OAuth2-API credential to make API calls, are:

  1. n8n must identify itself to Spotify as the app-implementation associated with the developer-account/spotify-app by sending the app's credentials (client id and client secret) to Spotify.
    • The Client ID and Client Secret are supplied in the n8n Spotify OAuth2 Credentials UI/dialog-box
  2. Separately, n8n must obtain an authorization token from Spotify to represent the permissions granted by the user to execute actions (call API endpoints) on behalf of the user (i.e. access things that belong to the user-account).
    • This authorization for the user-account access is obtained when the "Connect" or "Reconnect" button is clicked in the n8n Spotify Credentials UI/dialog-box (which pops up a separate authorization UI/browser-window managed by Spotify).
    • The Authorization for a given spotify-app stays "registered" in the user-account until revoked.
    • More than one user-account can be authorized for a given spotify-app. A particular n8n Spotify-OAuth2-API credential item appears to cache an authorization token for the user-account that was most recently authorized.
      • Up to 25 users can be allowed access to a spotify-app in Developer-Mode, but any user-account other than the one associated with the developer-account must be added by email address at https://developer.spotify.com/dashboard/{{app-credential-id}}/users
    • ALERT: IF the browser running the n8n UI is ALSO logged into a Spotify account, and the spotify-app is already authorized for that Spotify account, the "reconnect" button in the Spotify-OAuth2-API credential dialog may automatically grab a token for that logged in user-account, offering no opportunity to select a different user-account.
      • This can be managed somewhat by using "incognito" browser windows for n8n, Spotify, or both.

References

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Spotify Next

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 2 - Spotify Resume

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 3 - Spotify Pause

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 4 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 5 - Spotify API - Volume up 5pct

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 6 - Spotify API - Volume down 5pct

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 7 - Sticky Note8

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 8 - Spotify API - Get Available Devices

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 9 - Extract Individual Devices

Type / Role
n8n-nodes-base.splitOut - splitOut
Config choices
Version 1

Block 10 - Select Device by Name to get device_id

Type / Role
n8n-nodes-base.filter - filter
Config choices
Version 2

Block 11 - Custom Function 1 - P1

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 12 - Custom Function 2 - P2

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 13 - Custom Function 1 - P3

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 14 - Spotify API - Activate Target Playback Device

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 15 - Route to Requested Function

Type / Role
n8n-nodes-base.switch - switch
Config choices
Version 3

Block 16 - Custom Function 2 - P1

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 17 - Filter

Type / Role
n8n-nodes-base.filter - filter
Config choices
Version 2

Block 18 - Globals

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 19 - Custom Function 2 - P3

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Block 20 - Oops. How was this reached?

Type / Role
n8n-nodes-base.noOp - noOp
Config choices
Version 1

Block 21 - Spotify API - Get Device Status

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.1

Block 22 - Already playing on Target Device?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2

Block 23 - Is Playing?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2

Block 24 - Spotify Prev

Type / Role
n8n-nodes-base.spotify - spotify
Config choices
Version 1

Showing the first 24 of 26 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow IOT button remote / Spotify control integration with MQTT
Complexity advanced
Nodes 26
Categories Miscellaneous
Author Hubschrauber
Published 18 Aug 2024

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/2383/2383.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does IOT button remote / Spotify control integration with MQTT do?

This template integrates an IOT multi button switch (meant for controlling a dimmable light) with Spotify playback functions, via MQTT messages. This isn't likely to work without some tinkering, bu...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Miscellaneous use case.