Putting a Map in to a State? Is ST just broken?


I’m having a very strange issue with states and Maps?

I want to add a load of commands with descriptions in a smart app, they are populated using a string text field and need to be transported to a map


Now when adding these in to a map stored in a state, if I add the same thing twice it should just retain 1 entry, and this is what I see in the stored state in IDE, however returning the same state in debug logging shows two entries… If I add the same entry a third time then it’s not added, amendments are then not recorded in IDE, but adding a new one is?! Something is clearly wrong between IDE and the actual app…

Any ideas or is ST just broken??

Add once:
IDE: {Test=Hello}
Logging: [Test:Hello]

Add twice: - Creates a duplicate entry in the map, in logging only - this should not be possible in a map?!
IDE: {Test=Hello}
Logging: [Test:Hello, Test:Hello]

Add three times: - Doesn’t add a third - which is correct
IDE: {Test=Hello}
Logging: [Test:Hello, Test:Hello]

Update value to Goodbye: - Replaces the duplicate entry?! and IDE doesn’t update?!
IDE: {Test=Hello}
Logging: [Test:Goodbye, Test:Hello]

Create a Key as Test 2
IDE: {Test=Hello, Test2=Goodbye}
Logging: [Test:Hello, Test2:Goodbye]

Add Test 2 a second time - Duplicate entry in map in logging!!
IDE: {Test=Hello, Test2=Goodbye}
Logging: [Test:Hello, Test2:Goodbye, Test2:Goodbye]

Here is the code, it’s not the most elegant as I have been trying many permutations of code to make it work, I’ve used much simpler versions in the past, without saving to a variable first to do the same thing saving direct in to a state… so really no idea what the issue is!.. to add using atomicState results in the state not getting any update!

def addIRCommand(name, command){
    //state.rawCommands = [:]
    log.warn "Command Recieved $name, $command"
    def isMap = state.rawCommands instanceof Map
    if(!isMap){state.rawCommands = [:]}
    def tempMap = state.rawCommands
    tempMap << ["${name}": "${command}"]
    state.rawCommands = tempMap

    log.warn "state is ${state.rawCommands}"
    log.warn "tempMap is ${tempMap}"

P.S. Excuse the use of exclamation marks, but getting quite annoyed with this :slight_smile:

Edit: No still not fixed! I can put in a remove before the add… but a fairly expensive way when a map should already do this?

Can be closed now, putting the key in brackets solved the issue, but no idea why?!!

tempMap << [("${name}"): "${command}"]