How to use sunset and sunrise data

caprica picture caprica · Nov 6, 2014 · Viewed 12.2k times · Source

I see that the Hue API provides, on the "Daylight" sensor, fields for geographic location, and a sunrise/sunset offset.

To be specific:

lat
long
sunriseoffset
sunsetoffset

The API as currently published does not provide any information that I could find on how the sunrise/sunset can be used.

If I use the update sensor API to set the latitude and longitude, will the sunrise/sunset offsets be populated with data automatically, and I can query those to adjust my light schedule accordingly? I want to do e.g. "every day, light on at sunset + 20 minutes".

I was going to implement the necessary algorithm myself, but in light of these fields do I need to?

When testing this, I updated the latitude and longitude on the sensor config, here is a dump of the result of querying the sensor after the update:

{
  "state": {
      "daylight":false,
      "lastupdated":"2014-11-06T19:19:31"
  },
  "config": {
      "on":true,
      "long":"1.5333W",
      "lat":"56.2442N",
      "sunriseoffset":30,
      "sunsetoffset":-30
  },
  "name":"Daylight",
  "type":"Daylight",
  "modelid":"PHDL00",
  "manufacturername":"Philips",
  "swvversion":"1.0"
}

You can see the latitude and longitude values that I set (by default they are 'none').

It's now evident that the sunrise and sunset offsets are not calculated values. They are instead used to configure when the sensor value trips over from daylight to not-daylight or vice versa - for example, daylight becomes true "sunrise offset" minutes after sunrise.

Does the bridge know about sunrise and sunset times for the given geographic location?

If so, can I reliably query this sensor to determine daylight or not-daylight taking into account sunrise and sunset?

Answer

Jesper Riemer Andersen picture Jesper Riemer Andersen · Nov 11, 2014

There is a description of the Daylight sensor at Supported sensors page, a description of sensors at the Sensors page, and you need Rules to make good use of the sensors.

The Daylight sensor will set the state value daylight to true when there is daylight and false when there is not, of course taking into account the offset (in minutes) you specified in the sensor config.

To change your daylight sensor config, use PUT on /api/<username>/sensors/1/config with the following body:

{
    "long": "1.5333W",
    "lat": "56.2442N",
    "sunriseoffset": 30,
    "sunsetoffset": -30
}

Which means the state daylight value will change to false 30 mintues before sunset, and true 30 minutes after sunrise, where the sunset/sunrise times are calculated based on your long (longitude) and lat (latitude) values.

In order to e.g. turn on your lights at sunset, you need to specify a rule with the condition that the daylight value must be equal to false.

Use a POST on /api/<username>/rules with the following body:

{
    "name": "Daylight rule",
    "conditions": [
        { 
            "address": "/sensors/1/state/daylight",
            "operator": "eq",
            "value": "false"
        }
    ],
    "actions": [    
        {
            "address": "/groups/0/action",
            "method": "PUT",
            "body": { "on":true, "bri":254 }
        }
    ]
}

Where the condition for the rule is that the state daylight value must be eq(equal) to the value false.