Detecting overall Vera status and comment in lua script

Hi,

I am trying to pull the overall Vera controllers status into a set of variables in lua, but am struggling to do so - I wonder if anyone could help.

If I perform a:

http://XXX.XXX.XXX.XXX:3480/data_request?id=sdata&output_format=xml

from my browser, I get the following back (just the first few lines):

[code]

[/code]

I am trying to get my hands on the “state” and “comment” attribute values in lua to I can act on certain values, such as an error - my code at the moment (that is not working) is:

local theState = ( luup.attr_get ('state', 0) or 999) local theComment = ( luup.attr_get ('comment', 0) or "OK" ) if theState == 2 then luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "Reload", {}, 0) end

Research has led me to believe that the root could be accessible through using device level ‘0’ when performing a luup.attr_get

This is not working however, as it always returns “999” and “OK” - so I must be accessing the wrong attributes and/or in the wrong way.

Does anyone have any suggestions of how I should be tweaking this code to make it work?

Thanks in advance .

Cheers

Chris

the luup.attr_get and luup.attr_set commands act on the user_data content NOT the sdata content. The sdata content is generated from the user_data.

The “state” and “comment” attributes do not exist, and are not contained in the user_data… hence your code returns “999” and “OK”.

The only way to access the sdata content from lua is to issue an http.request() for json data, capture the data, and parse the response.

SDATA is just a subset of the user_data information … no way to customize it … it was created because user_data was too big … but it is to restrictive to be of user for any general purpose client. So almost everyone uses the user_data request.

Note: Your post is pretty vague … it sounds like you want to get LUA variables … there is no way to do that without writing/installing a request handler.

[quote=“cybrmage, post:2, topic:195142”]the luup.attr_get and luup.attr_set commands act on the user_data content NOT the sdata content. The sdata content is generated from the user_data.

The “state” and “comment” attributes do not exist, and are not contained in the user_data… hence your code returns “999” and “OK”.

The only way to access the sdata content from lua is to issue an http.request() for json data, capture the data, and parse the response.[/quote]

Thanks cybrmage.

You say that the sdata content is generated from the user_data which means that the “state” and “comment” attributes listed in the sdata content must be in some way derived from one or more user_data attributes by the sdata request handler. Do you know how those are derived, or is that hidden in the mysteries of the code?

Thanks for your help,

Cheers

Chris

The code you’d need is this:

local _,x = luup.inet.wget "http://127.0.0.1:3480/data_request?id=sdata"

local j = json.decode(x)

local comment = j.comment
local state = j.state

…assuming that you have defined json as a bona-fide JSON library.

[quote=“akbooer, post:5, topic:195142”]The code you’d need is this:

local _,x = luup.inet.wget "http://127.0.0.1:3480/data_request?id=sdata"

local j = json.decode(x)

local comment = j.comment
local state = j.state

…assuming that you have defined json as a bona-fide JSON library.[/quote]

Thanks - works a charm!

cheers