HUE Power Usage & Energy Script

Post Reply
trixwood
Posts: 510
Joined: Friday 18 March 2016 8:20
Target OS: -
Domoticz version: Offline
Contact:

HUE Power Usage & Energy Script

Post by trixwood » 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
Screen Shot 2016-05-01 at 09.42.09.jpg
Screen Shot 2016-05-01 at 09.42.09.jpg (237.27 KiB) Viewed 7147 times
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
    :D Change the hueWait which is number of seconds the script is executed, to keep the hue and domoticz responsive
    • ;) recommend 5 seconds
      :cry: 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.

    :P Change the hueRefresh which is number of seconds before the meters are updated anyway regardless is they where changed or not
    • :geek: recommend 60 seconds
    :?: Enter your IP and API key of the HUE bridge
    :!: 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

    Screen Shot 2016-05-01 at 09.31.37.jpg
    Screen Shot 2016-05-01 at 09.31.37.jpg (35.12 KiB) Viewed 7151 times
    Screen Shot 2016-05-01 at 09.31.28.jpg
    Screen Shot 2016-05-01 at 09.31.28.jpg (32.86 KiB) Viewed 7151 times
    :idea: 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)
    :roll: MAKE SURE YOU RESTART DOMOTICZ the new virtual sensors will not work in my version 3.5xxx until I restart... :-/
    :o Add this script in the event section, as a device event.
    :shock: Remove comment before the print statements for debug

    That's it... :ugeek:
Note In some versions commandArray[hue_name] = 'Off' works (v3.5076) but commandArray[otherdevices_idx[hue_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_name]..'|0|'.. 'Off' } does nothing, while in (v3.49) the first one sometimes works while the second one works perfectly... not sure if this is broken? The problem is only with the On/Off virtual sensor switch...btw ♫♬ squash that bug ♪♩♬

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
Last edited by trixwood on Sunday 29 May 2016 18:18, edited 10 times in total.

trixwood
Posts: 510
Joined: Friday 18 March 2016 8:20
Target OS: -
Domoticz version: Offline
Contact:

Re: HUE Power Usage & Energy Script

Post by trixwood » Friday 06 May 2016 10:50

Ha!

Now its completely working on the domonicz side, with no lag time anymore in domoticz. I would like some better readings of the powerusage of the HUE lamps. Because 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 (well if statement at the moment) which contains readings of the power usage corresponding to the brightness of the HUE light.

Anybody?

I do not have the old bulbs for example...

Kiliansitel
Posts: 1
Joined: Tuesday 31 May 2016 1:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HUE Power Usage & Energy Script

Post by Kiliansitel » Tuesday 31 May 2016 2:03

Hi there,

thanks for the script but having some ussues with this, did everything according to your instructions but I keep getting this error:

Error: EventSystem: in test: [string "-- TRiXWood May 2016..."]:266: attempt to concatenate field '?' (a nil value)

corresponding to this line in the script:

commandArray[otherdevices_idx[hue_power_name]] = { ['UpdateDevice'] = otherdevices_idx[hue_power_name]..'|0|'.. watt }

Also strange is that only a few lamps (6) are getting the power value the other 12 nothing.

my domoticz version is 3.5187

the issue is the same with your hue dimmer support script http://domoticz.com/forum/viewtopic.php ... 418#p85418

Thanks for your help !

Grtz

trixwood
Posts: 510
Joined: Friday 18 March 2016 8:20
Target OS: -
Domoticz version: Offline
Contact:

Re: HUE Power Usage & Energy Script

Post by trixwood » Thursday 02 June 2016 2:58

You probably have unsupported lamps... I cannot guess the wattage of lamps I do not have, you should add them, thats why you get 0 with some lamps :-)

Currently supported are

Model LWB006 (White Bulb)
Model LCT007 (800 lum v2 HUE bulb)

And you are welcome to add additional or better reading of the current lamps to this script by posting them here.

About the nil value, i don't have domoticz right here to test, but it is because of the missing lamps (which means there is a bug in the code) or that you mistyped somewhere and did not name them correctly, this weekend I hopefully have time to check, not promising anything.

Martijn85
Posts: 53
Joined: Wednesday 20 January 2016 21:21
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: HUE Power Usage & Energy Script

Post by Martijn85 » Monday 30 January 2017 11:33

I got the same message:

2017-01-30 10:32:00.293 LUA: Please add & submit to viewtopic.php?f=23&t=11778&p=84624#p84624
2017-01-30 10:32:00.306 LUA: HUE Power: Unsupported Lamp Detected Model LCT010
2017-01-30 10:32:00.306 LUA: Please add & submit to viewtopic.php?f=23&t=11778&p=84624#p84624
2017-01-30 10:32:00.320 LUA: HUE Power: Unsupported Lamp Detected Model LCT010
2017-01-30 10:32:00.320 LUA: Please add & submit to viewtopic.php?f=23&t=11778&p=84624#p84624
2017-01-30 10:32:00.333 LUA: HUE Power: Unsupported Lamp Detected Model LWB010
2017-01-30 10:32:00.333 LUA: Please add & submit to viewtopic.php?f=23&t=11778&p=84624#p84624
2017-01-30 10:32:00.333 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_hue_power.lua: /home/pi/domoticz/scripts/lua/script_time_hue_power.lua:150: attempt to concatenate field '?' (a nil value)

Will there be support for other lamps?

Gravityz
Posts: 284
Joined: Wednesday 16 December 2015 20:13
Target OS: NAS (Synology & others)
Domoticz version: V3.8637
Location: Netherlands
Contact:

Re: HUE Power Usage & Energy Script

Post by Gravityz » Saturday 18 February 2017 16:39

like this thread but can not get it to work since i do not have a valid luasocket for my synolgy 112+ (marcell cpu)

anybody knows where to get this

woodtrix
Posts: 37
Joined: Saturday 18 February 2017 18:54
Target OS: -
Domoticz version:
Contact:

Re: HUE Power Usage & Energy Script

Post by woodtrix » Saturday 18 February 2017 19:07

@Martijn85, all the measurements are done roughly, with a z-wave powernode 1... (just to remind you this is not an official/accurate measurement and the script gives an approximation of the actual power used, based on my measuring results...) I have quite a few new type of philips lamps but never bothered to measure them. You could measure yourself and add the missing lamps :-) You will get in trouble with the rgb strips extension cords... (how can you detect that... ;-) Or copy and paste the missing lamps from already measured ones... it will be off a little bit because lamps power consumption vary a little bit, but still give a little indication... but might I suggest appending the script with new measurements ;-)

Martijn85
Posts: 53
Joined: Wednesday 20 January 2016 21:21
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Netherlands
Contact:

Re: HUE Power Usage & Energy Script

Post by Martijn85 » Tuesday 14 March 2017 0:37

Ok thanks for the reply. Will look in to it, if in have some updates i will post them.

thijsvb
Posts: 1
Joined: Saturday 13 January 2018 12:53
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: HUE Power Usage & Energy Script

Post by thijsvb » Saturday 13 January 2018 13:20

Hi,

Thanks for the script. I'm trying to implement this at the moment.
I'm missing some lamps in het script.
The Innr GU10 light: RS 125
The hue iris color: LLC010
And the hue ambiance candle: LTW012

Would be great if these lamps could be added as well!

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

Re: HUE Power Usage & Energy Script

Post by ben53252642 » Sunday 14 January 2018 7:20

Trixwood, slightly un-related question... are you hearing a high pitch buzzing noise coming from your Lightstrips?

I've just added one to my wardrobe and the noise is driving me crazy.

assenzuid
Posts: 185
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: HUE Power Usage & Energy Script

Post by assenzuid » Tuesday 23 January 2018 22:24

thijsvb wrote:
Saturday 13 January 2018 13:20
Hi,

Thanks for the script. I'm trying to implement this at the moment.
I'm missing some lamps in het script.
The Innr GU10 light: RS 125
The hue iris color: LLC010
And the hue ambiance candle: LTW012

Would be great if these lamps could be added as well!
Also for the LWB010 and LCT012

PaulM
Posts: 10
Joined: Friday 13 November 2015 21:50
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: HUE Power Usage & Energy Script

Post by PaulM » Thursday 31 May 2018 15:58

I get the following error:

Code: Select all

2018-05-31 15:57:16.803 Error: EventSystem: in hue_status_update_2.lua: [string "-- TRiXWood May 2016 ..."]:52: bad argument #1 to 'sub' (string expected, got nil)
Line 52:

Code: Select all

 year = string.sub(s, 1, 4)
Any clue how to solve this?

phermn
Posts: 1
Joined: Monday 17 September 2018 11:23
Target OS: NAS (Synology & others)
Domoticz version: 4.9971
Contact:

Re: HUE Power Usage & Energy Script

Post by phermn » Sunday 23 September 2018 5:00

Hi PaulM, I'm having the exact same problem! Did you ever manage to resolve this issue by any chance?

User avatar
jeroenvanpelt
Posts: 21
Joined: Saturday 30 January 2016 14:19
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: HUE Power Usage & Energy Script

Post by jeroenvanpelt » Saturday 17 November 2018 18:03

phermn wrote:
Sunday 23 September 2018 5:00
Hi PaulM, I'm having the exact same problem! Did you ever manage to resolve this issue by any chance?
Most likely you have not created a Power and a Power Meter virtual sensor for every Hue light that is in your Domoticz. The error is triggered when the code tries to add up power values for Hue lights assuming you have created "<Light name> Power" and "<Light name> Power Meter" virtual sensors for every single one.
--
Domoticz latest stable - RaspberryPi 2 B - MySensors - Controlicz - Google Home - RFLink (w/MiLight)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests