This app is great! Thanks so much. Iâm trying to add a couple functionalities to this app but it seems my programming skills are a bit rusty.
In short, my concept is if I unlock with a certain code, it will update the mode and turn on a particular song on sonos.
Iâve added components from the Sonos Mood Music template to attempt to achieve this.
Hereâs the Sonos Mood Music code:
https://searchcode.com/codesearch/view/89517660/
Iâve been receiving this error:
java.lang.NullPointerException: Cannot get property âselectedSongâ on null object @ line 69
Highlighted below
Any help of course would be greatly appreciated.
/**
*/
definition(
name: âDoor Unlock Triggers with music selectionâ,
namespace: âgaryd9â,
author: âGary Dâ,
description: âTriggers switches, door controls, etc based on lock eventsâ,
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(âWhen a door unlocksâŠâ) {
input âlock1â, âcapability.lockâ, title: âDoor Lock?â, required: true
input âmySwitchâ, âcapability.switchâ, title: âSwitch? (for debugging)â, required: false
}
section(âAnd this user code opened the door:â) {
input âUser1â, âenumâ, title: âUser Codeâ, metadata:[values:[â1â,â2â,â3â,â4â,â5â]]
}
section(âThenâŠâ){
input âdoorsâ, âcapability.doorControlâ, title: âClose these doorsâ, multiple: true, required: false
input âswitchesâ, âcapability.switchâ, title: âTurn on these switchesâ, multiple: true, required: false
input ânewModeâ, âmodeâ, title: âChange to this modeâ, multiple: false, required: false
input name: âsendPushâ, type: âboolâ, title: âPush notification to mobile devices?â, defaultValue: false
input âsonosâ, âcapability.musicPlayerâ, title: âOn this Sonos playerâ, required: false
input âsongâ, âenumâ, title:âPlay this trackâ, required:false, multiple: false, options: songOptions()
label title: âAssign a nameâ, required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(lock1, âlock.unlockedâ, lockHandler)
subscribe(mySwitch, âswitchâ, switchHandler, [filterEvents: false])
log.debug "about to subscribe to commandâŠ"
subscribeToCommand(mySwitch, âonâ, switchOnHandler)
saveSelectedSong()
}
private songOptions() {
// Make sure current selection is in the set
def options = new LinkedHashSet()
if (state.selectedSong?.station) { // THIS IS WHERE THE ERROR OCCCURS
options << state.selectedSong.station
}
else if (state.selectedSong?.description) {
// TODO - Remove eventually? âdescriptionâ for backward compatibility
options << state.selectedSong.description
}
// Query for recent tracks
def states = sonos.statesSince("trackData", new Date(0), [max:30])
def dataMaps = states.collect{it.jsonValue}
options.addAll(dataMaps.collect{it.station})
log.trace "${options.size()} songs in list"
options.take(20) as List
}
private saveSelectedSong() {
try {
def thisSong = song
log.info "Looking for $thisSong"
def songs = sonos.statesSince(âtrackDataâ, new Date(0), [max:30]).collect{it.jsonValue}
log.info âSearching ${songs.size()} recordsâ
def data = songs.find {s -> s.station == thisSong}
log.info "Found ${data?.station}"
if (data) {
state.selectedSong = data
log.debug "Selected song = $state.selectedSong"
}
else if (song == state.selectedSong?.station) {
log.debug "Selected existing entry '$song', which is no longer in the last 20 list"
}
else {
log.warn "Selected song '$song' not found"
}
}
catch (Throwable t) {
log.error t
}
}
def performActions(evt)
{
doors.eachWithIndex {s, i ->
def state = s.latestValue(âdoorâ)
if (state == âopenâ)
{
log.debug "$s is currently $state. Closing it."
s.close();
}
else
{
log.debug â$s is currently $state. (canât close it)â
}
}
switches.eachWithIndex {s, i ->
def state = s.latestValue(âswitchâ)
if (state == âoffâ)
{
log.debug "$s is currently $state. Turning it on."
s.on()
}
else
{
log.debug â$s is already $state.â
}
}
log.debug "We made it this far!!!â
setLocationMode(newMode)
sonos.playTrack(state.selectedSong)
if (sendPush)
{
sendPushMessage(evt.descriptionText)
}
}
def lockHandler(evt)
{
if (evt.data == null)
{
log.debug âevData is nullâ
}
else
{
def evData = parseJson(evt.data)
log.debug âevData.usedCode: ${evData.usedCode}. Attempting to find user $User1"
if (â${evData.usedCode}â == â$User1â)
{
log.debug User1 + " unlocked the door!"
performActions(evt)
}
}
}
def switchHandler(evt)
{
// performActions(evt)
log.debug âswitchHandler ${evt.value}â
}
def switchOnHandler(evt)
{
log.debug âswitchOnHandler ${evt.inspect()}â
}