Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


TUTORIAL - Sonos Remote VD (using node-sonos-api)


Recommended Posts

Implement Sonos VD using node-http-API running on raspberry PI

 

The following should help if you want to implement an sonos VD that leverages the node sonos http api by jishi

The post consists in two parts

  • Setting up the API on a raspberry pi
  • Creating the VD and summary of other uses of the API and some sample code I use

 

**I'll structure this in a better format in the coming weeks but for now it should be sufficient**

 

I have tested this with Play1, 3, 5 (older version) ZP100, ZP80, ZP90. I don’t have a sonos soundbar or sub but from reading other forums it should work fine for those

I intend to use this more in ‘Press Button’ mode rather than directly as a remote control but it should be good either way.

You can also select what appears in HC2 UI by clicking the ‘main’ checkbox on the appropriate VD button ( I think you can have one button, one label and one slider)

 

Please login or register to see this attachment.

Please login or register to see this attachment.

 

Credit & Reuse:

This work draws on the work of many many people from both this forum as well as other forums. Without these people and the work they have done as well as what they have helped me learn over the last 12 months this wouldn't have been created.

Like all VD’s please feel free to constructively criticise or modify it to meet your needs

 

STEP1: Setup api on node.js device:

This VD requires jishi’s node-sonos-http-api to be installed on a node.js capable device. This can be a RPi, a NAS or something similar. Details on where to get the code and install can be found on Git - 

Please login or register to see this code.

You will also find an excellent blog/issue tracker at the link above for any issues you encounter

 

I have mine installed on a Rpi

 

Added Feb 5th

The fastest way I found to get up and running on a PI if you're unsure and just want to try it (takes about 15 minutes)

 

Use the sonos-api precompiled rpi image at 

Please login or register to see this link.

 (apply to blank sd card)

when you login over SSH (with something like putty) the password is root

Map a network drive or Start Run to \\<IP of Pi>\flash

click apps folder and you'll see the sonos-http-api folder

Get an API from VoiceRSS and create a settings.json file (details in the post) and drop it into the sonos-http-api folder

reboot and that's it - test through browser with something like http://<ip-of-Pi:5005/<playername>/Say/Hello

 

If you want to upgrade to the latest api release

Download the latest zip from 

Please login or register to see this link.

 (green button on right)

In the flash folder rename the sonos-http-api to -old

Rename what you downloaded to to sonos-http-api

Drop in your settings.json file

SSH into the Pi - login = root

cd /flash/apps

cd sonos-http-api

npm install --production

when finished reboot

 

 

DOCKER from @riemers   (thanks :-) )

You could also use docker

Some nas systems include docker too (synology) from the gui. Using docker is easy and simple to give to someone else too, work on Pi3 too. For node-sonos-api there is a docker image 

Please login or register to see this link.

 saves you the hassle of installing all dependency's, assuming you have some knowledge with linux. #

 

Edit - additional observation -  I like the Synology NAS option but mine doesn't reboot in the event of a power outage. The Rpi does reboot. If you use a NAS it might be good to use a UPS or find a way to reboot after an outage

 

Once you have this setup you are ready to move on to Step 2

 

STEP 2 : Import the VD, you will need one VD per sonos zone

 

Please login or register to see this link.

 

Name - The zone name needs to be the same as what it is in native Sonos (try and avoid chars such as /,etc) Spaces are okay

IP Address: This is the IP of the device where the api is installed (This is NOT the IP address of the sonos zone)

Port: Leave at 5005 (can be changed it needed, see git above)

 

Please login or register to see this attachment.

 

STEP 3 : Configure

Play, Stop, Repeat, Shuffle, etc – this should work as is

The parts you may want to modify are as follows

 

=> Volume

I opted for the vol + / vol – as opposed to the slider as I find the sliders difficult to use on a tablet

Vol + / - operates in increments of 2%. This is easy to change in the url string. The Vol 10% button is also easy to change

 

 => Favourites

You will need to modify this to your favourites and how they are named in your sonos setup

I would advise simplifying these names in the native Sonos setup as much as you can. Spaces are okay, you just need to use %20 where you have a space in a name

If you need more of less favourites, please add/delete buttons as required

 

=> Playlists

I don’t really use them but these are easily enough added in a similar way to the favourites above.

 

=> Line-in selection

You can create one button for each line in option on your setup for any connect/connect amp or Zp unit.. (I have 4 on my setup)

You will need to get the UUID for the zone that has the line in physically connected to it. You can get the UUID of that zone by viewing the topology of the zone with the topology url. (drop it into chrome/ff)

Please login or register to see this code.

The format of the url

Please login or register to see this code.

 and you will need to change the last numerical string for your own UUID.

 

=> Grouping Zones

I have included two examples of grouping and ungrouping

The url format is simple and easy to read. below are two examples that were setup on the kitchen zone where I wanted to group the Playroom zone two it and play what was playing on the kitchen zone

 

Group - [playroom joining kitchen and playing kitchen music]

Please login or register to see this code.

Ungroup - [playroom leaving kitchen zone]

Please login or register to see this code.

You could also include a group all and an ungroup all by stacking the commands from each zone under one button.

There are also options to control volume of grouped zones if you like but I haven't explored that yet


 

Beyond this…

The api is very extensive and still undergoing development. There are other functions in the api that could be used and might be worth a glance depending on your system and patterns of use

 

I'll update the post this evening with the actual VD as I can't seem to export it remotely. The node.js api will need to be installed first before the VD will be of any use.

 

Hopefully it will be of use to some people

 

Thanks

-Frank

 

Update: Adding VD file

 

Adding some extra information 

 

The api is very extensive and seems to keep growing
I use it for mainly behind the scenes control rather than me clicking the vd/scenes buttons manually
If you look at

Please login or register to see this code.

for any of your players you’ll see all the state json info and what’s possible to control/trigger from

 

Pretifying the json will show it's structure better

Please login or register to see this code.

 

 

I have a VD that shows me the status of all my players (attached)

main scene code (just add labels :-)

Please login or register to see this code.


Three of my zones use power amps (the are sonos connects) and I use the ‘playstate’ to turn the amp on / off when required. That’s the code at the bottom of the vd

At one stage I was even displaying the image (radio station, music album) on an openremote UI using the uri part of the state json.

 

I use the VoiceRSS TTS on teh http api  but there are others. That all that was there when I found it and it worked well

 

I have TTS to tell me “welcome home”, “good night”, "gate opening/closing", "garage door opening/closing" and all the usual stuff but I vary the volume it at night so it doesn’t wake everybody up. I intend this expand this feedback out so If we come home and the alarm went off it would tell me or that the washing machine has finished, the humidity is still too high in the bathroom and I should open the window (or that a fan was turned on) or some HC2 system info like the available memory went beyond certain thresholds, temp in a room rose or dropped, 

 

My siemens alarm is connected to my HC2 and I keep track of all the status of the alarm zones and when we press the “Leaving Home” button it checks the alarm zones and if one or many are open, it will TTS the open alarm zones in the hall sonos so we know where to go and close. Same thing when we go to bed at night.

 

I found that each of my zones have different volume levels that work at night and during the day. I’ll be using my HomeTable (different topic) to store this info for each zones but it can written directly into the scene/vd. (This is what the the following code is for) 

 

I also plan on using a central TTS engine (simple a scene that take a zone/message and vol) so I can TTS more easily to any player. For now I have specific TTS code on a per player basis.

 

I leverage the global variables that @Sankotronic Weather Basic VD populates and a scene that allows me to send the current weather (temp, humidity, wind speed direction, etc)  as a TTS to our bedroom in the morning as well as the temp of the house as part of our wake up routine 

 

The API has a presets concept.

This allows the user to pre-define a set of grouped players, source and volume. This is a file on the rpi and is referenced by a single command rather than starting one zone and grouping other zones.

An example of its structure is as follows ...

 

Please login or register to see this code.

You may notice that the volume is set as 0 because I use another scene to slowly raise the volume so we are not woken to the sharp shock to a loud song.

 

This simple vd button code is what I use to slowly raise the volume slowly

 

Please login or register to see this code.

 

I use this for morning music upstairs and downstairs as well as when the kids go to bed. (changing the presets on the rpi requires a restart of the api - i normally just reboot the pi )

 

Watch out for radio favorites and playlists - try and not have spaces in the names on sonos and you should have no issue calling them

 

ADDED - Jan 21st

Creating a single button on a VD to cycle through favorites or playlist instead of using 1 per favorite. Every time you click the button it selects the next favorite. I have it limited to 5 favorites but this is easily adjusted. I store the favorite in my HomeTable but it could also be a global variable. I use an array to decode the numerical value stored globally into the actual favorite so I can append to the sonos api call. This will need to be adjusted to suit your system

 

VD button code ....

 

Please login or register to see this code.

I also reflect this on a status label on the VD. 

 

Please login or register to see this code.

 

This display at the top of the VD doesn't account is somebody changes the favorite from the native sonos app.

I'll add code later to keep alignment between what is selected and what is stored in the global variable,

 

This is what it looks like on my setup that is still a work in progress

Please login or register to see this attachment.

x

 

Please see post 163 on this topic on how to get the VD icons dynamic for radio station or line in 

Please login or register to see this attachment.

Please login or register to see this attachment.

Please login or register to see this attachment.

sonos-blu.PNG.d84beb7bbb8f0fe692e214ed67c13a1c.PNGsonos-calm.PNG.409fe13aa4a7ebf2cb23f35b7c0de53a.PNGsonos-droid.PNG.d72d9e0efae1e1195a2b7bdeb6d6cf8a.PNG

 

 

 

Edited by AutoFrank
Link to comment
Share on other sites

  • 1 month later...

Thanks a bunch for this i love node-sonos-http have been using it together with domoticz. Im gonna try this one with fibaro tomorrow.

Link to comment
Share on other sites

  • Topic Author
  • Glad it helps someone...

    The vd stops working from time to time but i mainly use url's directly so i havent bothered to look into it..

    There is also sone nuances with spaces in the say command as it doesnt work with them so i have a function that encodes the utl by replaces the spaces with a '+' and that works well

    I would download the beta as it has volume control over say as well as other features

    I'm on holidays at the moment but can pass on what i have when i get home .....

    Link to comment
    Share on other sites

    Thanks that would be great. I have set it up and tried some it works great so far. I hade to change the stop button to /pause though. I dont think node-sonos-http supports /stop?

    Link to comment
    Share on other sites

  • Topic Author
  • Yes its only a play command, pause command and a play/pause toggle type command

    Link to comment
    Share on other sites

    • 2 months later...

    This is great....always wanted a Sonos setup where I can control grouping of rooms and this is it. Thanks for the instructions, just set all of mine up and modified the VD to suit my needs.

    Link to comment
    Share on other sites

    • 1 month later...

    AutoFrank,

     

    I'm totally new to node.js, but got node.js (v4.2.1) running on my Rpi, together with Apache2.

    Now I'm lost with installing the node-sonos-http-api.

     

    Do I just download this and put it in the Apache2 directory?

    Link to comment
    Share on other sites

    Never mind.. Found it. I think

    CD into "/usr/local/lib/node_modules" and clone it.....

     

    Will report back.

    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, Guest said:

    AutoFrank,

     

    I'm totally new to node.js, but got node.js (v4.2.1) running on my Rpi, together with Apache2.

    Now I'm lost with installing the node-sonos-http-api.

     

    Do I just download this and put it in the Apache2 directory?

     

    There are pretty goo install instructions and issues discussed on git 

    This can be found at 

    Please login or register to see this link.

    Link to comment
    Share on other sites

  • Topic Author
  • 4 minutes ago, Desmo said:

    Ignore me....I'm being stupid :)

     

    no worries 

    this is how I do it

     

    main loop

    Please login or register to see this code.

    and vd setup is the ip address of the player and the port which I think is 1400

     

     

     

     

     

     

     

    Link to comment
    Share on other sites

    Thanks...I thought I had it figured but it's still not showing information. I'll try your code above.

    Link to comment
    Share on other sites

    Okay... Looks like I got it working when calling it like so:

    Please login or register to see this link.

     

    When trying the node-sonos-api VD.vfib it does not work.

    (I've set the zone name to: living room, the IP to 10.0.0.105 and the port to 5005)

     

    Could it be that I use the wrong native? zone name? If so. How do I find it?

    Link to comment
    Share on other sites

  • Topic Author
  • 8 minutes ago, Guest said:

    Okay... Looks like I got it working when calling it like so:

    Please login or register to see this link.

     

    When trying the node-sonos-api VD.vfib it does not work.

    (I've set the zone name to: living room, the IP to 10.0.0.105 and the port to 5005)

     

    Could it be that I use the wrong native? zone name? If so. How do I find it?

    Could you export the VD, send on and I can take a look if you like ...

     

    Two things to watch

    Spaces in the zone name work in a browser but not on the VD lua

    You can add %20 in the VD command or just rename to something like living_room and get rid of the space problem which is what I did.

     

    Also you could try http://zone_ip/room_name/state  ( replacing Ip and room name accordingly)

     From a browser and you should see the json response 

     

    Watch things like vd labels that are case sensitive 

    Edited by AutoFrank
    Link to comment
    Share on other sites

    4 minutes ago, Guest said:

    Could it be that I use the wrong native? zone name? If so. How do I find it?

    You can find that in the Settings in the Sonos App on your phone/computer.

    Link to comment
    Share on other sites

    Autofrank.....looks like your last bit of code worked although I had to remove the first line as it doesn't seem relevant to my system.

     

    Thanks again.

    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, Desmo said:

    Autofrank.....looks like your last bit of code worked although I had to remove the first line as it doesn't seem relevant to my system.

     

    Thanks again.

    Oops ...sorry about that 

    I used a table to manage all my device id's 

     

    I use this api extensively and if you like I can send on some examples later ..

    Link to comment
    Share on other sites

    Some more samples would be great. I currently use your vd from the original post but have modified some of the buttons and settings a little.

    Link to comment
    Share on other sites

  • Topic Author
  • Just now, Desmo said:

    Some more samples would be great. I currently use your vd from the original post but have modified some of the buttons and settings a little.

     now worries - i'll send on some examples but also some scenarios where use it..

    once you get the hand of it you'll have no issues putting your own vd's together

     

    -f

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...