dzVents - Save 'color' object and reuse

1k Views Asked by At

I'd like to get the HUE color state, save it, change the color for few minutes and then restore the saved color.

The problem is that I can get the color state object but I can't restore the previous object as is:

return {
    on = {
        devices = {
            'My Light'
        }
    },
    data = {
        savedColor = { initial = {} }
        newColor = { initial = {} }
    },
    execute = function(domoticz, device)
        if (device.active) then
            domoticz.data.savedColor = device.getColor()
            device.setRGB(50,50,50)
            device.switchOff().afterSec(60).forMin(1)

            -- here I'd like to restore the previous state; something like:
            -- device.color = domoticz.data.savedColor
        end
    end
}

I do not know how to restore the previous state. I've saved the color object but the option to set color is the method setColor(r, g, b, br, cw, ww, m, t) that get the single values not the entire object saved!

1

There are 1 best solutions below

2
On BEST ANSWER

According to https://github.com/domoticz/domoticz/blob/6edc7436b9e23ff81adaeaf402e2228fe99b1ca9/dzVents/runtime/device-adapters/rgbw_device.lua

device.getColor returns a table

local ct = domoticz.utils.fromJSON(device.color, {})
            ct.hue, ct.saturation, ct.value, ct.isWhite = domoticz.utils.rgbToHSB(ct.r, ct.g, ct.b)
            ct.red  = ct.r
            ct.blue = ct.b
            ct.green = ct.g
            ct["warm white"] = ct.ww
            ct["cold white"] = ct.cw
            ct.temperature = ct.t
            ct.mode = ct.m
            ct.brightness = ct.value
            return (ct)

And function device.setColor(r, g, b, br, cw, ww, m, t) does not accept a table as input.

So the only way is to use the single params. Of course you may write some convenience function that lists those params from a color object, in case you need that more often.

Something like

local function color2Params(color)
  return color.r, color.g, color.b,
    color.brightness, color.cw, color.ww, color.m, color.t
end

Then you can simply call device.setColor(color2Params(domoticz.data.savedColor))