# Changes to atomicState?

**URL:** <https://community.smartthings.com/t/changes-to-atomicstate/26936>\
**Category:** SmartApps & Automations\
**Created:** [October 23, 2015, 1:00pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936 "2015-10-23T13:00:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdikland](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/cdikland/32/105520_2.png) [@cdikland](https://community.smartthings.com/u/cdikland)\
**Post date:** [October 23, 2015, 1:00pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/1 "2015-10-23T13:00:15Z")

</div>

A SmartApp that had been running for a couple of months now stopped working after last weekends maintenance. While debugging there were no errors reported but I noticed that an _atomicState_ attribute was not holding the correct value. In the _initialize_() function I set _atomicState.count =0_ . When a switch is turned a subscribed function is called where the attribute is incremented. So far so good.

When I reinitialize the app, the log shows this attribute has been reset to zero in the _initialize_() function but the old/previous value appears in the subscribed function when a switch is turned on.

I replaced all _atomicState_ with _state_ and the app preformed as expected. I reverted back to _atomicState_ but now I get an error when I try to increment the attribute value. (code in question see below)

Anyone else having issues with atomicState or does anyone know what changes have been made?

```
 def initialize() {
    	atomicState.currentCount=0 as Integer
    	atomicState.arrayCount=prefpresence.size()
	subscribe(prefpresence, "presence", presenceHandler)
}

def presenceHandler(evt)
{
    	atomicState.currentCount=atomicState.currentCount+1 // No longer works. Error can't add null+null
	log.debug "Event presenceHandler. Total Presence: $atomicState.arrayCount Current Presence Reporting: $atomicState.currentCount Name=$evt.name value=$evt.value"
    	if (atomicState.currentCount==atomicState.arrayCount) {
    		log.debug "Getting Current State"
    		getCurrentState()
        	atomicState.currentCount=0
      	}
}
```

---

<div class="post-metadata">

**Author:** ![Mike\_Maxwell](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/mike_maxwell/32/18673_2.png) [@Mike\_Maxwell](https://community.smartthings.com/u/Mike_Maxwell)\
**Post date:** [October 23, 2015, 6:58pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/2 "2015-10-23T18:58:08Z")

</div>

given your use case, are you sure you need to use atomicState?

I have apps that query state variables several times within a minute, and I’ve not had to use atomicState…

---

<div class="post-metadata">

**Author:** ![cdikland](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/cdikland/32/105520_2.png) [@cdikland](https://community.smartthings.com/u/cdikland)\
**Post date:** [October 23, 2015, 7:32pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/3 "2015-10-23T19:32:30Z")

</div>

If I only use 1 Presence Sensor atomicState is not required. The problem is when I use multiple sensors.

- First sensor leaves and triggers _presenceHandler_ which increments state.currentCount.
- Next sensor leaves (could be immediate or a minute later depends on when ST triggers the event) and triggers _presenceHandler_ which increments state.currentCount.

You might think state.currentcount would now equal 2 and sometimes it does but sometimes it does not. Totally unreliable. Often state.currentcount equals 1 when the second Sensor leaves. Why? I assume because a new instance of the app was started hence resetting the state.currentcount to zero.

---

<div class="post-metadata">

**Author:** ![Mike\_Maxwell](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/mike_maxwell/32/18673_2.png) [@Mike\_Maxwell](https://community.smartthings.com/u/Mike_Maxwell)\
**Post date:** [October 23, 2015, 8:28pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/4 "2015-10-23T20:28:27Z")

</div>

Ah I see, how about this?

```
def presenceHandler(evt)
{
	def total = prefpresence.size()
	def current = prefpresence.currentState("presence").count{ s -> s.value == "present"}
	log.debug "Event presenceHandler. Total Presence: ${total} Current Presence Reporting: ${current} Name=$evt.name value=$evt.value" 
}
```

---

<div class="post-metadata">

**Author:** ![cdikland](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/cdikland/32/105520_2.png) [@cdikland](https://community.smartthings.com/u/cdikland)\
**Post date:** [October 24, 2015, 7:38pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/5 "2015-10-24T19:38:39Z")

</div>

Brilliant. Works like a charm. Can you explain this line

> prefpresence.currentState(“presence”).count{ s → s.value == “present”

I not quite sure where the _s -\>s.value_ comes from

---

<div class="post-metadata">

**Author:** ![Mike\_Maxwell](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/mike_maxwell/32/18673_2.png) [@Mike\_Maxwell](https://community.smartthings.com/u/Mike_Maxwell)\
**Post date:** [October 24, 2015, 8:16pm UTC](https://community.smartthings.com/t/changes-to-atomicstate/26936/6 "2015-10-24T20:16:13Z")

</div>

It’s an inline self defined groovy iterator variable.  
s can be any name you want, by default it is “it” IE:  
{ -\> it.foo}…, which I find a little less readable.
