1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| local afterSetColorCallback = 'afterSetColorCallback'
local Color_switch_name = "Color switch"
local rgbw_dummyDevice_name = 'rgbw_dummyDevice'
return {
active = true,
on = {
devices = {
rgbw_dummyDevice_name,
Color_switch_name
},
httpResponses = {
afterSetColorCallback
}
},
logging = {
level = domoticz.LOG_ERROR, --domoticz.LOG_INFO,
marker = "Set-rgbw_dummyDevice-rgbwwz_"
},
data = {
lastLevel = { initial = 1 }
},
execute = function(domoticz, itemChanged)
local rgbw_dummyDevice = domoticz.devices(rgbw_dummyDevice_name)
local rgbwDeviceNames = {
'RGBW - 1',
'RGBW - 2',
'RGBW - 3'
}
local rgbwDevices = domoticz.devices().filter(rgbwDeviceNames)
local domoticz_base_url_1 = 'http://127.0.0.1:8080'
local domoticz_base_url_2 = '/json.htm?'
function setLevel(levelToSet)
domoticz.log('Level to set (in setLevel()): ' .. levelToSet, domoticz.LOG_INFO)
rgbwDevices.forEach(function(rgbwDevice)
setLevelForDevice(rgbwDevice, levelToSet)
end)
end
function setLevelForDevice(device, levelToSet)
local device_url = 'type=command¶m=switchlight&idx=' .. device.idx .. '&switchcmd=Set%20Level&level=' .. levelToSet
domoticz.openURL({
url = domoticz_base_url_1 .. domoticz_base_url_2 .. device_url,
method = 'GET'
})
end
function setColor()
local levelToSet = domoticz.data.lastLevel
local colorToSet = rgbw_dummyDevice.color
domoticz.log('Color to set: ' .. colorToSet, domoticz.LOG_INFO)
domoticz.log('Level to set (via domoticz.data.lastLevel) (in setColor()): ' .. levelToSet, domoticz.LOG_INFO)
rgbwDevices.forEach(function(rgbwDevice)
setColorForDevice(rgbwDevice, colorToSet)
end)
end
function setColorForDevice(device, colorToSet)
local levelToSet = domoticz.data.lastLevel
local device_url = 'type=command¶m=setcolbrightnessvalue&idx=' .. device.idx .. '&brightness=' .. levelToSet .. '&color=' .. colorToSet
domoticz.openURL({
url = domoticz_base_url_1 .. domoticz_base_url_2 .. device_url,
method = 'GET'
})
end
if(itemChanged.isDevice) then
if(itemChanged.name == rgbw_dummyDevice_name) then
if(rgbw_dummyDevice.state == 'Set Color') then
-- A bit weird: I do not receive the new chosen color, but the previous color.
-- To be able to receive the new color I have to apply a state "On" change and then the new color is received.
-- After the state 'On' change I use a callback to handle the new color
domoticz.log('In statement for rgbw_dummyDevice.state == "Set Color": ', domoticz.LOG_DEBUG)
local levelToSet = domoticz.data.lastLevel
local dummyDevice_ON_url = 'type=command¶m=switchlight&idx=' .. rgbw_dummyDevice.idx .. '&switchcmd=On&level='.. levelToSet ..'&passcode='
domoticz.openURL({
url = domoticz_base_url_1 .. domoticz_base_url_2 .. dummyDevice_ON_url,
method = 'GET',
callback = afterSetColorCallback,
})
elseif (rgbw_dummyDevice.state == 'Set Level') then
-- rgbw_dummyDevice.state == 'Set Level' never seem to happen.. The change of level triggers a .state == 'On' This statement could therefore be removed
domoticz.log('In statement for rgbw_dummyDevice.state == "Set Level": ', domoticz.LOG_DEBUG)
domoticz.data.lastLevel = rgbw_dummyDevice.levelVal
setLevel(rgbw_dummyDevice.levelVal)
elseif (rgbw_dummyDevice.state == 'On') then
domoticz.log('In statement for rgbw_dummyDevice.state == "On": millisecondsAgo: ' .. tostring(rgbw_dummyDevice.lastUpdate.millisecondsAgo), domoticz.LOG_DEBUG)
-- The millisecondsAgo of 1800 is a bit of a gamble, I have seen updates of 1400 while the "On" statement just ran (And in that case we want to execute the setLevel), so I have to be at least above 1400.
-- Did not have any interaction problems till now with 1800, if you change fast it is still not a real problem
if(rgbw_dummyDevice.lastUpdate.millisecondsAgo > 1800 or rgbw_dummyDevice.lastUpdate.millisecondsAgo < 200) then
-- Ignore the on state change if the lastUpdate was a short while ago. This because the 'Set Color' change activates the state On to apply the new color.
domoticz.log('In statement for rgbw_dummyDevice.lastUpdate.millisecondsAgo > 1800:', domoticz.LOG_DEBUG)
domoticz.data.lastLevel = rgbw_dummyDevice.levelVal
setLevel(rgbw_dummyDevice.levelVal)
end
elseif (rgbw_dummyDevice.state == 'Off') then
domoticz.log('In statement for rgbw_dummyDevice.state == "Off":', domoticz.LOG_DEBUG)
rgbwDevices.forEach(function(rgbwDevice)
rgbwDevice.switchOff().checkFirst()
end)
end
elseif(itemChanged.name == Color_switch_name) then
domoticz.log('In statement for itemChanged.name == Color_switch_name": ', domoticz.LOG_DEBUG)
domoticz.data.lastLevel = 2 -- Always set level to 2 (in my case that is a nice value)
local color = itemChanged.state
local colorToSet = ''
if color == "Off" then
colorToSet = '{"m":4,"t":3,"r":255,"g":248,"b":245,"cw":252,"ww":3}'
elseif color == "Oranje" then
colorToSet = '{"m":4,"t":255,"r":255,"g":89,"b":0,"cw":0,"ww":0}'
elseif color == "Licht geel" then
colorToSet = '{"m":4,"t":255,"r":255,"g":157,"b":43,"cw":0,"ww":213}'
elseif color == "Groen" then
colorToSet = '{"m":4,"t":11,"r":6,"g":21,"b":6,"cw":108,"ww":5}'
elseif color == "Licht blauw" then
colorToSet = '{"m":4,"t":255,"r":32,"g":255,"b":248,"cw":0,"ww":0}'
elseif color == "Paars" then
colorToSet = '{"m":4,"t":255,"r":255,"g":68,"b":213,"cw":0,"ww":0}'
elseif color == "Licht Rood" then
colorToSet = '{"m":4,"t":0,"r":7,"g":1,"b":1,"cw":16,"ww":0}'
else
colorToSet = '{"m":4,"t":255,"r":255,"g":102,"b":204,"cw":0,"ww":0}'
end
setColorForDevice(rgbw_dummyDevice, colorToSet)
end
elseif (itemChanged.isHTTPResponse and itemChanged.ok) then
domoticz.log('In statement for itemChanged.isHTTPResponse - afterSetColorCallback": ', domoticz.LOG_DEBUG)
setColor()
-- Setting a color contains a property for the level. But for some weird reason the level is ignored and set to 100 (full, or something like it), that is why I set the level again...
setLevel(domoticz.data.lastLevel)
end
end
} |