How to set device RGBWW via Lua script?

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

How to set device RGBWW via Lua script?

Post by ben53252642 »

Hey folks,

Does anyone know how to set a RGBWW Domoticz virtual device via Lua script?

Eg: commandArray['LED'] = 'On' & Colortemp= 180 & Brightness = 80

or

Eg: commandArray['LED'] = 'On' & R=180 B=80 G=221 & Brightness = 80

Thanks
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: How to set device RGBWW via Lua script?

Post by Nautilus »

You can use these examples as a reference: https://www.domoticz.com/wiki/Domoticz_ ... emperature

Also, a nice way to see the exact API call is to open developer console in a browser (e.g. in Chrome you can do it by right-click -> inspect), select network tab and then perform the action you want to script in the GUI.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set device RGBWW via Lua script?

Post by waaren »

ben53252642 wrote: Thursday 20 September 2018 21:28 Does anyone know how to set a RGBWW Domoticz virtual device via Lua script?
I don't think it can be done with a "native" commandArray command yet.
In the domoticz database the field Color in table deviceStatus for RGBWW devices is like {"b":129,"cw":0,"g":211,"m":3,"r":254,"t":0,"ww":0}
If I peek in the dzVents device-adapter for RGBWW devices, the setRGB command is coded as a JSON call like

Code: Select all

url = domoticz.settings['Domoticz url'] ..
					'/json.htm?param=setcolbrightnessvalue&type=command&idx=' .. device.id ..
					'&hue=' .. tostring(h) ..
					'&brightness=' .. tostring(b) ..
					'&iswhite=' ..  tostring(isWhite)
where hue, brightness and isWhite are the result of a quite complicated conversion function from rgb
Spoiler: show

Code: Select all

function self.rgbToHSB(r, g, b)
	local hsb = {h = 0, s = 0, b = 0}

	local min = math.min(r, g, b)
	local max = math.max(r, g, b)

	local delta = max - min;

	hsb.b = max;
	hsb.s = max ~= 0 and (255 * delta / max) or 0;

	if (hsb.s ~= 0) then
		if (r == max) then
			hsb.h = (g - b) / delta
		elseif (g == max) then
			 hsb.h = 2 + (b - r) / delta
		else
			hsb.h = 4 + (r - g) / delta
		end
	else
		hsb.h = -1
	end

	hsb.h = hsb.h * 60
	if (hsb.h < 0) then
		hsb.h = hsb.h + 360
	end

	hsb.s = hsb.s * (100 / 255)
	hsb.b = hsb.b * (100 / 255)

	local isWhite = (hsb.s < 20)
	return hsb.h, hsb.s, hsb.b, isWhite
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest