HUE Dimmer Switch
Posted: Monday 09 May 2016 4:22
TRiXWooD Scripts viewtopic.php?f=23&t=12145&p=87422#p87422
I've implemented a crude dimmer switch button implementation of the philips remote dimmer switch...
Ow no auto off yet...
Anyway same as the viewtopic.php?f=23&t=11778&p=84624#p846 ... 0Powercode but with the dimmer support.
If your dimmer switch is called "Bedroom Dimmer Remote" on the hue bridge you need to create 4 virtual switches
Bedroom Dimmer Remote On
Bedroom Dimmer Remote Up
Bedroom Dimmer Remote Down
Bedroom Dimmer Remote Off
Anyway response time is between instant and 3 seconds... not happy with that... the best lua script can do i think...
alpha... not finished
Example of usage: Color change of a hue light on dimmer switch press..
Tha Script!
I've implemented a crude dimmer switch button implementation of the philips remote dimmer switch...
Ow no auto off yet...
Anyway same as the viewtopic.php?f=23&t=11778&p=84624#p846 ... 0Powercode but with the dimmer support.
If your dimmer switch is called "Bedroom Dimmer Remote" on the hue bridge you need to create 4 virtual switches
Bedroom Dimmer Remote On
Bedroom Dimmer Remote Up
Bedroom Dimmer Remote Down
Bedroom Dimmer Remote Off
Anyway response time is between instant and 3 seconds... not happy with that... the best lua script can do i think...
alpha... not finished
Example of usage: Color change of a hue light on dimmer switch press..
Code: Select all
commandArray = {}
if (devicechanged['Hue dimmer switch 2 Button Up'] == 'On') then
brightness = 80
iswhite = "false"
hue = "0" -- red
idx = otherdevices_idx["Bol Lamp"] -- name lamp
url = 'http://10.0.0.1:8090/json.htm?type=command¶m=setcolbrightnessvalue&idx=' .. idx .. '&hue=' .. hue .. '&brightness=' .. brightness ..'&iswhite=' .. iswhite
commandArray['OpenURL']= url
end
return commandArray
Code: Select all
-- TRiXWood May 2016
-- https://www.domoticz.com/forum/viewtopic.php?f=23&t=11778&p=84624#p84624
-- https://www.domoticz.com/forum/posting.php?mode=edit&f=23&p=85418
-- configure Hue Bridge
local hueBridgeIP = '10.0.0.90'
local hueBridgeAPI = '73a968768758585ead808'
-- Max index of lights in the Philips App
-- this is not the number of lights you have!!!
-- since numbering continues if you delete a lamp, so check app!
local hueMaxIndex = 23
local hueMaxIndexSensors = 13
-- Wait time (sec) until script executes
local hueWait = 1
-- Wait time (sec) until refresh without change (so it stays blue not red)
local hueRefresh = 60
-- Names of the total power & powermeter sensors
local huePower = "HUE Power"
local huePowerMeter = "HUE Power Meter"
-- do not change beyond this line
-- this part will get the Hue status
function getHueLight(id)
local http = require('socket.http')
local ltn12 = require('ltn12')
local json = require('dkjson')
stop = false
t = {}
local url = string.format("http://%s/api/%s/lights/%s", hueBridgeIP, hueBridgeAPI, id)
b, c, h = http.request{url=url, sink = ltn12.sink.table(t), method='GET'}
huestring = tostring(table.concat(t))
local hue, pos, err = json.decode(huestring, 1, nil)
--print (huestring)
if (hue.name) then
hue_name = (hue.name)
hue_state = (hue.state.on)
hue_level = (hue.state.bri)
hue_reachable = (hue.state.reachable)
hue_model = (hue.modelid)
if not hue.state.reachable then hue_state = false end
else
hue_name = "ERROR"
stop = true
end
return hue_name, hue_state, stop
end
-- do not change beyond this line
-- this part will get the Hue status
function getHueSensor(id)
local http = require('socket.http')
local ltn12 = require('ltn12')
local json = require('dkjson')
stop = false
t = {}
local url = string.format("http://%s/api/%s/sensors/%s", hueBridgeIP, hueBridgeAPI, id)
b, c, h = http.request{url=url, sink = ltn12.sink.table(t), method='GET'}
huestring = tostring(table.concat(t))
local hue, pos, err = json.decode(huestring, 1, nil)
--print (huestring)
if (hue.modelid == "RWL021") then
hue_name = (hue.name)
hue_state = (hue.config.on)
hue_button = (hue.state.buttonevent)
--print (hue_button)
hue_button1 = false
hue_button2 = false
hue_button3 = false
hue_button4 = false
if (hue_button) then
x = tonumber(string.sub(tostring(hue_button), 1, 1))
if x == 1 then hue_button1 = true end
if x == 2 then hue_button2 = true end
if x == 3 then hue_button3 = true end
if x == 4 then hue_button4 = true end
x = string.sub(tostring(hue_button), 4, 4)
if x == 0 then hue_init = true end
if x == 1 then hue_hold = true end
if x == 2 then hue_shortreleased = true end
if x == 3 then hue_longreleased = true end
end
hue_init = false
hue_hold = false
hue_shortreleased = false
hue_longreleased = false
-- if (hue_button1) then print ("Button 1") end
-- if (hue_button2) then print ("Button 2") end
-- if (hue_button3) then print ("Button 3") end
-- if (hue_button4) then print ("Button 4") end
hue_battery = (hue.config.battery)
-- print (hue_battery)
hue_reachable = (hue.config.reachable)
--if (hue_reachable) then print ("yes") end
-- print (hue_state)
hue_model = (hue.modelid)
-- print (hue_model)
if not hue.config.reachable then hue_state = false end
-- print (hue_reachable)
else
hue_name = "ERROR"
stop = true
end
return hue_name, hue_state, stop
end
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
-- now check Hue state & correct Domoticz if needed
commandArray = {}
x = timedifference(otherdevices_lastupdate[huePower])
print(x)
if timedifference(otherdevices_lastupdate[huePower]) > hueWait then
i = 1
powertotal = 0
repeat
local hue = getHueLight(i)
-- Powercode!!!
-- new powercode create for every lamp a virtual usage (energy) sensor called the name of the lamp with "Power"
-- Example: "Bureau Lamp" you will need to add virtual usage (energy) sensor called "Bureau Lamp Power"
hue_power_name = hue_name .. " Power"
hue_powermeter_name = hue_power_name .. " Meter"
-- I thought level would be 0..99 but domoticz its 0..15 und hue its 0..254 or something
-- Anyway defaulting to 0..99
level = 0
if hue_state then
level = hue_level / 2.56
end
-- Philips Light Measuring with Powernode 1 (inaccurate :-)
modelfound = false
-- Model LCT007 (800 lum v2 HUE bulb)
if hue_model == "LCT007" then
modelfound = true
if level <= 0 then watt = 0.4 end
if (level > 0 and level <= 6) then watt = 1.6 end
if (level > 6 and level <= 13) then watt = 1.7 end
if (level > 13 and level <= 20) then watt = 1.8 end
if (level > 20 and level <= 26) then watt = 1.9 end
if (level > 26 and level <= 33) then watt = 2.2 end
if (level > 33 and level <= 40) then watt = 2.4 end
if (level > 40 and level <= 46) then watt = 2.7 end
if (level > 46 and level <= 53) then watt = 3.0 end
if (level > 53 and level <= 60) then watt = 3.5 end
if (level > 60 and level <= 66) then watt = 3.9 end
if (level > 66 and level <= 73) then watt = 4.5 end
if (level > 73 and level <= 80) then watt = 5.1 end
if (level > 73 and level <= 86) then watt = 5.5 end
if (level > 86 and level <= 93) then watt = 6.4 end
if (level > 93) then watt = 6.7 end
end
-- Model LWB006 (White Bulb)
if hue_model == "LWB006" then
modelfound = true
if level <= 0 then watt = 0.3 end
if (level > 0 and level <= 6) then watt = 1.4 end
if (level > 6 and level <= 13) then watt = 1.5 end
if (level > 13 and level <= 20) then watt = 1.7 end
if (level > 20 and level <= 26) then watt = 1.8 end
if (level > 26 and level <= 33) then watt = 2.0 end
if (level > 33 and level <= 40) then watt = 2.2 end
if (level > 40 and level <= 46) then watt = 2.5 end
if (level > 46 and level <= 53) then watt = 2.9 end
if (level > 53 and level <= 60) then watt = 3.5 end
if (level > 60 and level <= 66) then watt = 3.8 end
if (level > 66 and level <= 73) then watt = 4.6 end
-- fucked up here nog een keer meten 5.2W ???
if (level > 73 and level <= 80) then watt = 5.5 end
if (level > 80 and level <= 86) then watt = 6.5 end
if (level > 86 and level <= 93) then watt = 7.7 end
if (level > 93) then watt = 9.0 end
end
-- Model LST001 Lightstrip
if hue_model == "LST001" then
modelfound = true
if level <= 0 then watt = 0.4 end
if (level > 0 and level <= 6) then watt = 1.6 end
if (level > 6 and level <= 13) then watt = 1.7 end
if (level > 13 and level <= 20) then watt = 1.8 end
if (level > 20 and level <= 26) then watt = 1.9 end
if (level > 26 and level <= 33) then watt = 2.2 end
if (level > 33 and level <= 40) then watt = 2.4 end
if (level > 40 and level <= 46) then watt = 2.7 end
if (level > 46 and level <= 53) then watt = 3.0 end
if (level > 53 and level <= 60) then watt = 3.5 end
if (level > 60 and level <= 66) then watt = 3.9 end
if (level > 66 and level <= 73) then watt = 4.5 end
if (level > 73 and level <= 80) then watt = 5.1 end
if (level > 73 and level <= 86) then watt = 5.5 end
if (level > 86 and level <= 93) then watt = 6.4 end
if (level > 93) then watt = 6.7 end
end
if not stop and not modelfound then
print("HUE Power: Unsupported Lamp Detected Model "..hue_model)
print("Please add & submit to https://www.domoticz.com/forum/viewtopic.php?f=23&t=11778&p=84624#p84624")
watt = 0
end
if not hue_reachable then watt = 0 end
if not stop then powertotal = powertotal + watt end
if not stop and (otherdevices_svalues[hue_power_name] ~= tostring(watt) or timedifference(otherdevices_lastupdate[hue_power_name]) > hueRefresh) then
--print ("HUE Power (" .. hue_power_name .. ") Power: " .. otherdevices_svalues[hue_power_name] .. " Watt -> " .. watt .. " Watt")
commandArray[otherdevices_idx[hue_power_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_power_name]..'|0|'.. watt }
powerold, energyold = string.match(otherdevices_svalues[hue_powermeter_name], "(%d+%.*%d*);(%d+%.*%d*)")
interval = (timedifference(otherdevices_lastupdate[hue_powermeter_name]) / 3600)
commandArray[otherdevices_idx[hue_powermeter_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_powermeter_name] .."|0|" .. watt .. ";" .. (powerold*interval+energyold) }
--print ("HUE Power (" .. hue_powermeter_name .. ") Power: " .. powerold .. " Watt -> " .. watt .. " Watt - Usage: " .. energyold .. " kWh -> " .. powerold*interval+energyold .. " kWh")
end
if not stop and hue_state == true and otherdevices[hue_name] == 'Off' then
-- DOES NOT WORK sometimes it updates sometimes it doesnot...
commandArray[hue_name] = 'On'
-- USE
-- commandArray[otherdevices_idx[hue_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_name]..'|0|'.. 'On' }
-- print ("HUE Correction (" .. hue_name .. ") State: Off -> On")
end
if not stop and hue_state == false and otherdevices[hue_name] ~= 'Off' then
commandArray[hue_name] = 'Off'
-- commandArray[otherdevices_idx[hue_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_name]..'|0|'.. 'Off' }
-- print ("HUE Correction (" .. hue_name .. ") State: On -> Off")
end
i = i + 1
-- more hue lamps then change this...
until(i > hueMaxIndex)
i = 1
repeat
local hue = getHueSensor(i)
hue_button1_name = hue_name .. " Button On"
if not stop and hue_state and hue_button1 and otherdevices[hue_button1_name] == 'Off' then
-- print ("HUE Remote Button (" .. hue_button1_name .. ") Off -> On")
-- commandArray[otherdevices_idx[hue_button1_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button1_name]..'|0|'.. 'On' }
commandArray[hue_button1_name] = 'On'
end
if not stop and not hue_button1 and otherdevices[hue_button1_name] ~= 'Off' then
-- print ("HUE Remote Button (" .. hue_button1_name .. ") On -> Off")
-- commandArray[otherdevices_idx[hue_button1_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button1_name]..'|0|'.. 'Off' }
commandArray[hue_button1_name] = 'Off'
end
hue_button2_name = hue_name .. " Button Up"
if not stop and hue_state and hue_button2 and otherdevices[hue_button2_name] == 'Off' then
-- print ("HUE Remote Button (" .. hue_button2_name .. ") Off -> On")
commandArray[hue_button2_name] = 'On'
-- commandArray[otherdevices_idx[hue_button2_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button2_name]..'|0|'.. 'On' }
end
if not stop and not hue_button2 and otherdevices[hue_button2_name] ~= 'Off' then
-- print ("HUE Remote Button (" .. hue_button2_name .. ") On -> Off")
commandArray[hue_button2_name] = 'Off'
-- commandArray[otherdevices_idx[hue_button2_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button2_name]..'|0|'.. 'Off' }
end
hue_button3_name = hue_name .. " Button Down"
if not stop and hue_state and hue_button3 and otherdevices[hue_button3_name] == 'Off' then
-- print ("HUE Remote Button (" .. hue_button3_name .. ") Off -> On")
commandArray[hue_button3_name] = 'On'
-- commandArray[otherdevices_idx[hue_button3_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button3_name]..'|0|'.. 'On' }
end
if not stop and not hue_button3 and otherdevices[hue_button3_name] ~= 'Off' then
-- print ("HUE Remote Button (" .. hue_button3_name .. ") On -> Off")
commandArray[hue_button3_name] = 'Off'
-- commandArray[otherdevices_idx[hue_button3_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button3_name]..'|0|'.. 'Off' }
end
hue_button4_name = hue_name .. " Button Off"
if not stop and hue_state and hue_button4 and otherdevices[hue_button4_name] == 'Off' then
-- print ("HUE Remote Button (" .. hue_button4_name .. ") Off -> On")
commandArray[hue_button4_name] = 'On'
-- commandArray[otherdevices_idx[hue_button4_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button4_name]..'|0|'.. 'On' }
end
if not stop and not hue_button4 and otherdevices[hue_button4_name] ~= 'Off' then
-- print ("HUE Remote Button (" .. hue_button4_name .. ") On -> Off")
commandArray[hue_button4_name] = 'Off'
-- commandArray[otherdevices_idx[hue_button4_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_button4_name]..'|0|'.. 'Off' }
end
i = i + 1
until(i > hueMaxIndexSensors)
if (otherdevices_svalues[huePower] ~= tostring(powertotal)) or timedifference(otherdevices_lastupdate[huePower]) > hueWait then
commandArray[otherdevices_idx[huePower]] = { ['UpdateDevice'] = otherdevices_idx[huePower]..'|0|'.. powertotal }
powertotalold, energytotalold = string.match(otherdevices_svalues[huePowerMeter], "(%d+%.*%d*);(%d+%.*%d*)")
interval = (timedifference(otherdevices_lastupdate[huePowerMeter]) / 3600)
commandArray[otherdevices_idx[huePowerMeter]] = { ['UpdateDevice'] = otherdevices_idx[huePowerMeter] .."|0|" .. powertotal.. ";" .. (powertotalold*interval+energytotalold) }
-- print ("HUE Power (" .. huePower .. ") Power: " .. otherdevices_svalues[huePower] .. " Watt -> " .. powertotal .. " Watt")
-- print ("HUE Power (" .. huePowerMeter .. ") Power: " .. powertotalold .. " Watt -> " .. powertotal .. " Watt - Usage: " .. energytotalold .. " kWh -> " .. powertotalold*interval+energytotalold .. " kWh")
end
end
return commandArray