We have moved at community.getvera.com

Author Topic: Weather Plugin  (Read 220806 times)

Offline 325xi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1101
  • Karma: +0/-0
  • V1, V2, still V2...
Weather Plugin
« on: July 06, 2009, 11:24:22 pm »
I remember a hypothetical plugin, mentioned in Wiki, creating a virtual device that takes data from weather.com to make it available to other devices.

1. How do we do "wget" in LUA? Or what is the best way to do HTTP GET from LUA without going low level?
2. Is there easy way to process/parse HTTP response in Lua?
3. In what variable scope data should be stored to be available globally for all Lua resources on the device?
« Last Edit: May 07, 2011, 11:08:32 pm by guessed »

Offline micasaverde

  • Hero Member
  • *****
  • Posts: 1666
  • Karma: +15/-1
Re: Plugin for Web: weather.com et al
« Reply #1 on: July 07, 2009, 10:20:11 am »
I just added: lu_wget (see: http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#General_purpose).  This will be in the next release in a couple days.

Offline 325xi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1101
  • Karma: +0/-0
  • V1, V2, still V2...
Re: Plugin for Web: weather.com et al
« Reply #2 on: July 07, 2009, 10:29:02 am »
Great!

What about the two other questions? :)

Offline anker

  • Full Member
  • ***
  • Posts: 107
  • Karma: +0/-0
Re: Plugin for Web: weather.com et al
« Reply #3 on: July 07, 2009, 10:41:02 am »
...and what about implementation of curl?

http://curl.haxx.se/libcurl/lua/

Is it possible to add as well?

/Anker

Offline micasaverde

  • Hero Member
  • *****
  • Posts: 1666
  • Karma: +15/-1
Re: Plugin for Web: weather.com et al
« Reply #4 on: July 09, 2009, 03:54:22 pm »
765 has the wget now.

As far as parsing the response, I don't have much experience with Lua's text parsing.  I do basic stuff in my implementation files, like gc100, etc.  But I know Lua has pretty powerful parsing and supports regular expressions.  I think there's a tutorial on text handling in Lua at lua.org

Any variable that you don't put 'local' in front of is global automatically within that Lua instance.  So, if you add inside a snipped of code attached to a scene:

myvariable="abc"

in some other scene, you can read that variable.  The same is true in implementation files.  Look at: I_PanasonicPTZ.xml (in >=765) and note that I retrieve the http authentication in the startup, store it in a variable, and use it in the jobs.

As far as the scope and Lua sessions/states...  There is one "Master Lua State", which is what all the code attached to scenes and events goes into.  Also, any .lua files that you explicitly import into with Devices, Luup plugins, 'extra lua files' go into the 'master state'.  When you choose 'test lua code', if you leave the device id blank, or 0, your code is run inside the 'master state'.

Every top level device is given it's Lua state as well.  This is basically a separate engine, with separate variables, etc., to provide isolation if one device acts up so it won't effect the others, and so if one device creates a global variable, like "IP", and another device uses the same global variable, they won't conflict.  So, the GC100, which is actually one top level device and a bunch of child devices (i/r transmitter, relay, etc.), all the implementation files for the gc100 and children run in their own Lua state.  All the Z-Wave devices (again, 1 master, and many children) run in their own Lua state too.  And, when you create your own lua device, like the Somfy blinds, it also has it's own state.

Offline micasaverde

  • Hero Member
  • *****
  • Posts: 1666
  • Karma: +15/-1
Re: Plugin for Web: weather.com et al
« Reply #5 on: July 10, 2009, 10:38:33 pm »
We'll add the curl library on Monday.  This should make a lot of the plugins like weather.com interfaces much easier to use.

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Plugin for Web: weather.com et al
« Reply #6 on: July 11, 2009, 02:23:51 am »
If it helps, heres a few "stub" LUA routines that will parse the content of Yahoo's Weather service RSS.  I used this as an exercise to "learn" how to write LUA so the edges are really ragged:

    http://www.box.net/shared/n2j62yq7uv

To test-author this stuff I'm using the freestanding version LUA engine, and I'm running the Eclipse development env with the LUA Plugin (on a Mac, if it matters) to give me a GUI Editor.

Anyhow, the text above sample has a few crude parsing routines that can extract the Element/Attr values from the Yahoo feed.  To make it work outside Vera, I've mocked up a lu_wget() function, similar to the one that I'd have if I deployed it.... but I've never tested it in Vera itself.


Here's some sample output from running it.  I imagine you could use this stuff to create a Weather Device, but I haven't got that far (MCV just fixed a Lua Startup problem in my Vera)

I used Micasa's ZIP code to generate the output

Code: [Select]
Element: geo:lat, value: 39.53
Closed Element: yweather:condition
  Attribute date=Fri, 10 Jul 2009 9:55 pm PDT
  Attribute text=Mostly Cloudy
  Attribute temp=74
  Attribute code=27
Element: guid, value: USNV0076_2009_07_10_21_55_PDT
  Attribute isPermaLink=false
Closed Element: yweather:units
  Attribute speed=mph
  Attribute temperature=F
  Attribute pressure=in
  Attribute distance=mi
Closed Element: yweather:location
  Attribute country=US
  Attribute city=Reno
  Attribute region=NV

In Reno, it's currently 74(F) and Mostly Cloudy




I'm using this as the reference manual for Lua:
    http://www.lua.org/manual/5.1/manual.html#2.5

and this for the Lua Eclipse plugins:
    http://luaeclipse.luaforge.net/manual.html#introduction


Hoepfully this will get you a little further with what you want to build.

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Plugin for Web: weather.com et al
« Reply #7 on: August 15, 2009, 08:28:13 pm »
ok, so with some prodding from a more than a few...   The attached files implement a Weather device, reporting back the following properties, each displayed as "children" of the Parent Weather object:

    [Current] Temperature
    Low Temperature
    High Temperature
    [Current] Humidity

The device will "poll" for this information every ~30 minutes.

To get it going (using a newer Luup release like 1.0.862 etc):

  • 1. Download the 3x files attached to this posting in a ZIP file (Weather.zip, and upack to get the 3x XML Files inside)
  • 2. goto Devices --> Luup plugins --> Luup files
  • 3. Click the (Browse) buttons on the right, and enter the 3 filenames downloaded in (1)
  • 4. Select the checkbox called  "\[x\] Restart Luup after upload" and click (Go)
  • 5. ... wait whilst it uploads and restarts the Lua engine ...
  • 6. If this is successful, you should see the 3 files now loaded into the System.
  • 7. Now navigate to Devices, scroll to the bottom at the Add device form.
  • 8. Type "D_Weather.xml", and click (Add Device), type in a name of your choice against the device then click (Save)
  • 9. At this point you should have a working Weather component.
Please post on this thread if this doesn't work, providing detailed screenshots and log output, and I'll do my best to fix anything I can where there's enough detail in the forum posting to work it out.

This will get easier when it can be included in the Luup catalog...


When it starts, it prints  the following type of log entries:
Code: [Select]
50 08/15/09 16:28:50.068 luup_log:133: Google Weather #133 starting up with id  <0x803>
...
50 08/15/09 16:28:51.661 luup_log:133: At [-53.080002 ,73.489609], it's currently 26(US) and Clear.  Humidity is 1%.  Low is 30 High is 32 <0x803>

(given a device id of "133")

For a location like:
    http://maps.google.com/?ie=UTF8&ll=-53.080002,73.489609&spn=0.271404,0.464172&t=h&z=11

The Weather device will fire off a URL like:
    http://www.google.com/ig/api?weather=,,,-53080002,73489609

I have no idea how accurate this data is, how often it's updated, and where there's coverage so we'll roll the dice on that.  But, from a few samples (like the above) it has some offbeat places ;)

The data reported back is imperial, not metric... for now.



Many thanks to the pointers provided by @LibraSun and @Ap15e.  These suggestions made be cutover from using the Yahoo! Weather API to using the [undocumented] Google equivalent, since it's parameters are more flexible.

As a result, if you've established Vera's "Location" settings correctly, then this device should report the weather correctly.

It relies upon Vera's Luup releases, and I've only tried it on 1.0.862.  There will be a warning in the log output about the S_Weather.xml file.  This is incomplete, and designed for future Location-based overrides.  It can be ignored for now.

It could also do with some error handling, since it really doesn't have any right now ;)

(modified to remove the legacy version of Weather.zip, newer version attached later in forum thread)
« Last Edit: August 31, 2009, 10:34:23 pm by guessed »

Offline 325xi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1101
  • Karma: +0/-0
  • V1, V2, still V2...
Re: Plugin for Web: weather.com et al
« Reply #8 on: August 15, 2009, 08:45:25 pm »
Can anyone see the attachments?

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Plugin for Web: weather.com et al
« Reply #9 on: August 15, 2009, 08:50:39 pm »
ok, for backup, I've uploaded Weather.zip to box.net as:

    [legacy, removed]

and the screenshot of the running device is:

    [legacy, removed]

I can usually see others GIF Attachments, so no idea why these wouldnt work.  As a side-note, it's amusing that you cannot upload .XML files as attachments to the forums, since that's the format used for Luup device files ;)

UPDATE: Removed box.net versions, since they're antique and are now on code.mios.com (et-al)
« Last Edit: February 07, 2012, 05:56:07 pm by guessed »

Offline LibraSun

  • Hero Member
  • *****
  • Posts: 574
  • Karma: +2/-0
Re: Plugin for Web: weather.com et al
« Reply #10 on: August 15, 2009, 09:53:38 pm »
So, you've already pieced together the following:

(1) Can get Celsius results using: http://www.google.co.uk/ig/api?weather=[ZIP]

(2) Full Google Weather API documentation here: http://www.codeplex.com/awAPI

Glad you got this project underway.  Sounds fun!
Vera Model I running UI4 (Firmware 1.1.1338), died in 2015
Vera Plus running UI7 (Firmware 1.7.2935)

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Plugin for Web: weather.com et al
« Reply #11 on: August 15, 2009, 10:08:05 pm »
Yes, I should have been clearer.  I have both the Metric (C) feed info, and the "unofficial" documentation, but I'm missing a few key pieces:

a) For a [Vera] Temperature component should it present in degC or degF or in the format specified under Location?  Should this be normalized to always present in one of these, and have the UI "normalize" the value.  The specs here are vague.
b) A standardized way to select between the two collection types?
Should this follow the setting under Location, and if so, what variable is this within the luup.xxx area?  If not, what Service defn can be used to provide the UI/Selector?
c) There is no formal documentation for Google's Weather API, from a trusted *.google.com address, although there's lots of informal stuff floating around.

Offline micasaverde

  • Hero Member
  • *****
  • Posts: 1666
  • Karma: +15/-1
Re: Plugin for Web: weather.com et al
« Reply #12 on: August 16, 2009, 11:45:06 am »
Quote
For a [Vera] Temperature component should it present in degC or degF or in the format specified under Location?

The UPnP standard requires all temperatures to be stored in Celsius.  However, we had to break the standard because the RCS thermostats won't accept temperatures in Celsius or temperatures with decimal places, and because rounding errors were causing temperatures stored in Fahrenheit to be off by 1.

So, in our implementation, if you store a temperature as a number-only (ie 75) it is assumed to be in the same format specified under Location.  Or, you can append a C or F to be explicit (ie 22C or 72F).  This may break some UPnP control points because the official spec is that the temperature is numeric-only.  But, internally within the Luup engine, whenever we see a C or F we do a conversion if necessary so it's handled correctly.

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Plugin for Web: weather.com et al
« Reply #13 on: August 17, 2009, 11:27:43 am »
Ok, given the options, I'd like to make it "follow" the setting in the Location tab, as this seems to do the least amount of damage wrt UPnP spec.

I don't see this spec'd in the Luup extensions Wiki page, so can you let me know the name of the luup.xxxx variable to get this value?  (presumably with domain values of "C" and "F")

Offline zmistro

  • Hero Member
  • *****
  • Posts: 966
  • Karma: +1/-0
Re: Plugin for Web: weather.com et al
« Reply #14 on: August 19, 2009, 04:59:17 pm »
Anyone know why the Off next to the temp and no hunidity?