Saving Hue color lights to variable [solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Saving Hue color lights to variable [solved]

Post by niwreg »

Hi,

I'm trying to write a script that when an kodi start playing a video two hue lights wil switch to an specific scene. This isn't the tricky part.
However i would like that when kodi is paused or stopped the hue lights switch back to the previous color and brightnes (1 light only does brightnes so that simple)

Is there a trick to save the last color before switching the scene?
Last edited by niwreg on Sunday 14 October 2018 20:16, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Saving Hue color lights to variable

Post by waaren »

niwreg wrote: Thursday 30 August 2018 21:18 Hi,

I'm trying to write a script that when an kodi start playing a video two hue lights wil switch to an specific scene. This isn't the tricky part.
However i would like that when kodi is paused or stopped the hue lights switch back to the previous color and brightnes (1 light only does brightnes so that simple)

Is there a trick to save the last color before switching the scene?
check this post.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Saving Hue color lights to variable

Post by niwreg »

Thanks!

I'll have a look into it!
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Saving Hue color lights to variable

Post by niwreg »

Ok that took longer than expected to have a go with it.

Anyway i figured that the Hue go i use doesn't really like to respond to it,

Tried some basics with setting to Go to a color read the RGB values an then do a color(rgb) but i end up with completle different colors and values.

So my gues i have to do something with other value's but now shure how to do that?

I can get some Json strings what i get from the GO when i'm back home.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Saving Hue color lights to variable

Post by waaren »

niwreg wrote: Saturday 22 September 2018 22:29 Ok that took longer than expected to have a go with it.

Anyway i figured that the Hue go i use doesn't really like to respond to it,

Tried some basics with setting to Go to a color read the RGB values an then do a color(rgb) but i end up with completle different colors and values.

So my gues i have to do something with other value's but now shure how to do that?

I can get some Json strings what i get from the GO when i'm back home.
Can you try this approach ?

Code: Select all

-- KeepRestoreRGB

return {
    on =        {   devices                 = { "safeColor","getColor"}}  ,

    data    =   {   previousColor           = { initial = {} },
                    previousBrightness      = { initial = -1  }},
    
    logging =   {   level                   = domoticz.LOG_DEBUG,
                    marker                  = "KeepRestoreRGB" },
                        
    execute = function(dz, item)
        local rgbLight = dz.devices(156)
        
        local function internalURL(url)
            dz.openURL({
                            url = dz.settings['Domoticz url'] .. url,
                            method = "GET",
                        })
        end
        
        local function setColor(idx,color,brightness) 
            myUrl =  "/json.htm?type=command&param=setcolbrightnessvalue&" .. 
                    "idx=" .. idx .. 
                    "&color=" .. color ..
                    "&brightness=" .. brightness
            internalURL(myUrl)
        end

        if item.name == 'safeColor' or dz.data.previousBrightness == -1 then
            dz.data.previousColor       = dz.utils.fromJSON(rgbLight.color) -- Safe current color in dzVents persistent data
            dz.data.previousBrightness  = rgbLight.level                    -- Safe current brightness in dzVents persistent data
        elseif item.name == "getColor" then
            setColor(rgbLight.idx, dz.utils.toJSON(dz.data.previousColor), dz.data.previousBrightness)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Saving Hue color lights to variable

Post by niwreg »

I think i'll allmost get the jist of that. I'll try to implement this tomorow and let you know if it works. :)
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Saving Hue color lights to variable

Post by niwreg »

Ok I tested this and i'm working on some logging but what i can see:

Setting the light by hand works

Code: Select all

setColor(light.idx,dz.utils.toJSON({["g"] = 198;["t"] = 0;["cw"] = 0;["b"] = 254;["r"] = 221;["m"] = 3;["ww"] = 0;}),57)
Setting the DATA to storage works too, however changing the value back doesn't work

Code: Select all

dz.data.previousColor       = dz.utils.fromJSON(light.color) -- Safe current color in dzVents persistent data

Code: Select all

setColor(light.idx, dz.utils.toJSON(dz.data.previousColor), dz.data.previousBrightness)
Looking at the logs it looks like to work:

Code: Select all

2018-09-27 11:52:50.635 Status: dzVents: Info: KODI CONTROL 0.0.1...........: ------ Start internal script: KodiLightControl: Device: "KodiMediaCenter (Kodi)", Index: 17
2018-09-27 11:52:50.651 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:52:50.651 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: --------------------==<[ KODI SPEELT AF ]>==--------------------
2018-09-27 11:52:50.651 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:52:50.651 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:52:50.652 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: Prev
2018-09-27 11:52:50.653 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: {["r"]=221, ["cw"]=0, ["ww"]=0, ["g"]=197, ["b"]=255, ["m"]=3, ["t"]=0}
2018-09-27 11:52:50.653 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: Current
2018-09-27 11:52:50.683 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: {["b"]=255, ["t"]=0, ["ww"]=0, ["g"]=196, ["r"]=220, ["m"]=3, ["cw"]=0}
2018-09-27 11:52:50.684 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:52:50.688 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: --------------------MyUrl----------------------------------
2018-09-27 11:52:50.688 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: /json.htm?type=command&param=setcolbrightnessvalue&idx=163&color={"b":254,"cw":0,"g":198,"m":3,"r":221,"t":0,"ww":0}&brightness=57
2018-09-27 11:52:50.689 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ------------------------------------------------------------
2018-09-27 11:52:50.694 Status: dzVents: Info: KODI CONTROL 0.0.1...........: ------ Finished KodiLightControl
2018-09-27 11:54:00.523 Status: dzVents: Info: KODI CONTROL 0.0.1...........: ------ Start internal script: KodiLightControl: Device: "KodiMediaCenter (Kodi)", Index: 17
2018-09-27 11:54:00.538 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ---------------------------------------------------------------
2018-09-27 11:54:00.539 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: --------------------==<[ KODI OP PAUZE ]>==--------------------
2018-09-27 11:54:00.539 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ---------------------------------------------------------------
2018-09-27 11:54:00.539 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:54:00.540 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: Prev
2018-09-27 11:54:00.541 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: {["t"]=0, ["b"]=255, ["cw"]=0, ["ww"]=0, ["g"]=196, ["r"]=220, ["m"]=3}
2018-09-27 11:54:00.541 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: Current
2018-09-27 11:54:00.574 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: {["cw"]=0, ["b"]=119, ["t"]=0, ["ww"]=0, ["g"]=206, ["r"]=254, ["m"]=3}
2018-09-27 11:54:00.575 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ----------------------------------------------------------------
2018-09-27 11:54:00.577 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: --------------------MyUrl----------------------------------
2018-09-27 11:54:00.578 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: /json.htm?type=command&param=setcolbrightnessvalue&idx=163&color={"b":255,"cw":0,"g":196,"m":3,"r":220,"t":0,"ww":0}&brightness=100
2018-09-27 11:54:00.578 Status: dzVents: !Info: KODI CONTROL 0.0.1...........: ------------------------------------------------------------
2018-09-27 11:54:00.590 Status: dzVents: Info: KODI CONTROL 0.0.1...........: ------ Finished KodiLightControl
(prev is DZ Data before saving to the new value, Current is the actual light.color)

The problem i run into is that when setting the color directly it goes ok. It gets the color i expect. But when switch back it only reacts to the brightness it looks like.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Saving Hue color lights to variable

Post by waaren »

niwreg wrote: Thursday 27 September 2018 11:58 Ok I tested this and i'm working on some logging but what i can see:
The problem i run into is that when setting the color directly it goes ok. It gets the color i expect. But when switch back it only reacts to the brightness it looks like.
Sorry too hear that but I am kind of out of options with this now. Maybe @narel or @dannybloe can help you further. @narel created a setColBrightness method according to a post in this topic (I have not seen that one yet)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Saving Hue color lights to variable

Post by waaren »

niwreg wrote: Thursday 27 September 2018 11:58 Ok I tested this and i'm working on some logging but what i can see:
The problem i run into is that when setting the color directly it goes ok. It gets the color i expect. But when switch back it only reacts to the brightness it looks like.
Looked at it again and added a function colorTable2String. This seems to make a difference compared to the previous toJSON function.
also included the optional iswhite parm for the json call (defaults to false).
Tested the modified script on my Hue bulbs and they return to the safed state as designed. As before; very much interested in your findings.

Code: Select all

-- KeepRestoreRGB

return {
    on =        {   devices                 = { "safeColor","getColor"}}  ,          -- dummy triggerDevices to test the script 

    data    =   {   previousColor           = { initial = {} },
                    previousBrightness      = { initial = -1  }},
    
    logging =   {   level                   = domoticz.LOG_DEBUG,
                    marker                  = "KeepRestoreRGB" },
                        
    execute = function(dz, item)
        local rgbLight = dz.devices(156)           -- IDX of your colour bulb
        
        local function internalURL(url)
            dz.openURL({
                            url = dz.settings['Domoticz url'] .. url,
                            method = "GET",
                        })
        end
       
        local function colorTable2String(colorTable)
            local colorString    = "{"              -- transform table contents into formatted string 
            for k,v in pairs(colorTable) do 
                colorString = colorString .. '"' .. k .. '":' .. v .. "," 
            end
            return colorString:sub(1, -2) .. "}"
        end
       
        local function setColor(idx,color,brightness,iswhite) 
            myUrl =  "/json.htm?type=command&param=setcolbrightnessvalue&" .. 
                    "idx=" .. idx .. 
                    "&color=" .. color ..
                    "&brightness=" .. (brightness or 100) ..
                    "&iswhite=" .. (iswhite or "false")
            internalURL(myUrl)
        end

        if item.name == 'safeColor' or dz.data.previousBrightness == -1 then
            dz.data.previousColor       = dz.utils.fromJSON(rgbLight.color) -- Safe current color in dzVents persistent data
            dz.data.previousBrightness  = rgbLight.level                    -- Safe current brightness in dzVents persistent data
        elseif item.name == "getColor" then
            setColor(rgbLight.idx, colorTable2String(dz.data.previousColor), dz.data.previousBrightness)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
niwreg
Posts: 25
Joined: Thursday 19 February 2015 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Saving Hue color lights to variable [Solved]

Post by niwreg »

Hm this seems to work better. Still not completly satisified with the colors but working on it.

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest