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

  • Topic Author
  • 49 minutes ago, 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.

     

    Hi, @

    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

     

    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 

    Please login or register to see this code.

     

    The API has a presets concept.

    This allows the user to pre-define a set of grouped players, source and volume. This is a json file on the rpi and is referenced by a single command rather than starting one zone and grouping other zones. 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 )

     

    I expanded the use of this for the morning so now the process (for the sonos parts ) is as follows
    1. Use a 'api preset' to group the zones but at zero volume (master bedroom, landing  and the kids bedrooms) 
    2. Check if the internet station is running and if not then select another ( I use the some code to detect the chime when a station isn’t available) 
    3. Raise the volume slowly in each room to a set level (the code is rough and I’ll be rewriting it to be more efficient and only a couple of lines of code)
    4. And then as part of my ‘all off upstairs’ scene I stop all zones upstairs and ungroup everything. 
    5. Downstairs the kitchen, hall and playroom players when we come down in the morning (motion sensor in kitchen) 

     

    The sonos parts of some other scenes I have …
    • “Leaving Home” executes a pause/all and ungroups upstairs zones if not already done
    • “Coming home” executes a /resume/all
    • “PlayroomTV On” scene turns on the TV in the playroom for the kids, pauses their music zone then ungroups it from the kitchen /hall and adjusts the volumes accordingly

     

    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

    I haven't gone near the mp3 clips, music services

     

    There are a few other examples of  where I use the api but I should give a flavor of what’s possible

    Sorry for the long post but hopefully it helps

     

    -f

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    Okay got your "node-sonos-api VD.vfib" working.

    I needed to "hard code" the zonename (ie: local zonename = "living%20room";) to make it work.

     

    Will now try some more of your examples.

     

    Thank you.

     

    Link to comment
    Share on other sites

    Testing some of your code, but having problems with the following.

     

    Please login or register to see this code.

     

    cStr code does get encoded nicely to "this+is+a+test", but the Google TTS does now say the + also.

    Tried not encoding the string, but this does not work :-/

     

    What TTS engine ware you using?

     

    Link to comment
    Share on other sites

    Never mind (again)

    Need to change the encodeTTS function :-)

    Edited by Guest
    Link to comment
    Share on other sites

  • Topic Author
  • 8 minutes ago, Guest said:

    Never mind (again)

    Need to change the encodeTTS function :-)

    No worries...

    Link to comment
    Share on other sites

    For some reason the "zonestate" does not work (it's blank)

     

    My sonos json reply looks like this:

    Please login or register to see this code.

    I see in the main loop code

    Please login or register to see this code.

    As you can see in my Sonos json reply it does not have "zoneState", so I was hoping to be able to use "playbackState"

    Please login or register to see this code.

    Unfortunately this does not work :-(

     

    Any way to fix this?

    Link to comment
    Share on other sites

  • Topic Author
  • 8 minutes ago, Guest said:

    For some reason the "zonestate" does not work (it's blank)

     

    My sonos json reply looks like this:

     

    Please login or register to see this code.

     

    I see in the main loop code

    Please login or register to see this code.

     

    As you can see in my Sonos json reply it does not have "zoneState", so I was hoping to be able to use "playbackState"

     

    Please login or register to see this code.

     

    Unfortunately this does not work :-(

     

    Any way to fix this?

     

    That's an odd one

    playbackState should work

     

    Could you share all the code on the main loop ?

    Are the labels case sensitive to your variable zonestate'

     

    other option would be to reboot the pi

     

     

     

     

    Link to comment
    Share on other sites

    Sorry for the late reply. I needed to go to the airport and pick some one up.

     

    The code is the same as the node-sonos-api VD.vfib code (posted in your 1st post).

     

    Please login or register to see this code.

     

    Anyway. I've just had a look again and looks like it's working now :-)

     

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

  • Topic Author
  • 4 minutes ago, Guest said:

    Sorry for the late reply. I needed to go to the airport and pick some one up.

     

    The code is the same as the node-sonos-api VD.vfib code (posted in your 1st post).

     

     

    Please login or register to see this code.

     

     

    Anyway. I've just had a look again and looks like it's working now :-)

     

    Please login or register to see this attachment.

     

    That's looking great - glad it worked out ( i like the vol 10%)

    depending on how many players you might want to look at presets as well as group/leave/pause all/resume all commands and a few others

     

    I also use the following code to see if a chime com es on instead of a radio station and if not switch to a different station if thats helpful

    Please login or register to see this code.

    This is very helpful for me since I use my HC2 instead of my sonos alarms

     

    I'll need to add another one  

    If somebody/some automation selects a radio station or playlist and it doesn't start playing (wait 5 seconds and check that the transition playState transition to PLAYING is complete), then select another favourite/playlist

     

    should be easy enough to put together

     

    For me one I latched on to the basics I was able to leverage it more and more with greater easy

     

    good luck, post more if you get stuck ..

     

    -f 

     

     

    Link to comment
    Share on other sites

  • Topic Author
  • I updated the first post to turn it into more like a tutorial

    I also included some of my use cases and some code snippets to enable them

     

     

    Link to comment
    Share on other sites

    @AutoFrank Since i have sonos too, i wanted to use a docker image (hence the other topic) but now i see that i don't have a /stop in the api. This could mean that your version is older and if people install it newer it will miss that functionality. Instead i have 1 button that is Play/Pause only. (which is  playpause  ) And if you have too long names, if you click on the expand on the home screen things like this happen:

     

    Please login or register to see this image.

    /monthly_2017_01/Capture.PNG.a5f9f219ecb31b2a7d0eabd21ab507d8.PNG" alt="Capture.PNG" />

    Something to take into account, if i get it all working, i'll paste my work here too (docker wise) but it is so far a breeze.

    Link to comment
    Share on other sites

  • Topic Author
  • 5 minutes ago, riemers said:

    @AutoFrank Since i have sonos too, i wanted to use a docker image (hence the other topic) but now i see that i don't have a /stop in the api. This could mean that your version is older and if people install it newer it will miss that functionality. Instead i have 1 button that is Play/Pause only. (which is  playpause  ) And if you have too long names, if you click on the expand on the home screen things like this happen:

     

    Please login or register to see this link.

    Something to take into account, if i get it all working, i'll paste my work here too (docker wise) but it is so far a breeze.

     

    Hi @riemers

     

    I agree that the VD buttons seem to behave a but erratically in general and depending on the device they can get worse. I use a few ipads and a few android tablets and the buttons wrap when you least expect them.

    As I said above the api is constantly evolving and even since I installed my version they have added a few more like clip (mp3 for TTS) and togglemute which will be very useful. I keep an eye on git and if there is something new  I can use I'll upgrade my api version. Regarding the stop,I haven't seen it in the api the Jishi provides. Play, Pause and Playpause are what's provided and for me this is more than enough options - The full list of current api functions are at 

    Please login or register to see this link.

     

    The main reason I published the the Tutorial was to just let other users know that its available and how one might go about putting the pieces of the puzzle together. I'm not providing a full solution like Krikroff. Users need to decide on how they want to deploy the api and the VD above was meant to be just a sample to get people up and running and how the api can be leveraged. I think that once a user gets used to the process and format of the api calls they can put together a vd  (or modify mine) in a matter of minutes and I would encourage this. Everybody's setup is different and some people like to have a 'vd remote' that they can bring up on their phone to control they sonos. My aim in my house is 'light-touch automation' and as such I have little use for a "vd remote" but I leverage the individual api calls or multiple calls to perform a given task as part of an overall orchestrated sequence or in response to a trigger. I also have a combined VD for the kltchen that allow us to control come sonos functions (like vol and rad favourites) for that room along with sone TV on/off, Sat channels and some house modes. For us the choice is dependent on what we want to manually control at that location in the house. I have a similar vd's on the tablet on the landing, in the master bedroom and the garage. 

     

    It's probably a long winded way of saying that I was hoping the post would show people where the resources are to set it up, coupled with how it operates with a view that they could use it anyway they want. I think it's also great when people share how they are using it with sample code. I think the work you're doing on docker is great and will provide more alternatives for people to leverage what I think is a great api.

     

    Thanks 

    -f

     

     

    Link to comment
    Share on other sites

    @AutoFrank thanks for the great tutorial. I have installed the api on my rpi. Do you have a start script for /etc/init.d to allow autostart on the pi?

    Link to comment
    Share on other sites

    For docker, as stated in the readme: 

    Please login or register to see this link.

     

    First make the config and local dirs up

    Please login or register to see this code.

    Then run the docker image

    Please login or register to see this code.

     

    In the first post you can see the setup of the presets. Besides that everything else behaves the same. So if you install docker on your rpi, it is just this above. Due to the nature of docker, you have to make the files locally somewhere and mount them in the image. That is why the first few lines are used for the mkdirs. Where you place those files are up to you. Just make sure your in that directory when you run the docker run command.

     

    If you change any files, i don't think it will pick that up automaticly, but a "docker restart sonos" will be sufficient then. The --restart=always makes sure that if your rpi or whatever machines dies/reboots, it will auto start this container again.

     

    p.s. if you go to your sonos ip, and use port 1400 and go to /status, you can see the web interface of your sonos. The sonos-http-api just makes it json friendly.

    Edited by riemers
    Link to comment
    Share on other sites

  • Topic Author
  • Code for Single Favorite button

    Added some information to the first post to allow the user 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/playlist. 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

     

     

    Edited by AutoFrank
    Link to comment
    Share on other sites

    Is it possible to change the name of the button trough lua? You could show the station when you click on it as you rotate.

    Link to comment
    Share on other sites

  • Topic Author
  • 10 hours ago, riemers said:

    Is it possible to change the name of the button trough lua? You could show the station when you click on it as you rotate.

     

    Thats an interesting one...

     

    I'm not sure you can dynamically change the label... I'll have a try..

    -f

    Link to comment
    Share on other sites

    @AutoFrank do you have a example for a TTS Button? I get it to work over a browser http request like "http://192.168.3.22:5005/Hobbyraum/say/Test/de-DE/50" but not in a button :(

    Edited by gobbli
    Link to comment
    Share on other sites

  • Topic Author
  • 57 minutes ago, gobbli said:

    @AutoFrank do you have a example for a TTS Button? I get it to work over a browser http request like "http://192.168.3.22:5005/Hobbyraum/say/Test/de-DE/50" but not in a button :(

     

    Hi @gobbli

     

    I group all my TTS into a single VD and just call them by a button press

    I set port and IP address at the VD level and then use them in each button but you could easily set them in the button

     

    Please login or register to see this attachment.

    x

    replace the IP address above with yours -  192.168.3.22

     

    then the button code is 

    Please login or register to see this code.

    You can't have spaces when calling from a scene or a VD button

    A browser command is more forgiving as it will encode it as it sends it so you can have spaces and other characters

     

    I think the reason it may not work in a button is because the string you are trying to say has a / in the middle of it and is probably confusing the http request.

    "http://192.168.3.22:5005/Hobbyraum/say/Test/de-DE/50"

     

    I included some code above to replace any spaces with a + so you can send a normal sentence and encode and send within the button or scene

     

    hope this helps...

    -frank

     

     

     

     

     

     

     

     

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