Author Topic: Luup using value at http link - Resolved  (Read 343 times)

Offline jbonzey

  • Sr. Newbie
  • *
  • Posts: 43
  • Karma: +0/-1
Luup using value at http link - Resolved
« on: July 24, 2017, 12:16:43 am »
I am trying to get a scene to fire or not based on a value retrieved from a http link.  The value in the link http://auburnweatherlive.com/veraclientraw5.php
is a wind speed value I want to use to close an awning if it goes over 10.0   

Any recommendations on how to do this with LUUP.  All I want to do is have the luup read the http value and if its over 10.0 allow the script to fire otherwise it won't be allowed to fire.

I assume I need to use something with luup.inet.wget("http://auburnweatherlive.com/veraclientraw5.php",5) but lost.

Thanks a lot!!!!

« Last Edit: July 24, 2017, 12:27:40 pm by jbonzey »

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9741
  • Karma: +737/-136
    • RTS Services Plugins
Re: Luup using value at http link
« Reply #1 on: July 24, 2017, 08:59:34 am »
Your better off using the Weather Plugin.  Other wise you have to create something your self to continuously poll the weather condition.

It has a WindSpeed   and a Forecast MaxWindSpeed

Bind these to inputs using PLEG and add your logic to open/close your awning.

Offline jbonzey

  • Sr. Newbie
  • *
  • Posts: 43
  • Karma: +0/-1
Re: Luup using value at http link
« Reply #2 on: July 24, 2017, 09:18:04 am »
Thanks for the response.  I actually went down the wunderground path as my weather station sends data to them.   The problem I'm having is I can only poll wunderground 500 times a day before they terminate the service and if it starts to rain or getting high wind gusts it could be 15 min or more before the scene triggers. 

I also planned to adapt the solution to watch for lightning strikes which I can not do with the plugin.  I can hit my weather station server as many times as I want which is why I created the php script on the website server to provide the data directly on demand.

Any other suggestions would be appreciated and thanks very much for your time.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Luup using value at http link
« Reply #3 on: July 24, 2017, 09:51:23 am »
Well this is relatively straight-forward...

Make a scene scheduled to run every 5 minutes, or however often you want to poll your server, with an action to close the awning.  Add the following code to the scene Lua.  It will abort the scene execution if the speed is less than the threshold.

Code: [Select]

local threshold = 10
local a,b = luup.inet.wget "http://auburnweatherlive.com/veraclientraw5.php"

local status = false        -- by default, abandon scene execution
if a == 0 and b then
 status = tonumber(b: match "%d+%.?%d*") > threshold
end

return status



Edit:  If the code for your server is entirely under your control, then you would probably be better off returning the value (or values, in future) as JSON rather than as HTML.
« Last Edit: July 24, 2017, 11:05:12 am by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline jbonzey

  • Sr. Newbie
  • *
  • Posts: 43
  • Karma: +0/-1
Re: Luup using value at http link - RESOLVED
« Reply #4 on: July 24, 2017, 12:27:24 pm »
Thanks so much akbooer!!!  It works perfectly.

Thanks again
John

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 122
  • Karma: +2/-0
Re: Luup using value at http link
« Reply #5 on: July 25, 2017, 08:54:55 pm »
What is the benefit using JSON Vs. Html ?
Less load on CPU or Memory?
Please give example coding JSON Vs. Html to compare.

Thank you

Well this is relatively straight-forward...

Make a scene scheduled to run every 5 minutes, or however often you want to poll your server, with an action to close the awning.  Add the following code to the scene Lua.  It will abort the scene execution if the speed is less than the threshold.

Code: [Select]

local threshold = 10
local a,b = luup.inet.wget "http://auburnweatherlive.com/veraclientraw5.php"

local status = false        -- by default, abandon scene execution
if a == 0 and b then
 status = tonumber(b: match "%d+%.?%d*") > threshold
end

return status



Edit:  If the code for your server is entirely under your control, then you would probably be better off returning the value (or values, in future) as JSON rather than as HTML.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Luup using value at http link
« Reply #6 on: July 26, 2017, 03:07:22 am »
What is the benefit using JSON Vs. Html ?
Less load on CPU or Memory?
Please give example coding JSON Vs. Html to compare.

It's simply that parsing the data will become much easier if there are more returned values.  The given code works because it's simply doing a string search for a single decimal value.  JSON can easily wrap multiple values of different types with their variable names in complex structures, indeed Vera's user-data returns many thousands of values in this way.

Here's a snippet of JSON data from a Netatmo weather station:

Code: [Select]
    {"Indoor":{
      "deviceId":"70:ee:50:01:55:06",
      "measurements":{
        "AbsolutePressure":1009,
        "CO2":469,
        "DateMaxTemp":1497517502,
        "DateMinTemp":1497501833,
        "DewPoint":15.1,
        "Humidity":58,
        "MaxTemp":24.8,
        "MinTemp":23.2,
        "Noise":36,
        "Pressure":1016.5,
        "PressureTrend":"up",
        "TempTrend":"stable",
        "Temperature":23.9,
        "TimeUtc":1497536788
      }
  }

Supposing that string is in the variable 'j', then the following code:

Code: [Select]
local json = require "dkjson"
local x = json.decode(j)

will result in a Lua table like this:

Code: [Select]
{Indoor = {
    deviceId = "70:ee:50:01:55:06",
    measurements = {
      AbsolutePressure = 1009,
      CO2 = 469,
      DateMaxTemp = 1497517502,
      DateMinTemp = 1497501833,
      DewPoint = 15.1,
      Humidity = 58,
      MaxTemp = 24.8,
      MinTemp = 23.2,
      Noise = 36,
      Pressure = 1016.5,
      PressureTrend = "up",
      TempTrend = "stable",
      Temperature = 23.9,
      TimeUtc = 1497536788
    }
  }}
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.