0

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!

vlauciani
  • 1,010
  • 2
  • 13
  • 27
  • how do you "do something for a few minutes" in the first place? – Piglet Aug 22 '19 at 11:18
  • Update code: for example with `device.switchOff().afterSec(60).forMin(1)` – vlauciani Aug 22 '19 at 11:21
  • and you cannot resolve the previous state because you cannot set the color? because it doesn't work or because you don't know how? or is it because you loose the previous state? – Piglet Aug 22 '19 at 11:22
  • 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. – vlauciani Aug 22 '19 at 11:24
  • and you can't get those parameters from the color object? – Piglet Aug 22 '19 at 12:35
  • Yes I can! But Why each single params If I want to use the `color` object as is. The idea could be to have a method like: `device.setColor(domoticz.data.savedColor)` – vlauciani Aug 22 '19 at 12:36

1 Answers1

1

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))

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Ok, thank you! It is a solution... not the best, but works ;-) – vlauciani Aug 22 '19 at 13:29
  • the only alternative you have is to overwrite setColor but then you have to make sure this works everywhere it is used... or write a second function that takes color and does what my function does internally. – Piglet Aug 22 '19 at 14:05