Sonos Notify when door opens -- per person

Hi, I have 3 people at my home. I would like to have the Sonos play a specific welcome message per person when their presence sensor is detected as returning home AND the front door( multi sensor) opens.

I cannot seem to find a way to do this using the Android app, but I may just be missing something.

Any assistance would be appreciated.

@subnet88

I would be interested in the same thing. You could create a mode for each person. When presence is triggered change to their mode and then when the door is open trigger sonos message and change back to home mode. Seems messy though.

Anyone have a better idea?

I think that you would need to write a little app that fires off when the door is opened. Something like:

  1. When contact = open (subscribe to contact.open)
  2. See who’s presence is present
  3. Check to see who has newest presence
  4. play newest presence message

Make sense?
Twack

Thats what im trying now, but am getting stuck on Sonos Text to Speech. are there any documents or examples of how it is done in the Notify With Sound Smartapp?

@twack

How do you keep it from triggering when exiting?

@beckwith,

Good point. Check to see if they were present 5 minutes (or other value) before contact open.

Twack

It would probably work if we check to see if they were “not present” less than 5 minutes ago… I’m having difficulty with determining that code. So far I have whats below, but it doesnt seem to work.

def threshold = falseAlarmThreshold != null ? (60 * 60 * 1000) as Long : 10 * 60 * 1000L
def t0 = new Date(now() - threshold)
for (person in people) {
		if (person.latestValue == "present") 
        log.info("Looking for last status of ${person.displayName}")
        def states = person.statesSince("not present", t0)
		def recentNotPresentState = states.find{it.value == "present"}
        try{
        log.info("user last here at ${recentNotPresentState.date}")
        }
        catch(ex){
        }
        if (recentNotPresent) {
//Notify
}	

A second potential issue is if the door opens before the presence has been recognized. Any ideas how to get around that?

I got it working, Here is the tested code for my smartapp.

/**
 *  Welcome home notification
 *
 *  Copyright 2014 Garrette Grouwstra
 *
 *  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: "Welcome home notification",
    namespace: "",
    author: "Garrette Grouwstra",
    description: "Notify using Sonos when somebody comes home, and a door is opened",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
)

preferences {

section("When one of these people arrive at home") {
		input "people", "capability.presenceSensor", title: "Who?", multiple: true
        }
section("When the door opens...") {
		input "contact1", "capability.contactSensor", title: "Where?"
        }
		
section {
		input "sonos", "capability.musicPlayer", title: "Sonos Device", required: true
		}

	}

def installed() {
	subscribe(contact1, "contact.open", contactOpenHandler)

}

def updated() {

	unsubscribe()
	subscribe(contact1, "contact.open", contactOpenHandler)
}

def contactOpenHandler(evt) {
def t0 = new Date()
def t1 = new Date(t0.getTime() - (1000 * 60 * 2))
for (person in people) {
		if (person.latestValue == "present") 
        log.info("Looking for last status of ${person.displayName}")
        def states = person.statesSince("presence", t1)
	log.debug "${states?.size()} STATES FOUND, LAST AT ${states ? states[0].dateCreated : ''}"
        
		def recentNotPresentState = states.find{it.value == "present"}
        try{
        log.info("user last here at ${recentNotPresentState.date}")
        }
        catch(ex){
        }
        if (recentNotPresentState) {	
        def message = "Welcome home, ${person.displayName}"
				log.info(message)
				musicplayer(message)
                }
                else{
                log.debug "skipping notification of arrival of ${person.displayName} because they have been here for a while"
                }
			
		}
    
	log.trace "$evt.value: $evt, $settings"
	log.debug "$contact1 was opened, Setting "
	
}

private musicplayer(msg){
		
		log.trace "Playing $msg"
        
       
state.sound =textToSpeech(msg instanceof List ? msg : msg) 
log.info(state.sound.uri)
       sonos.playTrackAndResume(state.sound.uri, state.sound.duration, 100)	

		

    
}

2 Likes

Is this working for you still? Does music resume after the alert plays? I just got my sonos and just setup one to welcome us to the house, but couldn’t find a way to do it based on presence and closed door.

Any idea why this won’t work for me? Just plain won’t fire off