Dynamic variable error

What's wrong with this: ?

atomicState.device${deviceId} = [:]

Gives "atomicState.device$({ -> ... }) is a method call expression, but it should be a variable expression".

to play with Maps in atomicState, you should first copy them to local variable, do you changes, then re-assign back to atomicState.

In general you should avoid atomicState if you can...it is DB intensive and slows everything down.

try: atomicState."device${deviceId}"= [:]

atomicState.deviceState"${deviceId}" = [:]

gives ""$deviceId" is a GString expression, but it should be a variable expression"

atomicState.device${deviceId} = [:]

gives ""atomicState.device$({ -> ... })" is a method call expression, but it should be a variable expression"

Are GStrings not permitted with state variables??

This should work:

state."device${deviceId}" = [:]

EDIT: Welp, I'll leave this here for posterity to show that I didn't actually read @bcopeland's post. :wink: We basically said the same thing, but your example doesn't have quotes around the whole atomicState member name. Does it work if you fix that?


I read it once, but computer crapped out before saving. Second try, I didn't read.

1 Like

Next question.... Why does this not work:

atomicState."device${deviceId}" = ["state":"off"]
atomicState."device${deviceId}"."state" = "hello"
log.debug atomicState."device${deviceId}"

I expect it to be "[state:hello]", but I get "[state:off]".

You can't assign to individual elements of a map object in atomicState. Instead, you have to write the entire map object. So make a copy of the object in a local variable, change what you need to change, and then write the entire object.

I don't bother to use objects in atomicState anymore, because of this hassle. I just use naming to do it, and keep each element of what would be an object as separate atomicState variables. Like,

atomicState."device${deviceId}-state" = "hello"

Thanks. Don't think that'd be practical in my use case. I can reset the entire map as ["state":"hello"], but that can be rather unwieldy in some circumstances.

Is this an issue with Groovy or Hubitat's implementation? If the latter, any chance it could be resolved?

edit: Didn't see the your first response, but... yea, that's what I'm doing. To clarify, it would work if this were anything not atomicState/state? atomicMap.device.state = "hello" would work (as a regular map, not persistent)?

Don't know what you're doing, but I'm pretty sure you can get around having to use a map if you want. A deconstructed map is pretty straight forward to do. But, to each his own. Just sayin, I found it ultimately more efficient to just go with separate atomicState variables than using a map I had to copy out of atomicState and then back into it to change a single element of the map.

There is no plan to change how atomicState works.

While you cannot assign to a map element in atomicState, you can with state. state is read when an app instance runs, and written when it exits. So,

state."device${deviceId}".state = "hello"

works as expected. The issue is solely with atomicState.

Thanks again. Appreciate your Saturday time.

Download the Hubitat app