Author Topic: Luup get all available variables from service name?  (Read 135 times)

Offline smcline06

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Luup get all available variables from service name?
« on: November 18, 2017, 02:37:49 am »
I am banging my head on the wall here learning luup. I am trying to pull all valid variables from the luup.variable_get commands using the service name as an index.

Example of http://<IP>:3480/data_request?id=status&output_format=xml&DeviceNum=10 output for an energy meter:
Code: [Select]
<Device_Num_10 PendingJobs="0" status="-1">
<states>
<state id="1282" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="KWH" value="10230.3740"/>
<state id="1283" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="KWHReading" value="1510990220"/>
<state id="1284" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="Watts" value="1025.165"/>
<state id="1285" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="Log" value="1025,1026,1026,1510990222,23"/>
<state id="1286" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="ActualUsage" value="1"/>
<state id="1287" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="Volts" value="124.219"/>
<state id="1288" service="urn:micasaverde-com:serviceId:EnergyMetering1" variable="Amps" value="8.941"/>
...

What I am looking to get, is a list with the values {"KWH", "KWHReading", "Watts", "Logs", "ActualUsage", "Volts", "Amps"} but cannot seem to locate the correct commands to do this. Using examples I am able to get device attributes, but nothing about service variable names.

Code: [Select]
for k, v in pairs(luup.devices) do
    for k2, v2 in pairs(v) do
        luup.log("Device #" .. k .. ":" .. k2 .. "=" .. tostring(v2))
    end
end

Offline kdschlosser

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-4
Re: Luup get all available variables from service name?
« Reply #1 on: November 21, 2017, 04:57:45 am »
http://<IP>/cgi-bin/cmh/view_upnp_file.sh?file=S_EnergyMetering1.xml.lzo

Offline BobFrankston

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Re: Luup get all available variables from service name?
« Reply #2 on: December 06, 2017, 11:18:49 pm »
Does this generalize? How can I get the variable names for DimmableLight?

Offline kdschlosser

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-4
Re: Luup get all available variables from service name?
« Reply #3 on: December 07, 2017, 02:45:54 am »
ok so here is the skinny the code below will get those variables you want and puts them into a key, value paring. it will only do it for dimmable lights.
This is the first time i have ever coded anything in Lua so be easy on me. LOL

Code: [Select]
local variables = {"KWH", "KWHReading", "Watts", "Logs", "ActualUsage", "Volts", "Amps"}


for lul_device, attr in pairs(luup.devices) do
local testString = string.find( attr.device_type , "urn:schemas-upnp-org:device:DimmableLight:1",1,true) or 0
if testString ~= 0 then
    local readings = {}
    for idx, variable in pairs(variables) do
    local value, tstamp = luup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", variable, lul_device)
readings[idx] = {variable, value}
end
for idx, reading in pairs(readings) do
    luup.log(tostring(reading[1]).. " - ".. tostring(reading[2]))
end
end
end

it prints the output to
Code: [Select]
http://<IP>/cgi-bin/cmh/log.sh?Device=LuaUPnP