Page 1 of 1

RGB Color from Dimmer

Posted: Tuesday 25 August 2020 17:24
by gian72
Hi to all.
I'm trying to get the values of a virtual dimmer (RGB color and master slider), so I can push them via mqtt.
The question is: how can I read this values and put them in variables?
Thanks so much for your help.
Gianluca

Re: RGB Color from Dimmer

Posted: Tuesday 25 August 2020 17:45
by waaren
gian72 wrote: Tuesday 25 August 2020 17:24 Hi to all.
I'm trying to get the values of a virtual dimmer (RGB color and master slider), so I can push them via mqtt.
The question is: how can I read this values and put them in variables?
Thanks so much for your help.
Gianluca
Reading is the easy part.
Question is what needs to be done with the values ?

Code: Select all

return
{
    on =
    {
        devices =
        {
            3137, -- idx or 'name' of  your RGBW device
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'getColor',
    },

    execute = function(dz, item)
        dz.utils.dumpTable(item.getColor() )

        dz.log(item.name ..'; color = ' .. dz.utils.toJSON(item.getColor()),dz.LOG_DEBUG)
        dz.log(item.name ..'; dimLevel = ' .. item.level,dz.LOG_DEBUG)
    end
}
Spoiler: show

Code: Select all

Info: getColor: ------ Start internal script: 20200826 dz senColorToMQTT: Device: "Hue gevel zuid (Hue)", Index: 3137
> cw: 0
> isWhite: false
> hue: 45.0
> r: 255
> brightness: 100.0
> m: 3
> temperature: 0
> warm white: 0
> g: 242
> cold white: 0
> blue: 203
> ww: 0
> b: 203
> mode: 3
> green: 242
> value: 100.0
> saturation: 20.392156862745
> t: 0
> red: 255
Debug: getColor: Hue gevel zuid; color = {"b":203,"blue":203,"brightness":100.0,"cold white":0,"cw":0,"g":242,"green":242,"hue":45.0,"isWhite":false,"m":3,"mode":3,"r":255,"red":255,"saturation":20.392156862745,"t":0,"temperature":0,"value":100.0,"warm white":0,"ww":0}
Debug: getColor: Hue gevel zuid; dimLevel = 94
Info: getColor: ------ Finished 20200826 dz senColorToMQTT

Re: RGB Color from Dimmer

Posted: Tuesday 25 August 2020 19:19
by gian72
Thanks for the quick reply. I will try it asap.
I've a modified aqaura gateway (https://github.com/cadavre/miio_gateway) and I'm trying to send, via mqtt the commands to change the colors. I've already achieved the on off (via node red), now I'm trying to change the color.
Do you kn ow a simpler way :roll: ?
Thank you!

Re: RGB Color from Dimmer

Posted: Tuesday 25 August 2020 20:03
by waaren
gian72 wrote: I've a modified aqaura gateway (https://github.com/cadavre/miio_gateway) and I'm trying to send, via mqtt the commands to change the colors. I've already achieved the on off (via node red), now I'm trying to change the color.
Do you know a simpler way :roll: ?
Thank you!
What is the mqtt command you are looking for?



Re: RGB Color from Dimmer

Posted: Tuesday 25 August 2020 21:14
by gian72

Code: Select all

return
{
    on =
    {
        devices =
        {
            201, -- idx or 'name' of  your RGBW device
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'getColor',
    },

    execute = function(dz, item)
        dz.utils.dumpTable(item.getColor() )

        dz.log(item.name ..'; color = ' .. dz.utils.toJSON(item.getColor()),dz.LOG_DEBUG)
       
        dz.log(item.name ..'; dimLevel = ' .. item.level,dz.LOG_DEBUG)
        comando = 'mosquitto_pub -h mqttserver -t /mihome/brightness -m ' ..item.level
        os.execute(comando)
    end
}
The function above sets the brightness level correctly!

So what i'm trying right now is how to get the RGB value in a format like 'fffff' so I put it in a command like

Code: Select all

comando = 'mosquitto_pub -h mqttserver -t /mihome/rgb -m ' ..item.SOMETHING
and to have the item.SOMETHING containing the rgb value.

Gianluca

Re: RGB Color from Dimmer

Posted: Wednesday 26 August 2020 8:04
by waaren
gian72 wrote: Tuesday 25 August 2020 21:14 What i'm trying right now is how to get the RGB value in a format like 'fffff' so I put it in a command like

Code: Select all

comando = 'mosquitto_pub -h mqttserver -t /mihome/rgb -m ' ..item.SOMETHING
and to have the item.SOMETHING containing the rgb value.
Can you test this?

Code: Select all

return
{
    on =
    {
        devices =
        {
            201, -- idx or 'name' of  your RGBW device
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'sendColor',
    },

    execute = function(dz, item)
       
       local mqttServer = '192.168.192.116' -- Change to IP or name of your MQTT server
       
       local color = item.getColor()

       local function osCommand(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

        local function toHex(...)
            local hex = ''
            for _, rgb in ipairs({...}) do
                hex = hex .. string.format('%02X',rgb) -- use string.format('%02x',rgb) if you need lower case hex
            end
            return hex
        end

        local function createMQTT(server, topic, message)
            return 'mosquitto_pub -h ' .. server .. ' -t ' .. topic ..' -m ' .. message
        end

        osCommand( createMQTT( mqttServer, '/mihome/brighness', item.level ) )
        osCommand( createMQTT( mqttServer, '/mihome/rgb', toHex(color.r, color.g, color.b ) ) )

    end
}

Re: RGB Color from Dimmer  [Solved]

Posted: Wednesday 26 August 2020 10:20
by gian72
Works great!
The functions toHex and osCommand opened my mind on how dzVents is working :D .
Thanks so much for the solution!
Gianluca