Page 1 of 1

Not able to set Kelvin on a WW lamp

Posted: Wednesday 30 October 2019 22:51
by jaaap
Hi all,

I have a small script running that should set the color temperature of a lamp, based on the daytime. In the log of domoticz it states:

Code: Select all

2019-10-30 22:47:36.342 Status: dzVents: Error (2.4.19): Method setKelvin is not available for device "Lamp gang beneden" (deviceType=Color Switch, deviceSubType=WW). If you believe this is not correct, please report.
In my view, a WW should be the type of lamp that should be able to be set on a specific color temperature. Isn't it? Or is this only available for RGBWW lamps? If so, is there anyone willing to extent the functionality so WW lamps can also be set in dzVents scripts?

Please help

Re: Not able to set Kelvin on a WW lamp

Posted: Wednesday 30 October 2019 23:00
by waaren
jaaap wrote: Wednesday 30 October 2019 22:51 I have a small script running that should set the color temperature of a lamp, based on the daytime. In the log of domoticz it states:
In my view, a WW should be the type of lamp that should be able to be set on a specific color temperature. Isn't it? Or is this only available for RGBWW lamps? If so, is there anyone willing to extent the functionality so WW lamps can also be set in dzVents scripts?
It is already implemented for WW type of lamps but only in recent Beta's and not in version 2.4.19.
Can you upgrade ?

If not you could try to replace the RGBWW device-adapter with the latest version but that one is only tested on recent Beta's

Re: Not able to set Kelvin on a WW lamp

Posted: Wednesday 30 October 2019 23:35
by jaaap
Can you upgrade ?
Just by overwriting the dzvents folder in the domoticz installation?

Re: Not able to set Kelvin on a WW lamp

Posted: Thursday 31 October 2019 0:14
by waaren
jaaap wrote: Wednesday 30 October 2019 23:35 Just by overwriting the dzvents folder in the domoticz installation?
No I meant upgrading domoticz. dzVents is completely integrated so if you upgrade domoticz, dzVents will be upgraded with it.

If you don't want that you could try to replace your rgbw device-adapter with the one below. But before you do that please make sure you made a copy from the current one

first

Code: Select all

sudo cp <domoticz dir>/dzVents/device-adapters\rgbw_device.lua <domoticz dir>/dzVents/device-adapters\rgbw_device.lua_keep
then replace the current rgbw_device.lua with the one below. No need to restart domoticz / dzVents. It will work directly (or not and then you will have to restore the kept one)

Code: Select all

local TimedCommand = require('TimedCommand')

return {

	baseType = 'device',

	name = 'RGB(W) device adapter',

	matches = function (device, adapterManager)
		local res = device.deviceType == 'Color Switch'
		adapterManager.addDummyMethod(device, 'setKelvin')
		adapterManager.addDummyMethod(device, 'setWhiteMode')
		adapterManager.addDummyMethod(device, 'increaseBrightness')
		adapterManager.addDummyMethod(device, 'decreaseBrightness')
		adapterManager.addDummyMethod(device, 'setNightMode')
		adapterManager.addDummyMethod(device, 'setRGB')
		adapterManager.addDummyMethod(device, 'setHex')
		adapterManager.addDummyMethod(device, 'setHue')
		adapterManager.addDummyMethod(device, 'setColor')
		adapterManager.addDummyMethod(device, 'setColorBrightness')
		adapterManager.addDummyMethod(device, 'setHue')
		adapterManager.addDummyMethod(device, 'getColor')
		adapterManager.addDummyMethod(device, 'setDiscoMode')

		return res
	end,

	process = function (device, data, domoticz, utils, adapterManager)

		if (device.deviceSubType == 'RGBWW') or (device.deviceSubType == 'WW')  then
			function device.setKelvin(kelvin)
				local url
				url = domoticz.settings['Domoticz url'] ..
						'/json.htm?param=setkelvinlevel&type=command&idx=' .. device.id .. '&kelvin=' .. tonumber(kelvin)
				return domoticz.openURL(url)
			end
		end

		function device.setWhiteMode()
			local url
			url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=whitelight&type=command&idx=' .. device.id
			return domoticz.openURL(url)
		end

		function device.increaseBrightness()
			local url
			url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=brightnessup&type=command&idx=' .. device.id
			return domoticz.openURL(url)
		end

		function device.decreaseBrightness()
			local url
			url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=brightnessdown&type=command&idx=' .. device.id
			return domoticz.openURL(url)
		end

		function device.setNightMode()
			local url
			url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=nightlight&type=command&idx=' .. device.id
			return domoticz.openURL(url)
		end

		function device.setDiscoMode(modeNum)
			if (type(modeNum) ~= 'number' or modeNum < 1 or modeNum > 9) then
				domoticz.log('Mode number needs to be a number from 1-9', utils.LOG_ERROR)
			end
			local url
			url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=discomodenum' .. tonumber(modeNum) .. '&type=command&idx=' .. device.id
			return domoticz.openURL(url)
		end

		local function inRange(value, low, high)
			return (value >= low and value <= high )
		end

		local function validRGB(r, g, b)
			return  (
				type(r) == 'number' and
				type(b) == 'number' and
				type(g) == 'number' and
				inRange(r, 0, 255) and
				inRange(b, 0, 255) and
				inRange(g, 0, 255)
			)
		end

		local function RGBError()
			 domoticz.log('RGB values need to be numbers from 0-255', utils.LOG_ERROR)
		end

		function device.setRGB(r, g, b)
			if not(validRGB(r, g, b)) then RGBError() return false end
			local h, s, b, isWhite = domoticz.utils.rgbToHSB(r, g, b)
			local url = domoticz.settings['Domoticz url'] ..
						'/json.htm?param=setcolbrightnessvalue&type=command'  ..
						'&idx=' .. device.id ..
						'&hue=' .. math.floor(tonumber(h) + 0.5) .. 
						'&brightness=' .. math.floor(tonumber(b) + 0.5) ..
						'&iswhite=' .. tostring(isWhite)
			return domoticz.openURL(url)
		end

		function device.setColor(r, g, b, br, cw, ww, m, t)
			if not(validRGB(r, g, b)) then RGBError() return false end
			if br and not(inRange(br, 0, 100)) then
				domoticz.log('Brightness value need to be number from 0-100', utils.LOG_ERROR)
				return false
			end
			if m == nil then m = 3 end
			local colors = domoticz.utils.fromJSON(device.color,{ t=0, cw=0, ww=0})
			local brightness = br or 100
			if br == nil then
				local _ , _ , brightness = domoticz.utils.rgbToHSB(r, g, b)
			end
			if cw == nil then cw = colors.cw end
			if ww == nil then ww = colors.ww end
			if t  == nil then t = colors.t end
			local url = domoticz.settings['Domoticz url'] ..
						'/json.htm?type=command&param=setcolbrightnessvalue' ..
						'&idx=' .. device.id   ..
						'&brightness=' .. brightness ..
						'&color={"m":' .. m ..
						',"t":' .. t ..
						',"cw":' .. cw ..
						',"ww":' .. ww ..
						',"r":' .. r ..
						',"g":' .. g ..
						',"b":' .. b .. '}'
			return domoticz.openURL(url)
		end

	   function device.setColorBrightness(r, g, b, br, cw, ww, m, t)
			return device.setColor(r, g, b, br, cw, ww, m, t)
		end

		function device.setHex(r,g,b)
			local _ , _ , brightness = domoticz.utils.rgbToHSB(r, g, b)
			local hex = string.format("%02x",r) .. string.format("%02x", g) .. string.format("%02x", b)
			local url = domoticz.settings['Domoticz url'] .. '/json.htm?type=command&param=setcolbrightnessvalue' ..
				'&idx=' .. device.id ..
				'&brightness=' .. brightness ..
				'&hex=' .. hex ..
				'&iswhite=false'
			return domoticz.openURL(url)
		end

		function device.setHue(hue,brightness,isWhite)
			if not(brightness) or not(inRange(brightness, 0, 100)) then
				domoticz.log('Brightness value need to be number from 0-100', utils.LOG_ERROR)
				return false
			end
			if not(hue) or not(inRange(hue, 0, 360)) then
				domoticz.log('hue value need to be number from 0-360', utils.LOG_ERROR)
				return false
			end
			if isWhite and type(isWhite) ~= "boolean" then
				domoticz.log('isWhite need to be of boolean type', utils.LOG_ERROR)
				return false
			end
			if not(isWhite) then isWhite = false end
				local url = domoticz.settings['Domoticz url'] .. '/json.htm?type=command&param=setcolbrightnessvalue' ..
					'&idx=' .. device.id ..
					'&brightness=' .. brightness ..
					'&hue=' .. hue ..
					'&iswhite=' .. tostring(isWhite)
				return domoticz.openURL(url)
		end

		function device.getColor()
			if not(device.color) or device.color == "" then
				domoticz.log('Color field not set for this device', utils.LOG_ERROR)
				return nil
			end
			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)
		end
	end
}

Re: Not able to set Kelvin on a WW lamp

Posted: Thursday 31 October 2019 10:38
by jaaap
I updated, it now gives me:

Code: Select all

2019-10-31 10:32:26.973 Error: Error opening url: http://127.0.0.1:8084/json.htm?param=setkelvinlevel&type=command&idx=53&kelvin=0
Maybe it is an error within the script? Here's the script I use:

Code: Select all

return {
	on = {
		devices = {'PIR gang beneden'}, -- Change out PIR
	},
   
   execute = function(dz, PIR)
        local Lux           = dz.devices('LUX woonkamer') 
        local partymode     = dz.devices('Party Mode')
--        local dimmerGroup   = dz.groups("dimGroup")
        local Light = dz.devices('Lamp gang beneden') -- Change out light
        
        local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                    ['at 00:01-06:00']  = { 
                                                                [100]       = 10,
                                                                [200]       = 50,
                                                           },
                                    ['at 06:01-09:00']  = {     
                                                                [80]       = 50,
                                                                [110]      = 100,
                                                                [130]      = 0,
                                                           },
                                    ['at 09:01-21:00']  = { 
                                                                [80]       = 100,
                                                                [110]      = 100,
                                                                [130]      = 100, -- deze weer op 0 zetten
                                                           },
                                    ['at 21:01-24:00']  = {
                                                                [0]         = 75    
                                                          },
                              }
                              
        local KelvinTimeTable = { --  [   'timeSlot'   ]  = Kelvin
                                     ['at 00:01-07:00']  = 80,
                                     ['at 07:01-16:00']  = 10,
                                     ['at 16:01-20:00']  = 80,
                                     ['at 20:01-24:00']  = 100,
                              }
                              
        function getDimlevelBasedOnLux(t, measuredLux)
            local delta = 999999
            local keyDimLevel = 1
            for key, dimLevel in pairs(t) do
                if math.abs(measuredLux - key) < delta then
                    delta = math.abs(measuredLux - key)
                    keyDimLevel = key
                
                end
            end
            return t[keyDimLevel]
        end                      
        
--        function dimDevicesInGroup(groupName,dimValue)
--            dz.groups(groupName).devices().forEach(function(device)
--                device.cancelQueuedCommands()
--                device.dimTo(dimValue)
--                dz.log(device.name .. ' switched On (dimlevel: ' .. dimValue .. ')') 
--            end)    
--        end
--

--        function switchOffDevicesInGroup(groupName,delay)
--            dz.groups(groupName).devices().forEach(function(device)
--                 device.switchOff().afterSec(delay)
--                 dz.log(device.name .. ' will be switched Off after ' .. delay .. ' seconds.') 
--            end)    
--        end

        if (PIR.state ~= 'On' and partymode.state == 'Off') then 
            Light.switchOff().afterMin(1)
            dz.log(Light.name .. ' switched Off')
        else
            for timeSlot, luxLevels in pairs (dimTimeTable) do
               if dz.time.matchesRule(timeSlot) then 
                    local dimValue = getDimlevelBasedOnLux(luxLevels, Lux.lux )
                    
                  for timeSlot, KelvinTime in pairs (KelvinTimeTable) do
                        if dz.time.matchesRule(timeSlot) then 
                             local Kelvin = KelvinTime
                             dz.log(Kelvin)
                             dz.log(Light.name .. ' will be switched On (dimlevel: ' .. dimValue .. '); Based on: timeslot ('.. timeSlot .. '), measured Lux (' .. Lux.lux ..')') 
                            Light.setKelvin(Kelvin)
                            Light.dimTo(dimValue)
                            
                            return false
                        end
                    end
               end      
            end
        end     
    end
}

Re: Not able to set Kelvin on a WW lamp

Posted: Thursday 31 October 2019 10:54
by waaren
jaaap wrote: Thursday 31 October 2019 10:38 Maybe it is an error within the script?
I don't see an error in your script at first glance.
Did you open domoticz to be accessed without password from 127.0.0.1 ?

from the dzvents wiki
Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz.

Re: Not able to set Kelvin on a WW lamp  [Solved]

Posted: Thursday 31 October 2019 11:11
by jaaap
YES! Works! :-)

I misread and seperated the IP addresses with a komma, instead of puntkomma. Thank you @waaren once again for the support and time!