Atomic State not working as expected


(Mitali Palekar) #1

Hey guys!

I am writing a smartapp to perform certain operations when in different “modes”. As of right now, I am testing some really basic functionality which is not working as desired. The flow of control is as follows:

  1. I ping the HTTP endpoint changestate/trigger and so in that function I set the value of atomicState.currState to trigger. I have verified this to work because the JSON and sendPush return the correct data.
  2. I then invoke the contactHandler method by changing the state of the contact handler. We would expect that “Contact Sensor Handler entered in trigger state” be printed out since the atomicState.currState is currently trigger (as set above). However, when I log it the atomicState.currState is somehow still “dummy” and I cannot figure out why.

I have verified already that the initialization function is not called again as well as tried it with state instead of atomic state. I am not sure why this is happening and so I would really appreciate some input. The code that I am using has been shown below:

preferences {
	section("Turn on when motion detected:") {
        input "contactSensor", "capability.contactSensor", required: true, title: "Which in-motion object?"
    section("Turn on this light") {
        input "lightSwitch", "capability.switch", required: true, title: "Which switch object?"

mappings {
  path("/changestate/:state") {
    action: [
      GET: "changeState"

def installed() {

def updated() {

def initialize() {
    atomicState.currState = "dummy"
    subscribe(contactSensor, "contact", contactHandler)

def contactHandler(evt) {
	sendPush "Current State: ${atomicState.currState}"
	if (atomicState.currState == "trigger") {
        sendPush "Contact Sensor Handler entered in trigger state"
    } else {
    	sendPush "Contact Sensor can only be accessed in the trigger state"

def changeState() {
    atomicState.currState = params.state
    sendPush "Current State: ${atomicState.currState}"
	return [message: "Current State: ${atomicState.currState}"]


(Steve White) #2

For one thing, you need to add params as a parameter to changeState(). I would start by doing that as that should fix it.

def changeState(params)

(Mitali Palekar) #3

Thanks for the suggestion! I tried fixing that, but it still didn’t work. Do you have any other suggestions?