We have moved at community.getvera.com

Author Topic: openWeather plugin for openLuup  (Read 9696 times)

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
openWeather plugin for openLuup
« on: March 09, 2016, 12:01:10 pm »
As I was moving as many plugins as possible from my Vera to openLuup on a Raspberry Pi, I stumbled across the fact that the Weather plugin I had obtained from the MiOS app store would not run under openLuup as it is encrypted by his author. Since I need the functionality, I wrote from scratch a basic plugin for my own purposes that reads a few data from the Weather Underground website. Far less functionality than the original plugin that only runs on Vera, but it can be expanded if there is interest.

UPDATE January 31, 2017:

Version 1.6 is now available on the "App Store" from the AltUI interface.

Documentation is at https://raw.githubusercontent.com/999LV/openWeather-plugin-for-openLuup/master/documentation/openWeather%20documentation.pdf

Changelog:
Version 1.0 2016-07-15 - first production version, installation via the AltUI/openLuup App Store
Version 1.1 2016-08-24 - major rewrite for cleaner and faster code, but no new functionality
Version 1.2 2016-09-22 - added language parameter to fetch the WU data in another language than English (@korttoma suggestion) and added today and tomorrow forecast data (high/low temps, conditions and text forecast)
Version 1.3 2016-11-13 - added WU display and observation locations (@jswim788 suggestion)
Version 1.4 2016-11-14 - added 'AllowEstimated' parameter variable, to ignore estimated data produced by WU when a weather station becomes somehow unavailable
Version 1.5 2017-01-19 - update suggested by @amg0 to allow for two icons to appear in the device in AltUI: Not only today's weather condition, but also tomorrow's. This change will be effective with a new release of AltUI by @amg0 to enable the display of the additional icon.
Version 1.6 2017-01-30 - amg0 update for 'WindSpeed' variable

If anyone is interested, the plugin files and installation instructions are available at https://github.com/999LV/openWeather-plugin-for-openLuup.git

« Last Edit: January 31, 2017, 04:35:43 am by logread »
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #1 on: March 10, 2016, 05:05:08 am »
I took a quick look and was surprised to see this line  in the source code:
Code: [Select]
if this_device ~= nil then -- quick & dirty solution to the fact that init gets called twice upon luup startup
...since this is not a known issue. 

Then I looked further and saw that this is no surprise at all, since you explicitly call it twice: once on line 161
Code: [Select]
init()

...and once from the implementation file:
Code: [Select]
<startup>init</startup>

I also see that you create the 'child' devices as independent devices, rather than using the luup.chdev modules (which, admittedly, are a bit more cumbersome.)  This is fine, and exactly the way I would do it if running the Lua code as part of startup rather than a bona fide device.  What it means is that the child devices will not get deleted if the parent device is deleted.

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
Re: openWeather plugin for openLuup
« Reply #2 on: March 10, 2016, 06:43:32 am »
Many thanks Akbooer for taking the time to check my code. Much appreciated.

I now see the issue with the call to init() on line 161... Legacy from an early version that I had ran as a module for testing... and that I forgot to remove once the code was moved to a plugin version. Appreciate the pointer and will correct.

Regarding the creation of the child devices, I actually tried to use the luup.chdev module but I could not get it to work (the parent device implementation code stopped responding after the call to chdev.sync). I then decided to stop banging my head on the wall and just use luup.create_device despite the shortcoming that you describe. I may relook at this in a future version.
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #3 on: March 10, 2016, 07:02:27 am »
chdev.sync will cause a reload if there are any changes in created child devices (eg. the first time you create them.)  This would certainly cause the handler to stop responding until openLuup.init() is up and running again.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
Re: openWeather plugin for openLuup
« Reply #4 on: March 10, 2016, 11:55:26 am »
I addressed the startup and child devices comments from akbooer. Many thanks to him.

version 0.10 now on GitHub
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #5 on: March 10, 2016, 12:10:05 pm »
Did you not remember to sync the repository?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
Re: openWeather plugin for openLuup
« Reply #6 on: March 10, 2016, 12:31:11 pm »
You were faster than me...  ;D
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #7 on: July 23, 2016, 04:54:03 pm »
Great to see this is now in the Alt App Store... downloaded to openLuup with one click of the download button... Plugged in my location and key and away we go.   Thanks!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
Re: openWeather plugin for openLuup
« Reply #8 on: July 23, 2016, 05:07:15 pm »
Thanks Akbooer !
Not happy with the icon not rendering in the store though. Pointers welcome 🙂
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #9 on: July 23, 2016, 05:21:02 pm »
Not happy with the icon not rendering in the store though. Pointers welcome 🙂

Fixed! You should have been using the "raw" file reference:

Code: [Select]
https://raw.githubusercontent.com/999LV/openWeather-plugin-for-openLuup/master/openWeather.gif
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: openWeather plugin for openLuup
« Reply #10 on: July 23, 2016, 06:05:52 pm »
@logread

You might be interested in this code.  If you see any benefit, feel free to use it:

http://forum.micasaverde.com/index.php/topic,15566.msg119956.html#msg119956

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #11 on: July 23, 2016, 06:17:22 pm »
Or, equally, just use the JSON syntax call ?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: openWeather plugin for openLuup
« Reply #12 on: July 23, 2016, 06:47:28 pm »
Yes that would probably be cleaner.

Offline logread

  • Full Member
  • ***
  • Posts: 214
  • Karma: +7/-1
Re: openWeather plugin for openLuup
« Reply #13 on: July 24, 2016, 02:00:23 am »
Thanks to both @akbooer and @a-lurker ! Will take a look at the code (btw, I did not realize all the history about weather plugins)
Vera Lite UI7, Fibaro FGS-221, FGS-212, FGSS-001, FGK-101, FGWPE/F-101, FGMS-001, Aeon HEM G2, GreenWave PowerNode 6,  Everspring ST-814, SE-812, Swiid SwiidInter.
Raspberry Pi2 Raspbian w/ openLuup. AltUI, SV Thermostat, Virtual Switch, Weather (openWeather), System Monitor (openSysMon), HomeWave.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openWeather plugin for openLuup
« Reply #14 on: July 24, 2016, 06:11:24 am »
Or, equally, just use the JSON syntax call ?

This certainly works in openLuup:

Code: [Select]
local json = require "json"
local key  = "XXXXXXXXXX"
local req_fmt = "https://api.wunderground.com/api/%s/conditions/forecast/q/%0.6f,%0.6f.json"

local url = req_fmt: format (key, luup.latitude, luup.longitude)
local status, result = luup.inet.wget (url)
local info = json.decode (result)

print (pretty(info))
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.