tl;dr: How do I pull saved color data from a device and maintain it as a map and not a big string?
Long version: From what I can tell, based on testing here, when you tell Alexa to set something to a color it is passed to a device handler through the “setColor” routine in the map but only Hue and Saturation are passed with no other values. So based on that I want to recreate all the color data to properly store it in my device. I do it through a bunch of pretty standard functions and assign it like this:
// came from the ST cloud which only contains hue and saturation. So build our color data and pass it.
def myRGB = hslToRGB(color.hue, color.saturation, 0.5)
def myHEX = rgbToHex(myRGB)
def colorData = [:]
colorData = [red: myRGB.red,
hex: myHEX,
saturation: color.saturation,
blue: myRGB.blue,
hue: color.hue,
green: myRGB.green,
alpha: 1]
sendEvent(name: "color", value: colorData)
adjustColor(colorData)
So with some debug lines added in I can see the progression when I say “Alexa set the cabinet lights to red”
raw color map passed in: [hue:0, saturation:100]
myRGB: [red:255, green:0, blue:0]
myHEX: #ff0000
LightLevel: 86
FullColorMap: [red:255, hex:#ff0000, saturation:100, blue:0, hue:0, green:0, alpha:1]
AdjustedColorSentToLight: #db0000
Everything works exactly as it should, I get the hue and saturation values for red, convert those to red, green, blue, and hex, build my map, save it to the device, then pass it to my adjustedColor routine which modifies the HEX based on dimmer setting and sends it to the physical light.
The problem is when I try to pull the color map back out. If I adjust the dimmer I use the last known color by pulling it with this:
def lastColor = device.latestValue(“color”)
adjustColor(lastColor)
Then in the adjustColor routine it takes the color map and does a color.red to pull red etc. Problem is when I pull the data using latestValue or currentValue it errors out with a “groovy.lang.MissingPropertyException: No such property: red for class: java.lang.String”.
For example clicking the color chooser sends the color map like in the debug output above and color.red works perfectly fine. lastestValue gives this:
{red=255, hex=#ff0000, saturation=100, blue=0, hue=0, green=0, alpha=1}
So as you can see passed directly from the color picker or if I build the color map using data from Hue/Sat then it works great and each item in the map has a colon after it then the value. If I pull from lastValue it seems to bring it in as a big string with quotes and equal signs so the map is broken.
How do I pull the saved color data in a device back out and maintain it being a map so I can do things like lastColorValue.red and get the red value?