Changes to atomicState?

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
	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=$    value=$evt.value"
    	if (atomicState.currentCount==atomicState.arrayCount) {
    		log.debug "Getting Current State"

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…

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.

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=$    value=$evt.value" 

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

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