[RELEASE] Simpe Volume with Voice

smartapp_av
smartapp_voice

(Jesse S) #1

For the past few months I’ve been thinking about how nice it would be to adjust the Sonos-TV volume with Alexa. The specific use case is actually when I’m in the kitchen cooking and want to turn the TV volume up/down/whatever without having to rinse my hands, dry them off, and reach for a remote. I searched in the forums a couple times and didn’t see any case-specific SmartApps, but happened to notice Ask Alexa has this capacity. If nothing else, at least it would be a fun exercise in AWS.

Anyway, I set it up and as-if-it-was-magic, now I could say “Alexa, ask SmartThings to set Living Room Sonos volume to 35” and it worked! After experimenting for a while, I found the utterance was just too danged long. So, I shortened “SmartThings” to a monosyllabic name and “Living Room Sonos” to “TV,” but still found “ask …to set … volume to XX” to be a bit clunky.

With a nod to the folks over in this thread:

I decided to write a purpose-built app to do the job more simply. The idea is to:

  1. Create a virtual dimmer in the IDE.
  2. Install the SmartApp and configure it to tie your speaker to the virtual dimmer
  3. Add the virtual dimmer in Alexa’s list of discovered devices and name it something simple.

Now, I’m controlling the TV volume with “Alexa, set TV to 35.” Yes, I managed to save myself a few syllables, but more importantly, the Alexa integration is more streamlined; the same voice commands control lighting levels and speaker volume.

If you don’t already have a virtual dimmer device type, I’ve been using Michael Struck’s version:

The code itself is ridiculously simple for a SmartApp, but given that I wasn’t able to easily find speaker volume control via Alexa, I thought my virtual dimmer approach might be worth sharing with the community. Hope this helps some folks out there!

/**
 *  Dimmer-Sonos Bridge
 *
 *  Copyright 2016 Jesse Silverberg
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Dimmer-Sonos Bridge",
    namespace: "JLS",
    author: "Jesse Silverberg",
    description: "Bridge a virtual dimmer to a Sonos for Alexa volume control.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("Bridge this dimmer...") {
		input "myDimmer", "capability.switchLevel", multiple: false, title: "Choose virtual dimmer", required: true
	}
	section("...to this speaker") {
        input "mySpeaker", "capability.musicPlayer", title: "Choose Speaker(s)", multiple: true, required: true 
    } 
}


def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(myDimmer, "level", setNewVolume)
}


def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribe(myDimmer, "level", setNewVolume)

}


def setNewVolume(evt) {

	log.debug "in handler with: ${evt.value}"
	def newVolume = myDimmer.currentState("level").integerValue
	mySpeaker.setLevel(newVolume)

}

[Solved] Am I the only one with a Sonos Playbar problem?
Sonos subpar integration
(itsamti) #2

Great work. Thank you!


#3

What is the best way to set this up? I added your app to IDE and added a virtual dimmer.


(Jesse S) #4

Hi @Fubie – Did you have any problems configuring the app to connect to your Sonos player and the virtual dimmer yet? Have you tested what happens when you have music playing and you adjust the virtual dimmer %-age value?


#5

If I adjust the dimmer on my phone it has no affect. I don’t think the dimmer is connected to my Sonos


#6

I get this error in my log in IDE.

error java.lang.IllegalArgumentException: Command ‘getDeviceDataByName’ is not supported by device 748b6501-cd83-4c1a-b940-b6583dd7da0e of type ‘Sonos Player’. Supported commands: [on, off, refresh, play, pause, stop, nextTrack, playTrack, setLevel, mute, previousTrack, unmute, setTrack, resumeTrack, restoreTrack, getVolume, subscribe, getCurrentMedia, getCurrentStatus, seek, unsubscribe, setLocalLevel, tileSetLevel, playTrackAndResume, playTextAndResume, playTrackAtVolume, playSoundAndTrack, playTrackAndRestore, playTextAndRestore] @ line 448


(Jesse S) #7

Hmm… How many Sonos speakers do you have selected?


#8

I have 4, but only 1 is selected.


(Jesse S) #9

By any chance, did the order of dimmer and Sonos get switched in the setup? That is, can you verify that the virtual switch is the first item selected and the speaker is the second? Can you also check to see if any other speakers are problematic (I’m wondering if maybe that particular speaker is not properly recognized by ST… Ghosts in the machine and all that jazz).


#10

Here is my setup, Jams is the name of the Virtual Dimmer.

I switched to another speaker & I have the same result.


(Jesse S) #11

Very interesting. Okay, here’s a few more questions:

What virtual dimmer switch are you using – is it the one I recommended above?

When you’re in the IDE, do you ever see the log.debug text “in handler with $blah blah blah” show up when you adjust the dimmer’s value?

And in my configuration, I leave the “assign a name” field blank (like the mode restriction, it’s a default field for naming the app). In terms of tracking down the issue, I highly doubt this will make a difference, but would you mind removing the name from the field and trying again?

See… What’s confusing me is that error in the IDE you found refers to a command “getDeviceDataByName” for the Sonos player… But my code doesn’t use this command. Instead, I use setLevel for the Sonos player, which is listed as an available command in the same error log(??!!).


#12

I’m using Michael Struck’s Virtual Dimmer.

I ran the live logging and then adjusted the volume, then did a search for log.debug but it showed no results.

I removed the app and re added from scratch without the “assign name” It made no difference.


(Tony - SmartThings Unpublished Contributor ) #13

For basic Sonos controls (play, pause, skip, last song, mute, volume, and playlists) via Alexa, I’ve found that the Yonomi skill works great.


(Michael) #14

Sonos is currently working on their own direct integration with Alexa/Echo
that should be out soon. Sonos just released a beta of their Spotify
integration that lets you control your Sonos system from within Spotify
without using the standard Sonos app. We just started testing the
integration a couple days ago and so far so good…


#15

Yes, but when is soon to Sonos?

I got my 4 Sonos speakers because they could play my Audible.com books natively.

They took that ability away saying they would put it back soon… That was a year ago.

Sorry if this sounded harsh, it is not pointed at you in any way.


(Michael) #16

I hear you! I am a Cubic.ai backer and would much rather be going down that
path then the Alexa/Amazon route for voice integration but I have had to
learn the waiting way that soon can unfortunately mean something between
someday and never…


#17

I’m trying the Cubic.ai right now to see how I like it.

Have you looked at Google Home at all?

I like it so far.


(Michael) #18

I have not looked into Google’s offering. I like the concept of Cubic and
the basic app has come along way but they got way off track with so much
effort into Hue and Nest. The slow to develop AI that originally perked my
interest along with the incomplete cube (the hardware) that has never been
completed along with their choices of HA devices they intend to support is
heading off in a different path then I am comfortable with. They seem to
have issues with ST in general but then most folks do. It probably isn’t
that important given the rate at which HA is growing… we will all be
using something completely different in a few years… hopefully everything
will become integrated into the everyday instead of this hobby approach
with little to no industry standards driving developers to a common goal…


(Jesse S) #19

@Fubie Well, you’ve got me stumped here. The combination of what your log is and isn’t showing suggests the smart app isn’t installing right, which … maybe … maybe it’s a copy-paste issue? Really, the biggest clue is in the IDE log you sent, which is referring to a function that isn’t even in the code. I wish I could sit down with you side-by-side to look at what was happening in real time so we could chase this down and get it working. Do you have any interest in doing something like a Skype chat with screen share so we can go through it? If so, PM me and we can set something up.

Otherwise, I suppose the other option is to wait for the direct Sonos-Alexa integration. Like others (@infofiend, @susanandmichaelw, and just about everyone else in this forum) I’m super excited for this to come out. It really is a must-have functionality, and will most likely make my little smart app obsolete. In my experience, Sonos really does have a good track-record with their products, even if they are very slow to update.

As for Google’s HA offering, I was generally neutral. Yes, it’ll be cool to connect things to Chromecast (which I find we use more and more for YouTube junk). And I strongly suspect Google’s ability to answer random questions will rapidly catch up with and surpass Alexa. However, the ability to do Amazon Prime ordering-by-voice has become a real treat and that’s enough to keep me on the Amazon platform… for now. But I think @susanandmichaelw is right – at the current rate, two years from now, who knows what we’ll be using.


(Jesse S) #20

Brief follow up on the conversation. @Fubie and I video chatted last night and looked into the problem. Turns out GROUPED Sonos speakers throw out errors in the capability.music player setlevel() command. By ungrouping the speakers, we found everything worked as intended.

The other thing we noticed while debugging was that Alexa’s reserved words, although necessary, can be annoying. :stuck_out_tongue: Does anyone know if there’s a published list of all the reserved words? If so, linking it here would be helpful.

Related reading: