Hue Lights Color from Realtime Energy Usage

Posted on
Mon Sep 16, 2013 12:29 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Hue Lights Color from Realtime Energy Usage

I thought I might share this with whomever is interested. This is how I've been using the Hue Lights plugin and Energy EAGLE plugin to set the color of a LightStrip and Bloom based on the current amount of power being used throughout the house.

  1. Create a "Device State Changed" type trigger. Select your energy meter device. Select the "Current Load" state. Select "Has Any Change" as the change type.
  2. Use any conditions you like, but none are necessary for this to work.
  3. For the Action, select "Execute Script" from the Server Actions. Choose Embedded Python as the script type and enter the following code.
    Code: Select all
    # Set the color of the light to represent household energy demand.
    #  - Deep blue = 0.0 kW
    #  - Solid green = 5.0 kW
    #  - Deep red = 10.0 kW
    # Change the highUsage value from 10 to whatever you like if 10 kW is too high or too low for you needs.

    highUsage = 10.0
    hueDev = indigo.devices[<device ID of hue light>] # Enter the device ID from Indigo
    energyDev = indigo.devices[<device ID of energy monitor>] # Enter the device ID from Indigo
    power = energyDev.states['energyInput1']
    saturation = 100 # (percent) Change if you like.
    rampRate = 10 # (seconds) Change if you like.

    plug = indigo.server.getPlugin('com.nathansheldon.indigoplugin.HueLights')

    # Convert the power demand into a hue degree and brightness.
    #   hue is based on maximum 10 kW load. 260 degrees = no load.
    #   brightness is based on maximum 10 kW load. 1 = no load.
    hue = int(round((highUsage - power) / highUsage * 260.0))
    # Make sure the hue doesn't go below 0 or above 260.
    if hue < 0:
       hue = 0
    if hue > 260:
       hue = 260
    brightness = int(round(power / highUsage * 100.0))
    # Prevent the lights from going completely off if power usage is nearly zero.
    if brightness == 0:
       brightness = 1
    # Prevent the brightness value from going over 100%.
    if brightness > 100:
       brightness = 100
    # Only proceed if the Hue Lights plugin is enabled.
    if plug.isEnabled():
       plug.executeAction("setHSB", hueDev.id, props={'hue':hue, 'saturation':saturation, 'brightness':brightness, 'rate':rampRate})
  4. Modify the devices and values above to fit your situation. Click the "Compile" button to ensure everything is okay. If so, click OK to save the trigger.

Note that this will change the color settings for your Hue Lights device every time the Current Load value changes, even slightly. You can prevent this by adding a condition in the above code so that the code only executes if the "energyInput1Delta" state of the Energy EAGLE device is greater than, say, 0.1. Alternatively, you can add a condition in the Conditions tab that requires the "Change in Current Load" device state (for the same energy meter device in the code above) is greater than, say, 0.1. This had one disadvantage, though. Because the "Change in Current Load" state is updated just slightly after the "Current Load" state, the trigger checks the "Change in Current Load" state before it can be updated, making this condition unreliable. Including a condition in the Python code above works better because, by the time the Actions are executed, the Change in Current Load state has been updated.

Posted on
Wed Sep 18, 2013 8:37 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

This is EXACTLY what I had in mind when I started looking at your more recent plugins (well, this and also a weather/outdoor temp-driven Hue bulb). This isn't necessarily why I bought an Energy Eagle, but it was definitely rattling around in the back of my head.

Damnit man, now I have to get a Hue starter kit - yet another $200 I hadn't planned on spending, but obviously must. ;)

Posted on
Wed Sep 18, 2013 8:51 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Hue Lights Color from Realtime Energy Usage

I agree - I'm finding it harder and harder to resist getting a starter kit as more and more ideas pop into my head like this.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Sep 18, 2013 11:04 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Hue Lights Color from Realtime Energy Usage

richy240 wrote:
This is EXACTLY what I had in mind when I started looking at your more recent plugins...

Awesome! My wife kept telling me I should share this little integration. Glad you can get some use out of it too!
nsheldon wrote:
Damnit man, now I have to get a Hue starter kit - yet another $200 I hadn't planned on spending, but obviously must.

Muhahahahahaaa... My maniacal plan is coming to fruition! LOL. ;-)
jay (support) wrote:
I agree - I'm finding it harder and harder to resist getting a starter kit as more and more ideas pop into my head like this.

Come to the Dark Side Jay! :-D

Posted on
Fri Oct 04, 2013 3:44 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

Found a small problem with the code above... In the last line, diningLight.id should be changed to hueDev.id. It should read as follows, I assume...

Code: Select all
   plug.executeAction("setHSB", hueDev.id, props={'hue':hue, 'saturation':saturation, 'brightness':brightness, 'rate':rampRate})


This is SUPER COOL. With only a few adjustments this is going to suit me just fine. :)

Posted on
Fri Oct 04, 2013 4:26 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Hue Lights Color from Realtime Energy Usage

Thanks for pointing that out. I've edited my post and corrected it to show "hueDev.id". Glad you can get some use from that. :-D

Posted on
Sat Oct 05, 2013 2:33 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

Ok, I've got this working! Although I'm experimenting with different Schedules and Triggers to actually trigger this whole ordeal, I've created an Action Group that pulses the bulb once:

  • Executes the setHSB action based on your script with a 0-second delay and a 1-second ramp rate
  • Executes the setBrightness action to 0 brightness and a 1-second ramp rate, and a 1-second delay on the action

Then I have a couple Action Groups that fire based on whether the bulb is already on or not. If it's not, it executes the Action Group described above three times (three 2-second pulses). If the bulb is already on, it dims to 0 brightness (like described above), executes the Action Group above twice (two 2-second pulses), and brightens the bulb to 80 brightness with a 1-second ramp rate.

Each Action Group's actions are spaced out appropriately using delays to allow the two- or three-pulse display to be 4 and 6 seconds respectively (2 seconds per pulse).

Took a while to figure it all out, but I'm pretty happy with the results so far. The alert action doesn't seem to let me customize the color of the light (unless I'm missing something), so unfortunately that didn't benefit me.

Posted on
Sat Oct 05, 2013 6:47 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Hue Lights Color from Realtime Energy Usage

Great! Glad you got it working the way you wanted. Yea, the alert action only blinks the bulb to 100% at the current color setting, you can't specify different colors for the alert. I guess you could set the color first, then set the alert, but I don't think that would save you anything in terms of lines of script needed. It'd probably increase it actually.

Posted on
Sun Oct 06, 2013 2:47 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

nsheldon wrote:
Great! Glad you got it working the way you wanted. Yea, the alert action only blinks the bulb to 100% at the current color setting, you can't specify different colors for the alert. I guess you could set the color first, then set the alert, but I don't think that would save you anything in terms of lines of script needed. It'd probably increase it actually.

I haven't had any luck setting the bulb color without it actually lighting up. Say, for instance, the current color was set using setCT to 3000K and the bulb was powered down (so to speak)... I haven't been able to set it at some RGB or hue value while keeping the brightness at 0 so I could then use an alert action. What am I doing wrong? Or does this not actually not work? More lines or less lines of code, I'd love to know how this could be done.

To accomplish what I'm doing, I am using a collection of Action Groups that execute Python scripts, delays on those Action Groups, and default light values when the bulb isn't providing feedback. It's WAY more complicated than it should be, but it does work rather well. I could make it a little more self-contained if there was a way to set delays on actions in Indigo using Python (such as the "Delay by" option in the Indigo UI; if this does exist, I can't seem to find it documented anywhere). I also don't know Python very well (kinda just learning it as I tinker with this stuff), so...

EDIT: It would appear that time.sleep() works in Indigo, so I should have this all wrapped up soon.

Posted on
Tue Oct 08, 2013 3:38 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Hue Lights Color from Realtime Energy Usage

Unfortunately, it doesn't seem to be possible to set the Hue devices' color settings without turning them on first, or at the same time. So I don't think you're doing anything wrong there.

I use multiple actions to accomplish most tasks (not just with the Hue stuff), so I think this is pretty common for anything but the most basic tasks in Indigo.

Yes, you can use time.sleep() within an embedded Python script. Keep in mind, however, that any delays within embedded scripts also delays the entire Indigo server, so if you put a time.sleep(2) line within the embedded script, your entire Indigo system will be effectively hung for that 2 seconds. Indigo will automatically kill the embedded script executor if it doesn't complete in less than 10 seconds, but that's still a pretty big delay in execution of other Indigo actions. It's still a better practice to use action delays within the Indigo GUI rather than building them into the embedded scripts.

Posted on
Tue Oct 08, 2013 4:11 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Hue Lights Color from Realtime Energy Usage

nsheldon wrote:
Keep in mind, however, that any delays within embedded scripts also delays the entire Indigo server, so if you put a time.sleep(2) line within the embedded script, your entire Indigo system will be effectively hung for that 2 seconds.


Well, not exactly. Any other embedded Python scripts that are scheduled to execute will be delayed by 2 seconds. The Python script execution process is a separate process from the server however (unlike AppleScript) so the server itself will continue to process everything else.

Otherwise, what he said... :)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 08, 2013 4:15 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Hue Lights Color from Realtime Energy Usage

Ah. Right you are! Thanks for correcting that. :-)

Posted on
Tue Oct 08, 2013 7:45 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

Well that sucks... I was looking forward to condensing four Action Groups into one for this, but now I'm not so sure. :(

Does this only apply to embedded scripts, or does it apply to the execution of all Python (such as those in plugins as well)?

EDIT: I think I answered my own question by reading the Plugin Guide documentation. Looks like a no, but I'd really appreciate someone confirming that for me. :P

Posted on
Tue Oct 08, 2013 10:02 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Hue Lights Color from Realtime Energy Usage

Only embedded scripts have the 10 second rule. Script files and plugins run in their own process and can do whatever they need to do.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 08, 2013 10:15 pm
richy240 offline
Posts: 111
Joined: Jul 08, 2012

Re: Hue Lights Color from Realtime Energy Usage

Thanks for the info. I'm a little less concerned about pausing execution in embedded scripts now, but I'll probably steer clear of it for safety's sake.

Thanks for all the help guys, the deeper I get into Indigo, the more fun it is! :lol:

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests