4

I'm trying to accomplish something simple. When someone turns on a hue light, if it's after 6pm, set the light to a specific color.

I haven't been able to find a way to do this without polling constantly (which seems lame).

The main Hue iOS app has alarms, but lights only respond to alarms when they are turned on. So the app's alarms also fail to set a specific color after 6pm when a light is turned on at 7pm.

anthonysapien
  • 67
  • 1
  • 6
  • I don't know anything about hue, but surely you'll get some sort of exception if it's off and not responding? But, why don't you just send the 'change colour' command once and not worry -- if it's on it'll work and if it's off it doesn't matter you've only wasted a couple of moments? – Software Engineer Nov 15 '15 at 00:42

2 Answers2

5

The API has now been updated to support this. A rule with the following conditions can be stored on the bridge so no need to keep polling from an app. First condition specifies the times you want the rule to trigger, second condition specifies a certain light must be on and third condition specifies that light must have just changed from being off.

"address":"/config/localtime", "operator":"in", "value":"T18:00:00/T23:00:00"

"address":"/lights/1/state/on","operator":"eq","value":"true"

"address":"/lights/1/state/on","operator":"dx"

miknik
  • 5,748
  • 1
  • 10
  • 26
  • Can you point me to the documentation for this? I cannot find it anywhere. I'm trying to monitor any changes to light color, not just on and off. @miknik – Christian Stavro Dec 06 '18 at 20:31
  • 1
    @ChristianStavro The hue api is fully documented at https://developers.meethue.com/develop/hue-api/ There is no support for rule creation based upon changes to lamp colours, the only way you can achieve this is by writing your own script to poll the values from the hub and react to changes that way. – miknik Dec 06 '18 at 20:57
  • thanks for the response. This is what I am currently doing via the heartbeat method and the fetch method in the swift/obj sdks. Unfortunately the data I get back is super stale and even though I have an open issue for it on the GitHub page, I was hoping you had some cool trick with rules to make it work :) @miknik – Christian Stavro Dec 11 '18 at 21:16
  • @ChristianStavro I wrote a python script to poll the REST api every second and parse the returned JSON and the data returned is always accurate for me. I think it may depend on how you are controlling the lights, for example if you set a colour change from red to blue with a transition of 600 seconds I don't think the changing colour value will be returned each time you poll the bridge, it will show the bulb value as being blue throughout the transition. Sensor values for the light level and temp from the motion sensors can appear stale as they only update every 5 minutes. – miknik Dec 11 '18 at 22:06
  • @ChristianStavro The only trick you can possibly implement is to create some custom sensors within the bridge and base some rules on these sensor values. This allows you to create some flexibility but it won't provide a work around in all situations, again it all depends on exactly what you are trying to do and how you are controlling the lights in the first place. – miknik Dec 11 '18 at 22:11
0

But a Hue app using the API is already polling the bridge, at whatever the heartbeat is set to. So, when the heartbeat fires, you read the cache, inspect you light's state, and store its "reachable" value, which is false if the light is physically off, and true if it is physically on. The next time through the loop, check it again. If it was false and is now true, bingo: the light was just turned on, so send it a command to set the color you want.

Because the API uses heartbeat-based polling, not callbacks or interrupts, this is the best you can do to detect external changes (like a light being physically switched on or off, or a light being changed by some other application, IFTTT rule, etc.) You do have control over the heartbeat interval of resources by type, so you can poll the lights more frequently to be able to react more quickly.

Ron Reuter
  • 1,287
  • 1
  • 8
  • 14
  • The whole Hue API and bridge system gives me goose bumps. A heartbeat to the bridge is so lame, but what's worse, the bridge only responds with the last known value the bridge had for the light. The bridge itself isn't smart enough to ask the light, "hey cool bright light, how you doin?" (You use to be able to test this by forcibly turning the light on/off at the wall switch). It's been a long time since I coded anything for Hue and maybe this has been fixed? But it never handled it before and you could get bogus (out of date) readings back. I just wish we could talk to the lights directly :/ – Madivad Apr 28 '16 at 11:47