Newbie and having a code issue with a temperature object

I have a smartthings app I am trying to write. It is using a Samsung motion sensor with temperature. The device is defined as a smartsense motion sensor. I will post the code. In my preferences section I set it as a “capability.temperatureMeasurement” type and I subscribe to it. The events are working. Any time I try to access mytemp object I get a null point of not found. I will post the log statements and I have tried various things but with no object they are not going to work. Now my dry contacts object is found and I have no issues with it. I know I am doing something very stupid but I just can’t seem to resolve this. I will include the logs and smartapp code. Any help would be appreciated.

Logs

91518d17-0198-4d3f-898d-3ca93d8e45aa 2:40:58 PM: error java.lang.NullPointerException: Cannot invoke method currentValue() on null object @line 63 (temperatureChangedHandler)
91518d17-0198-4d3f-898d-3ca93d8e45aa 2:40:58 PM: debug Temperature object null
91518d17-0198-4d3f-898d-3ca93d8e45aa 2:40:58 PM: debug Contacts Object FortrezZ MIMOlite
91518d17-0198-4d3f-898d-3ca93d8e45aa 2:40:58 PM: debug Temperature Changed Detected 71.0

Code

/*  Copyright 2019 Mike Calhoun
*  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: "Pellet Stove Wireless Heat Thermostat",
    namespace: "calhounm",
    author: "Mike Calhoun",
    description: "Control Pellet stove with temperature sensor and dry contacts",
    category: "My Apps",



preferences {
    section("When this temperature sensor:") {
            input "mytemp", "capability.temperatureMeasurement", required: true, title: "Sensor?"
      }
    section("Low Temperature Set Point") {
      input "lowsetpoint", "number", required:true, title:"Low Temperature SetPoint"
    }
    section("High Temperature Set Point") {
      input "highsetpoint", "number", required:true, title:"High Temperature SetPoint"
    }
    section("Turn Up Heat Contacts") {
        input "thecontacts", "capability.switch", required: true
    }
    
}

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(mytemp, "temperature", temperatureChangedHandler)
}    

// TODO: implement event handlers
def temperatureChangedHandler(evt) {
      log.debug "Temperature Changed Detected ${evt.doubleValue}"
           /*log.debug "high level  temp ${mytemp.currentTemperature}"*/
      log.debug "Contacts Object ${thecontacts}"
      log.debug "Temperature object ${mytemp}"    
      def tempattr = mytemp.currentValue("temperatureMeasurement")
      /* def tempattr = mytemp.currentValue("temperature").doubleValue()*/
      log.debug "The current temperature is ${tempattr instanceof Number}"
      
      log.debug "The current temperature is ${tempattr}"
          def currentValue = thecontacts.currentValue("switch")
          log.debug "the current value of mycontacts is $currentValue"
          log.debug "the current value of mycontacts is ${currentValue instanceof String}"

       def latestValue = thecontacts.latestValue("switch")
       log.debug "the latest value of thecontacts is ${latestValue instanceof String}" 

            if (evt.doubleValue < lowsetpoint) {
                  updateCheckIfHeatIsOn()

        } else {
            if (evt.doubleValue > highsetpoint) {
                  updateCheckIfHeatIsOff()
            }
        }
    
}

def updateCheckIfHeatIsOn() {
      /*thermostat.setThermostatMode(desiredMode)*/
      
      log.debug "check if heat is on because below low set point"
      /*log.debug "high level  temp ${mytemp.currentTemperature}"*/
      def tempattr = mytemp.latestValue("temperature")
      log.debug "The current temperature is ${tempattr instanceof Number}"
      log.debug "The current temperature is ${tempattr}"
      thecontacts.on()
}

def updateCheckIfHeatIsOff() {
      log.debug "check if heat is off because above high set point"
     /* log.debug "high level  temp ${mytemp.currentTemperature}"*/
      def tempattr = mytemp.latestValue("temperature")
      log.debug "The current temperature is ${tempattr instanceof Number}"
      log.debug "The current temperature is ${tempattr}"
      thecontacts.off()
}

I am not sure why it jumbled up my code and there is a /* for comment that only shows / above. If someone can tell me how to post the code that shows up normal that would be great as well.

Surround the entire block of code with 3 back-single-quotes (like below, but without the space).

`` `
Code

`` `

Thanks I will do that in the future.

Also I restarted my hub and disconnected devices from app and reconnected and code started working fine.

Thanks

You can hit the pencil on your post and edit it to fix it. Much more likely to get help that way.

Or post on GitHub, gist, or Pastebin.

As Terry said, if you can edit to put your code in a code block it’ll be easier to read. But, I think this:

Should be:

mytemp.currentValue(“temperature”)
1 Like

Edited original post to fix the unformatted code. @calhounmike Take a look at your post’s source to see how it’s done for future reference. :+1:

1 Like