HUE Power Usage & Energy Script
Posted: Sunday 01 May 2016 5:21
Power HUE
TRiXWooD Scripts viewtopic.php?f=23&t=12145&p=87422#p87422
This is my script for adding virtual Usage and Energy sensors for philips hue lamps and fixing the simple update script ...
This is an approximation in no way are these the real values, there are no sensors on the HUE bulbs and strips. What this script does is using a lookup table which contains readings of the power usage corresponding to the brightness of the HUE light.
Update: with hue remote dimmer switch support see: http://domoticz.com/forum/viewtopic.php ... 418#p85418
I used the simple update script for hue as basis... work in progress... Currently thinks all lamps are white bulbs since i did not tried a colored one... but can easily be adjusted buy detecting the right bulb (from the hue json).. Also the reading are done with my powernode 1 probably not that accurate... but this is all approximations anyway... I just wanted to know how much energy the lamp will cost me
* Improvement of Simple HUE Script (detect unreachable & fix bug)
* Individual & Total Power/Energy virtual readings
It incorperate the SimpleHueScript for updating at the moment.. which is broken btw... that why you have to also change the number of the highestnumberlamp with the highest number you can find in you hue app... (the script is broken because you can delete bulbs which makes a hole in the index of the lamps which the original scripts detects as "we're done"... https://www.domoticz.com/wiki/Simple_sc ... Hue_status
Usage
v0.1 Tryout
v0.2 Typos & Bug fixed
v0.3 Time Bug Fixed
v0.4 Detect Lamp Types (HUE 800 lum & White added)
TRiXWooD Scripts viewtopic.php?f=23&t=12145&p=87422#p87422
This is my script for adding virtual Usage and Energy sensors for philips hue lamps and fixing the simple update script ...
This is an approximation in no way are these the real values, there are no sensors on the HUE bulbs and strips. What this script does is using a lookup table which contains readings of the power usage corresponding to the brightness of the HUE light.
Update: with hue remote dimmer switch support see: http://domoticz.com/forum/viewtopic.php ... 418#p85418
I used the simple update script for hue as basis... work in progress... Currently thinks all lamps are white bulbs since i did not tried a colored one... but can easily be adjusted buy detecting the right bulb (from the hue json).. Also the reading are done with my powernode 1 probably not that accurate... but this is all approximations anyway... I just wanted to know how much energy the lamp will cost me
* Improvement of Simple HUE Script (detect unreachable & fix bug)
* Individual & Total Power/Energy virtual readings
It incorperate the SimpleHueScript for updating at the moment.. which is broken btw... that why you have to also change the number of the highestnumberlamp with the highest number you can find in you hue app... (the script is broken because you can delete bulbs which makes a hole in the index of the lamps which the original scripts detects as "we're done"... https://www.domoticz.com/wiki/Simple_sc ... Hue_status
Usage
- Change the number hueMaxIndex which is the highest number in lamps section in the hue app
Change the hueWait which is number of seconds the script is executed, to keep the hue and domoticz responsive- recommend 5 seconds
you can use 0 but even with the original simple script, the lag between the z-wave door sensor and my hue light is in the range of 0.5 seconds to 20s slower, not recommended.
Change the hueRefresh which is number of seconds before the meters are updated anyway regardless is they where changed or not- recommend 60 seconds
If your HUE Lamp is called "Bureau Lamp" you will need to add new virtual sensors "Bureau Lamp Power" & "Bureau Lamp Power Meter". Do this for all your lights
Add virtual sensors "HUE Power" and "HUE Power Meter" which are the accumulation of all the HUE lamps individual components (I mean total summation of all the... you get it the TOTAL)
MAKE SURE YOU RESTART DOMOTICZ the new virtual sensors will not work in my version 3.5xxx until I restart... :-/
Add this script in the event section, as a device event.
Remove comment before the print statements for debug
That's it... - recommend 5 seconds
v0.1 Tryout
v0.2 Typos & Bug fixed
v0.3 Time Bug Fixed
v0.4 Detect Lamp Types (HUE 800 lum & White added)
Code: Select all
-- TRiXWood May 2016
-- https://www.domoticz.com/forum/viewtopic.php?f=23&t=11778&p=84624#p84624
-- configure Hue Bridge
local hueBridgeIP = '10.0.0.90'
local hueBridgeAPI = '73a9ade4584754567397aa767b88'
-- 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
-- Wait time (sec) until script executes
local hueWait = 5
-- 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
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 = {}
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 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")
elseif 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)
if (otherdevices_svalues[huePower] ~= tostring(powertotal)) or timedifference(otherdevices_lastupdate[huePower]) > hueRefresh 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