Subscribe() not working

I'm porting over some simple code from SmartThings into a custom app, and I'm having trouble getting any subscriptions to work (similar to the problem encountered here).

My code:

/**
 *  Cherry Point
 *
 *  Copyright 2019 M. Adams
 *
 *  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:           "Cherry Point",
  namespace:      "adams",
  author:         "M. Adams",
  description:    "Handles select smarthome activity at Cherry Point Drive.",
  category:       "",
  iconUrl:        "",
  iconX2Url:      "",
  iconX3Url:      ""
)


preferences {
   
  //-------------------------------------------------------------------------
  // Lights
  //-------------------------------------------------------------------------
  page( 
      name:       "pageOne",
      title:      "Lights",
      nextPage:   "pageTwo",
      uninstall:  true) {
      
  section { 
      paragraph   "Front porch lights"
      input       "LTS_FF_FRONT_PORCH", "capability.switch", 
      required:   true, 
      multiple:   true,
      title:      "Which lights?"

      paragraph   "Back porch lights"
      input       "LTS_FF_BACK_PORCH", "capability.switch",  
      required:   true, 
      title:      "Which lights?" 
    } 
  }

  //-------------------------------------------------------------------------
  // Open/close sensors
  //-------------------------------------------------------------------------
  page(
    name:       "pageTwo",
    title:      "Open/close sensors",
    install:    true,
    uninstall:  true) {
      
  section { 
    paragraph   "Front door open/close sensor"
    input       "OCS_FF_FRONT_DOOR", "capability.contactSensor", 
    required:   true, 
    title:      "Which open/close door sensor?"
    } 
  }
}
    
    

//--------------------------------------------------------------------------------- INSTALLED
def installed() {
  initialize() 
}


//----------------------------------------------------------------------------------- UPDATED
def updated() {
  log.info "updated(): unsubsribing, then initializing"
  unsubscribe()
  initialize() 
}

//--------------------------------------------------------------------------------- INITIALIZE
def initialize() { 
  log.info "initalize(): subscribing to events, setting state.previousMode"
  state.previousMode = location.mode 
  subscribe(LTS_FF_FRONT_PORCH, "sunset", handleSunset) 
  subscribe(LTS_FF_BACK_PORCH, "sunset", handleSunset) 
  subscribe(LTS_FF_FRONT_PORCH, "sunrise", handleSunrise) 
  subscribe(LTS_FF_BACK_PORCH, "sunrise", handleSunrise)
  subscribe(OCS_FF_FRONT_DOOR, "switch", handleFrontDoor)
}


//------------------------------------------------------------------------------ HANDLE SUNRISE
def handleSunrise() {
  def SUNRISE_TIME = location.sunrise
  log.info "Executing sunrise handler at ${SUNRISE_TIME}"

  LTS_FF_FRONT_PORCH.off()
  LTS_FF_BACK_PORCH.off()
  location.setMode("MODE_DAY") 
}

//------------------------------------------------------------------------------- HANDLE SUNSET
def handleSunset(evt) {
  def SUNSET_TIME = location.sunset
  log.info "Executing sunrise handler at ${SUNSET_TIME}"

  LTS_FF_FRONT_PORCH.on()
  LTS_FF_BACK_PORCH.on()

  // Only overwrite the current mode if it's MODE_DAY; otherwise, store MODE_NIGHT in state,
  // where it'll be fetched whenever the other mode reverts.
  if (location.mode == "MODE_DAY") {
    location.setMode("MODE_NIGHT")
  } else {
    state.previousMode = "MODE_NIGHT"
  }
}

//---------------------------------------------------------------------------- HANDLE FRONT DOOR
def handleFrontDoor(evt) {
  log.info "The front door opened."
}

A screenshot including event subscriptions for the app:

Logs:

And 0 events are shown for the app. The sunset/sunrise handlers didn't fire last night or this morning, and the handleFrontDoor handler is never fired when the door is opened.

Is something obvious sticking out to anyone? I've tried rebooting the hub, deleting/reinstalling the app, etc., all to no avail.

The first thing I notice...

If the front door is a contact then the subscription is wrong.

You should subscribe to ‘contact’ not ‘switch’

Next..
You are subscribing to sunset & sunrise for you porch switches.
These should be a ‘switch’ attribute.

2 Likes

What @cobra said.

Also if you are trying to subscribe to the sunrise/sunset event use

subscribe(location, "sunset", handleSunset)
subscribe(location, "sunrise", handleSunrise)

And put in your handleSunset/handleSunrise methods the commands to turn on/off devices.

2 Likes

Thank you both! Subscribing to a contact instead of a switch worked immediately, and subscribing to location instead of the lights makes total sense. I was misinterpreting a previous post.