We have moved at community.getvera.com

Author Topic: need help with parsing JSON data from HTTP and store variables to virtual device  (Read 2425 times)

Offline chris66

  • Sr. Member
  • ****
  • Posts: 318
  • Karma: +9/-8
Ok, for me the error was: (forget the main)

16:42:05: SiteSensor: Eval #3: "response.main.temp - 273.15"=("number")22.25
16:42:05: SiteSensor: Eval #4: "response.main.humidity"=("number")38

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
15:45:23: SiteSensor: Failed to execute `"response.humidity"', { location=18, __source="luaxp", message="Subreference not found: humidity", type="evaluation" }

The expression should be response.main.humidity, because humidity lives underneath the main subkey in the response.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline chris66

  • Sr. Member
  • ****
  • Posts: 318
  • Karma: +9/-8
More complicated, because sometimes datas are missing, like for the wind direction (deg):
10:57:00: SiteSensor: Failed to execute `"response.wind.deg"', { location=18, __source="luaxp", message="Subreference not found: deg", type="evaluation" }

{"coord":{"lon":7.36,"lat":46.23},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":290.14,"pressure":1018,"humidity":50,"temp_min":277.15,"temp_max":298.15},"visibility":10000,"wind":{"speed":0.5},"clouds":{"all":20},"dt":1529569200,"sys":{"type":1,"id":6005,"message":0.0028,"country":"CH","sunrise":1529552328,"sunset":1529609161},"id":2658576,"name":"Sion","cod":200}

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
OK. For this one, you'll need a minor update. Download the attached Lua file and upload it to your Vera through the Apps > Develop apps > Luup files uploader. This update allows references to non-existent subkeys to put null into the result stack rather than immediately throwing an error.

Once that's done, you can use the if() function like this: if( response.wind==null, "no wind data", response.wind.deg )

The if() function takes three arguments: a condition expression (boolean), an expression to evaluate when the condition is true, and an expression to evaluate when it's false (it's modeled after the Excel function of the same name). In this case, if OpenWeatherMap does not provide wind data, response.wind will evaluate to null, so the "no wind data" string will be the expression result. If it is provided (i.e. response.wind is not null) then the expression returns the value of response.wind.deg.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline chris66

  • Sr. Member
  • ****
  • Posts: 318
  • Karma: +9/-8
@rigpapa, thanks very much, never seen so good support! Will try this after Holidays, a nice day/holidays?  8) Chris

Offline cranb

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Hello,

I have set up the sitesensor plugin to parse the data from an openweathermap URL. Works great. As i am looking for a replacement for the Wunderground weather plugin, would it be possible to put the data from the sitesensor into a device? I am looking for a setup similar to the Wunderground plugin which consist of a number of devices (temp, hum) that get their data from the plugin. See attached screenshot.



Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
Hello,

I have set up the sitesensor plugin to parse the data from an openweathermap URL. Works great. As i am looking for a replacement for the Wunderground weather plugin, would it be possible to put the data from the sitesensor into a device? I am looking for a setup similar to the Wunderground plugin which consist of a number of devices (temp, hum) that get their data from the plugin. See attached screenshot.

The data that your 8 expressions produce is stored in your SiteSensor sensor, and can be retrieved into Lua, Reactor, PLEG, etc. Use service urn:toggledbits-com:serviceId:SiteSensor1, and the variable names are Value1 through Value8.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline cranb

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Thanks for your quick answer! I am able to get the values in Pleg and use them in for example the multistring plugin. But what i really want is to create a separate device to display that value and that is where my knowledge is not sufficient at the moment  :(  Could you provide some explanation / steps on how to achieve this?

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
Thanks for your quick answer! I am able to get the values in Pleg and use them in for example the multistring plugin. But what i really want is to create a separate device to display that value and that is where my knowledge is not sufficient at the moment  :(  Could you provide some explanation / steps on how to achieve this?

If you just want to display one of the values (and given that it's weather data, I'm guessing that's not the case... you want temp, humidity, etc.), you can put an expression into the "MessageExpr" state variable in your SiteSensor, and it will display that expression result on its dashboard card. Since the data is probably already one of the existing 8 expressions, you can set MessageExpr to expr[n] where n is 1 to 8 and it will show the value from that expression.

If you need to display the result of more than one expression result in as many devices, I don't have a built-in solution for you at the moment. I can add the ability to create virtual devices of specific types to the future feature list. In the meanwhile, if it's only about display, I'm thinking rstrouse's excellent Virtual Panel plugin may be a solution for you: http://forum.micasaverde.com/index.php/topic,31128.0.html
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline cranb

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Indeed i was thinking of a structure in where site-sensor is the mother and the child-devices are temperature, humidity, etc. A bit like the current WU-ground plugin where the child devices get their data from the plugin.

I am going to have a further look at the panel plugin. Already have it installed to control my IR TV through broadlink but did not think of it in this case.

Thanks for the tips and the work on your great plugins!!

Offline phoenixaus

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Hi,

Sorry for a Newbie question, but I am trying to achieve exactly what the subject of this thread is!!

I'm using SiteSensor to retrieve a temperature reading from my local Aircon system. I have configured the device correctly, as I am getting the measured Temp reading coming through correctly.

However, I'm trying to push the value via a scene running to a virtual Temp Sensor, but I can't seem to get it to work.

Any ideas?


Code: [Select]
local bedtemp=luup.variable_get("urn:toggledbits-com:serviceId:SiteSensor1","Value1",78)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", bedtemp,85)
return true

78 is the device id for the sitesensor
85 is the device id for the temp sensor.

85 has a variable called CurrentTemperature , so I thought this would work. When I run the scene it says success, but then Vera gives me the error in Lua scenes.
Any hints and tips greatly appreciated!
Steve
« Last Edit: January 22, 2019, 08:22:51 am by phoenixaus »

Offline cranb

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Hi,

Do you know the Virtual Sensor plugin from rigpapa? The last update of the plugin allows you to create a virtual sensor that can get data from another source.

I use it in combination with Sitesensor to get data from openweathermap into a temp and humidity sensor. Works like a charm!

Offline questusa

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Please help with my code:

I need to access local device with web interface by using curl. It returns json file, then I need to parse it and get value.

local json = require "dkjson"
local j = [[os.execute(curl -d 'api_key=b05bad1826f1a1910935b777918eec702be395d5&command=get_water_temperature' -X GET http://192.168.1.104/Api ]]
local data = json.decode(j)
local my_temp = ["msg"]
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",my_temp,290)
return true

Offline Catman

  • Sr. Member
  • ****
  • Posts: 338
  • Karma: +10/-0
Can you run that curl statement direct from the command line?

I'd get it working there first if not, or is that not the problem?

C

Offline icare

  • Jr. Member
  • **
  • Posts: 86
  • Karma: +0/-0
The description of the SiteSensor plugin suggest "Virtual sensors are defined by simply choosing the sensor type from the dropdown menu next to a SiteSensor expression". I'm running version 1.9 from the Vera AppStore but can't locate any dropdown menu as suggested. What am I missing.