Mixing atomicState and state

@chuck.schwer - sorry I didn't tag you re my reply (above) to your post asking for code

Also - if I put a very small delay of 50ms in the device driver before the return of each event so they are paced better it works with 26 counted and everything is obviously in A-Z order .. but I suspect state would then work too. Less than 50ms and it starts to introduce more and more problems. This is probably dependent on how long I take in my methods to handle the event , avoiding concurreny of execution

I believe atomicState is still not totally robust to concurrency... with the italics being important in the ST documentation

Modifications (additional, removal, updating) to Atomic State within an execution are persisted to external storage more or less immediately .

and this is a bit misleading..

If using atomicState , which reads and writes to the external data store when the object is updated or accessed,

as it falls foul of the same issue state does but at a higher threhold

Yes I was going to reply with the same thing. atomicState reduces the possibility of a race condition but does not eliminate it. Unfortunately things are happening so fast with this that you are seeing this problem. I will give some thought about a possible solution to this problem, it might require a synchronization option for methods, or maybe for atomicState.

Ironic, It’s surfaced in HE because your Hub is local for network devices and so damn fast at it !

I have my bigger app working OK now and populating complete lists for input selections... as I had originally intended.

One last Q.. can I create but hide and/or disable devices programmatically ?

My device driver delay routine / loop is horrible too, can’t we have a better option like sleep()


Nothing to contribute here. I've just been following this thread so I could learn more about atomicState vs state variables and since I can't give you a solution I figured I'd give you what asked for :stuck_out_tongue_winking_eye:.



Maybe it's not such an atomic era

1 Like

what are you using? I guess not pauseExecution ?

I'm using this from an old ST post by Terry (ActionTiles)

  • should I be using pauseExecution ?

    def pause(millis) {
    def passed = 0
    def now = new Date().time
    /* This loop is an impolite busywait. We need to be given a true sleep() method, please. */
    while ( passed < millis ) {
    passed = new Date().time - now

Yes you should be :slight_smile:
The method you posted puts unnecessary load on the hub. I'm wondering how many people are using something like that?

sorry I missed it .. where is it documented ?
Google was not my friend
I assume it takes a single parameter in mS

The post above has a link to it (click on pauseExecution):

Thanks - and no possibility to disable or hide devices ?

I'm not sure what you mean.. you want to hide devices from the user? in the main devices list? If so, then no.

I wanted to hide them from the user or disable them like the 'disable' checkbox does. One way they would be totally hidden except for getChildDevice() etc and the other they just don't function. Not a problem but that would have been an easy solution as there are 150 devices involved (for me) and they clutter the interface currently.

Why don’t you just filter the devices at creation time and only create the ones you need?

I for one think it would be a very bad idea for apps and devices to be able to hide devices. That concept has the potential for serious abuse.


Initially it was because I couldn’t create a valid input drop down to select from, now using a delay in my device driver I can.

Also still as an obstacle is the deviceID, label and capabilities all arrive separately, and I need to match them up before creating the device. I’m on the way to being able to implement this though now.

Having disable as is already implemented in the UI would be useful but yes, I can also see issues with hidden.


It's very difficult to provide feedback without seeing more of your code and you probably don't want or need any....you are obviously a more proficient programmer than I am. But based on the pieces I'm putting together from this thread, it sounds like you are trying to do things in the driver that might be better accomplished in the app portion of the code. For instance, your device list could be stored in a map or list and presented as an enum input where the user could select the drivers they want created.

I realize I am making a lot of assumptions above but it just seems like it should be possible and would make timing the device creation easier and avoid needless cycles creating unneeded devices. Just my 2c.

Thats where I would focus my energy if I were in your position. If you can't change the serial nature of the data coming in, you can at least cache it in state and then create your devices once you have all of the needed components.

Indeed I can now do that using the small inserted delay that has got the list 'stable' - before it was being mangled even using atomicState

I couldn't actually as it arrived asynchronously and too fast so state was inconsistent. Even too fast for atomicState , which is not bullet proof. By using a tiny delay of 50mS I now have it robust though so, once more I'm all set to proceed.

This is the nature of MQTT - if you subscribe to a wildcarded topic to retrieve say a list of devices you will get a load of payloads returned instantaneously in a burst of individual messages. In my case over 150. Payloads e.g. labels' arrive together but the devices they apply to are all interleaved.

I'm on the right track now though.

I've also been considering whether it would be useful to present MQTT devices to other Hub Connect clients directly for selection. Of course creating a device on HE facilitates this anyway.

A word of caution... It you are using atomicState to store data with continuous high-frequency access (reads and writes to the database) you will inevitably create a “hub killer” app. Hopefully you’re able to avoid that.

As I visualize this I still think you can use state for this with a periodic runIn job to marry up all of the arriving data parts leading to device creation. It’s just a thought based on my very limited of what you’re trying to do.

I gave some thought to MQTTA integration with HubConnect but inevitably ran into the same challenges you are facing. I just couldn’t visualize how I could emulate the same or at least similar user experience.

I’m open to suggestions if you ever want to take a crack at it.

Fortunately It’s just the startup of the app that needs to model the incoming messages. Maybe the first 30 seconds, and then only if you are using MQTT discovery. Once that is complete I don’t think I ongoingly use atomicState at all so this isn’t an issue. State would indirectly create much the same effect.

I’ll keep pondering about MQTT and HubConnect...

1 Like