This node.js application is a bridge between the Sonos and a mqtt server. The status of all your sonos devices will be published to mqtt and you can control the sonos speakers over mqtt.
It's intended as a building block in heterogenous smart home environments where an MQTT message broker is used as the centralized message bus. See MQTT Smarthome on Github for a rationale and architectural overview.
Check out the other bridges in the software list
Using sonos2mqtt is really easy, but it requires at least Node.js v8 or higher, because of it's async usage. (This app is tested against v8 and v9).
sudo npm install -g sonos2mqtt
sonos2mqtt 1.4.4
A smarthome bridge between your sonos system and a mqtt server.
Usage: sonos2mqtt [options]
Options:
-v, --verbosity Verbosity level
[choices: "error", "warn", "info", "debug"] [default: "info"]
-n, --name instance name. used as mqtt client id and as prefix
for connected topic [default: "sonos"]
-u, --url mqtt broker url. See
https://github.com/mqttjs/MQTT.js#connect-using-a-url
[default: "mqtt:https://127.0.0.1"]
-d, --publish-distinct Publish distinct track states
[boolean] [default: false]
-h, --help Show help [boolean]
--version Show version number [boolean]
Use the MQTT url to connect to your specific mqtt server. Check out mqtt.connect for the full description.
Connection without port (port 1883 gets used)
[protocol]:https://[address] (eg. mqtt:https://127.0.0.1)
Connection with port
[protocol]:https://[address]:[port] (eg. mqtt:https://127.0.0.1:1883)
Secure connection with username/password and port
[protocol]:https://[username]:[password]@[address]:[port] (eg. mqtts:https://myuser:[email protected]:8883)
Every message starts with a prefix (see usage) that defaults to sonos
. So if you change this all the topics change.
This bridge uses the sonos/connected
topic to send retained connection messages. Use this topic to check your sonos bridge is still running.
0
or missing is not connected (set by will functionality).1
is connected to mqtt, but not to any sonos device.2
is connected to mqtt and at least one sonos speaker. (ultimate success!)
The status of each speaker will be published to sonos/status/speaker_name/subtopic
as a JSON object containing the following fields.
val
The value for this subtopicname
The name of the speakerts
Timestamp of this message
By default you can subscribe to the following subtopics state
(retained), volume
(retained), muted
(retaind) and track
(not retained) but if you wish to have separate topics for the track values you can specify the -d
or --publish-distinct
parameter and you will get the artist
title
album
and albumart
topics.
You can control sonos by sending an empty message on these topics. The topic format is like sonos/set/room_name/command
for instance sonos/set/Office/next
.
Speaker commands:
next
- Play the next songprevious
- Play the previous songpause
- Pause playingplay
- Resume playbacktoggle
- Toggle betweenpause
andplay
stop
- Stop playback (you better use pause!)selecttrack
(payload requires number) - Select an other track in the queue.seek
- Skip to position in track, payload needs a relative time like0:03:45
to skip to 3 min, 45 sec.volume
(payload requires number) - Set the volume to certain level between 0 and 100volumeup
(payload number optional) - Increse the volume by number from payload or by 5volumedown
(payload number optional) - Decrese the volume by number from payload or by 5mute
- Mute the volumeunmute
- Unmute the volumesleep
(payload requires number) - Set a sleeptimer for x amount of minutes (from payload)notify
- Play a notification on this device 🎉 (and revert to current state) see parametersqueue
- add a song to the queue, payload should be json string or uri. See parameterssetavtransporturi
- See parametersjoingroup
- Join a group by device name, payload should be a string with the name of the deivce to join.leavegroup
- Leave a group.playmode
- Set the playmode,payload should be NORMAL, REPEAT_ALL, SHUFFLE or SHUFFLE_NOREPEAT.command
- One topic for all commands. Payload like{"cmd":"volumeup"}
or{"cmd":"volume", "val":10}
for commands that need a payload.
There are also some genir commands not tied to a specific speaker. These generic commands should be send to sonos/cmd/command
like sonos/cmd/pauseall
.
Generic commands:
pauseall
- Pause all speakers know to the bridge.listalarms
- This will fetch all the current alarms and sends them tosonos/alarms
.setalarm
- This allows you to set/unset an alarm. Requires json object withid
andenabled
notify
- Play a notification on all devices (and revert to current state) see parameters
To play a notification on all devices you send the following json string to sonos/cmd/notify
{
"uri": "https://archive.org/download/Doorbell_1/doorbell.mp3",
"volume": 10
}
If your want to test this library it's best to create a mqtt server just for testing. This can easily be done with the followinf docker command:
docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
Use PM2 to run in background
If everything works as expected, you should make the app run in the background automatically. Personally I use PM2 for this. And they have a great guide for this.
This library depends on node-sonos that I just completly promistified. All other libraries using node-sonos should also be able to implemented all the nice features included there. Like notifications which is the coolest new addition for sonos2mqtt!
This bridge and my work on the sonos library took me quite some time, so I invite everyone using this bridge to Buy me a beer.
The latest version of this bridge is inspired on hue2mqtt.js by Sabastian Raff. That was a great sample on how to create a globally installed, command-line, something2mqtt bridge.