Groovy Map - Find Key Using Value

Does anyone have an example of looking up a Groovy Map object to find a key by passing in a value stored in a variable?

E.g.

def setThermostatFanMode(fanmode) {

parent.debugLog("setThermostatFanMode: ${fanmode}")
//def myFanMode = "low-medium"
def vfanModeValue = fanModeMap.find {it.value == fanMode}?.key
parent.debugLog("setThermostatFanMode: ${fanmode}, ${vfanModeValue}")

}

If I pass in the fanmode parameter from the thermostat tile on a dashboard it correctly outputs the selection I made in the first debug log entry, but the second debug log outputs a null for the result of the key lookup, i.e. the vFanModeValue variable.

dev:11892020-09-08 15:15:21.461 debugsetThermostatFanMode: low-medium, null

dev:11892020-09-08 15:15:21.439 debugsetThermostatFanMode: low-medium

If I change the code to uncomment the myFanMode variable and pass that into the find statement, it works, outputting the key for the fan mode.

def setThermostatFanMode(fanmode) {

parent.debugLog("setThermostatFanMode: ${fanmode}")
def myFanMode = "low-medium"
def vfanModeValue = fanModeMap.find {it.value == myFanMode}?.key
parent.debugLog("setThermostatFanMode: ${fanmode}, ${vfanModeValue}")

}

dev:11892020-09-08 15:19:27.549 debugsetThermostatFanMode: low-medium, 2

dev:11892020-09-08 15:19:27.536 debugsetThermostatFanMode: low-medium

I'm assuming it is something to do with strings or the lookup attemptin to compare object references rather than contents. I have tried numerous combinations of toString(), etc. Just can't figure it out...

Any thoughts?

Simon

Looks like this may have been to do with the fan mode text sent through from the dashboard including a space at the front of it. I had already started to change the code when i realised this, so will need to test the map lookup when i change it back to see how it behaves. I have overwridden the default fan modes, so not sure if that has played a part....