Requesting Sunset Time For Current Day

Many of my scenes trigger on sunrise or sunset. I have a side project that could use access to the sunset time for the current day. Rather than go out to the web is there an HTTP query that can be run from the other device toward the VeraLite which would return the sunset time? Like I run http://:3480/data_request?id=sunset&output_format=json and it returns:

[{"id":"sunset","value":"184211"}] or something like this? JSON format would be best but that’s me just being picky :slight_smile:

Alternatively, could I define a scene to run LUUP code to send an HTTP GET to the other device like http://<other_device>/?sunset=184211. I can parse this on the far end if it’s possible but don’t know how to get the var from VL.

Does VeraLite even expose this variable? I don’t see it in the wiki pages [url=http://wiki.micasaverde.com/index.php/Luup_Requests]http://wiki.micasaverde.com/index.php/Luup_Requests[/url] and [url=http://wiki.micasaverde.com/index.php/Luup_UPnP_Variables_and_Actions]http://wiki.micasaverde.com/index.php/Luup_UPnP_Variables_and_Actions[/url].

Thanks!

These values are easily accessed in Lua on Vera, see here

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_sunset_.2F_sunrise

It is trivial to set up an HTTP handler in Lua Startup to respond to a request for these values.

Ask again if you need more info.

Hi akbooer - thanks for the reply. I’ve not set up any LUA startup items. I think a call to sunset() is what returns the timestamp in UNIX format - yes? The VL location is set (-7 for AZ). I can send the /GET to VL as needed. It’s coming from a Wemos D1 mini - which is of no significance.

I have no knowledge of setting up the handler. Any guidance is appreciated.

Thanks again.

An example of an HTTP handler defined at startup is described here.

http://forum.micasaverde.com/index.php/topic,39814.msg296454.html#msg296454

You would need to modify it to return sunrise/sunset which you could encode in JSON.

Ask again if you need more explicit directions.

Thanks again. I’m trying to adapt your code to suit the var I’m after.

[code]function HTTP_user (_,p)
luup.log ("HTTP_user: " … tostring(p))
for a,b in pairs (p) do global[a] = b end
local out = {}
for a,b in pairs (global) do out[#out+1] = a … ’ = ’ … b end
return table.concat (out, ‘\n’), “text/plain”
end

global = {} – ‘global’ table, call it whatever you like
luup.register_handler (“HTTP_user”, “user”)[/code]

If I send it

http://VeraIP:3480/data_request?a=sunset

How does that return the value for sunset() ?

I assume I change “text/plain” to “application/json” on the return line.

Where does the json formatting come from ? Or does this as written require a second var?

Thanks for sticking with me. This language is new to me.

Put this in your Startup Lua:

function HTTP_sun_rise_set ()
    local time_format = "%H:%M:%S"
    local json = [[{"sunrise":"%s", "sunset":"%s"}]]
    local rise = os.date (time_format, luup.sunrise())
    local set  = os.date (time_format, luup.sunset())
    return json:format (rise, set), "application/json"
end

luup.register_handler ("HTTP_sun_rise_set", "sun_rise_set")

Then an HTTP request like this:

http://VeraIP:3480/data_request?id=lr_sun_rise_set

will return:

{"sunrise":"04:46:31", "sunset":"21:27:04"}

Strictly speaking, these functions return the next sunrise and sunset, so usually within two minutes of what you need. Also, they are formatted in current local time, but this could be changed to anything you like.

akbooer, thank you for the code. I wouldn’t have figured that out anytime soon. I know providing turn-key code is frowned on but Lua isn’t clicking yet, so this helps me understand structure and keeps me moving forward.

I think there is something going on with sequencing or maybe a race condition. JSON returned from an Arduino is received successfully like this:

[code]HTTP/1.1 200 OK
Content-Type: application/json
Connection: close

[{“id”:“pumpWatts”,“value”:“340”},{“id”:“pumpRpm”,“value”:“1600”},{“id”:“poolTemp”,“value”:“85”},{“iy”,“value”:“4350”},{“id”:“chlorSetpoint”,“value”:“51”},{“id”:“poolMode”,“value”:“32”}]

JSON parseObject() pairing OK[/code]

But the JSON from Vera looks jumbled. The JSON is first, then the HTTP/1.1 200 OK is sent on the same line (without the \n). The end of the 200 OK there is a \n, and the next line I think is content-type or connection closed, but I only get the “c”.

{"sunrise":"05:17:02", "sunset":"19:37:49"}HTTP/1.1 200 OK c JSON parseObject() failed. Error 0x1B

UPDATED: I was able to get it to parse - the ArduinoJSON library requires open and close brackets [] around the braces {}. That plus the line feed to satisfy the Arduino code that splits client.read() chars on “\n”. Here’s the tweaked Lua that parses now… I’m sure it could be refined :wink:

[code]function HTTP_sun_rise_set ()
local time_format = “%H:%M:%S”
local json = [[%s{“sunrise”:“%s”, “sunset”:“%s”}%s%s]]
local rise = os.date (time_format, luup.sunrise())
local set = os.date (time_format, luup.sunset())
local crlf = “\r\n”
local openBracket = “[”
local closeBracket = “]”
return json:format (openBracket, rise, set, closeBracket, crlf), “application/json”
end

luup.register_handler (“HTTP_sun_rise_set”, “sun_rise_set”)[/code]

Which yields

HTTP/1.1 200 OK
content-Type: application/json

[{"sunrise":"05:17:02", "sunset":"19:37:49"}]

 JSON parseObject() pairing OK

Thanks!

Try changing the line

    local json = [[{"sunrise":"%s", "sunset":"%s"}]]

to this

    local json = [[{"sunrise":"%s", "sunset":"%s"}\r\n]]

Oops, I replied while you posted. Could you review the update in my previous post? Thanks

In that case, you could just replace the original line with:

    local json = '[{"sunrise":"%s", "sunset":"%s"}]'

No need, really, to use the format statement to insert literal constants.

OK, I wasn’t sure, because the original line had double brackets but were not in the response. I figured adding a third wouldn’t do anything.

local json = [[{"sunrise":"%s", "sunset":"%s"}]]

So this will do the same as the double brackets and I can pull the format statements?

local json = '[{"sunrise":"%s", "sunset":"%s"}]\r\n'

Yes. Lua has three types of string delimiters, actually more which you can define on the fly.

You need to revert to the original format statement for the sun rise/set times.

Great. I (you) have it working now. Thanks you very much for the help.