Virtual device with energy metering and xml parsing ?

Hi !

I know how to create a virtual device, but i search more “difficult”. I have a solar installation that products energy. I can have production information in a xml files without problem.

What i want is that Vera read the xml file, and put informations about watts and Kwh (total production) in a virtual device.

Do you have an idea how to make this ? I use differents plugins from the forum, but i don’t know program in lua :confused:

Create a virtual device with a variable named [tt]Watts[/tt] and use something like

http://<Vera's IP address>:3480/data_request?id=variableset&DeviceNum=<ID of your virtual device>&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=Watts&Value=<current value>

to set the variable (assuming that you can extract the value from the XML data).

Thanks, it works great !

I create a virtual device with D_PowerMeter1.xml, and i use this request, it’s ok ! I can also see the production in the energy monitoring :smiley:

In the wiki i see different variables, what is difference between Watts, ActualUsage, UserSuppliedWattage and WholeHouse ?
Because i create another virtual device for my consumption (total, with a special product conect to the teleinfo in France)

Watts is the instant usage, which is either read from the device, or, if the device doesn’t report its energy consumption, is the UserSuppliedWattage. UserSuppliedWattage can be set in a device’s Settings tab, Energy used input field.

AFAIK the ActualUsage and WholeHouse variables are not used.

ok, thanks !

Last question (i hope :p): on the device, i have Watts, with value that i put, and KwH, that is null. What make Kwh normaly ? Total, or is the same value ?

I think that only certain devices can report the kWh.

Thanks for your help !

hi,

This solution works great for some informations.

Now, i have another information to get, and i can’t use this solution :confused: So, i copy some functions of the weather plugin.

I have a xm file as it:

<response> <led0>1</led0> <led1>0</led1> <led2>0</led2> <led3>0</led3> <led4>0</led4> <led5>0</led5> <led6>0</led6> <led7>0</led7> <btn0>up</btn0> <btn1>up</btn1> <btn2>up</btn2> <btn3>up</btn3> <btn4>up</btn4> <btn5>up</btn5> <btn6>up</btn6> <btn7>up</btn7> <an1>0</an1> <an2>0</an2> <an3>0</an3> <an4>0</an4> <day>2</day>

I try this script:

[code]function extractAttribute(stuff)
local result = {}

    for attrName, attrValue in (string.gmatch(stuff, "(%w+)=\"(.-)\"")) do
        result[attrName] = attrValue
    end

    return result
end


function extractElement(stuff, element)
   
    for attrString, data in string.gmatch(stuff, "&lt;" .. element .. "(.*)&gt;(.*)&lt;/" .. element .. "&gt;") do
        return { value = data, attrs = extractAttribute(attrString) }
    end

    for attrString in string.gmatch(stuff, "&lt;" ..  element .. " (.-)/&gt;") do
        return { value = nil, attrs = extractAttribute(attrString) }
    end

    return {};
end

local url = “http://192.168.2.11/status.xml
local status, rss = luup.inet.wget(url, 10)
local currentTemp = extractElement(rss, “led0”)[“attrs”][“data”][/code]

i don’t have error, but i don’t have value :stuck_out_tongue:
I don’t understand very well how works “for attrString, data in string.gmatch”
If anybody can help me ?

In short, its being used to “find” [the first] element of the form:

<[i]element[/i]*>*</[i]element[/i]>

ie. an XML Element tag for “element”. If you pass in a value like “btn0”, then it will go looking for the string:

[tt]<btn0*>*</btn0>[/tt]

So if you have a value like [tt]up[/tt] it should return “[tt]up[/tt]”

I probably need to re-write it now that I know more Lua, but it does the job.

Note that it returns an array (“table” in Lua) with two named values:
a) “[tt]value[/tt]” - the value of the Element (eg. “[tt]up[/tt]”)
b) “[tt]attrs[/tt]” - the value of any attrs present in the element (or nil in your case)

so I suspect you have a problem with the extraction into the [tt]currentTemp[/tt] var…

Of course, I haven’t tested it with your data, but that’s roughly how the original code works…

After this, i try to put data in a device:

luup.variable_set(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, currentTemp, 150)

If you want to test, the external url for the xml file is http://web.locqueneux.com:82/status.xml (i’ll close access when it was ok).
With the value * i must have 0 or 1. I’ll use it to control a device, but for the moment i put it in device to see it quickly.

Another example of XML parsing in Lua can be seen in the CurrentCost EnviR plugin (L_CurrentCostEnviR1.lua in trunk – CurrentCost EnviR Energy Monitor).

@cedriclocqueneux,
Did you get it working?

Yes, my code is now working well.

Problem was with “<” that i must replace by “<”.

The full code is:

[code]local function extractElement(tag, xml, default)
local pattern = “<”…tag…“>(.*)</”…tag…“>”
local result = (xml:match(pattern) or default)
return result
end

local url = “http://192.168.2.11/status.xml
local status, rss = luup.inet.wget(url, 10)

if (status == 0) then
local etatrelai0 = extractElement(“led0”, rss, “N/A”)
luup.variable_set(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, etatrelai0, 94)
end[/code]

With this i read the value of “led0”, and i adjust status of my virtual device.

hello

I was trying to trace the ip of my card info
for states 0 and 1 is good
by cons how to make up the state is 0 if dn?
and
up gives 1

I tried many things but nothing works

thank you

edit :

The application allows ipx800 tcp on port 9470
it supports CONTROLS bit set and a relay to switch on or off and can also get commands to read the state of inputs and relays GetOut or getin for outputs or inputs
I saw on the post that Vera could read I’d like to read tcp states and assigned to a virtual module
taking up a code on the forum I saw it but what is the line:
“c: send (string.char (0,0,0,0,0,6,1,4,0,0,0,8))” what should I put this in place of string.char

local socket = require(“socket”)
host = “192.168.1.55”
c = assert(socket.connect(host, 9470))
c:send(string.char(0,0,0,0,0,6,1,4,0,0,0,8))
data = c:receive(25)
luup.log (data)
c:close()

php the script is

<?php $fp = fsockopen($IP, $port, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)
\n"; } else { $out = "GetOut1\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>

but in lua ?*thanks David

nobody ? ??? :-[ :cry: :-\

I try to move forward on my problem

I get good value for my condition relayboard in your virtual container with this (I will fetch the values ​​are up and dn)

local PortailDelph = extractElement("btn2", rss, "N/A") luup.variable_set("urn:upnp-org:serviceId:VContainer1","VariableName1","Portail Del",54) luup.variable_set("urn:upnp-org:serviceId:VContainer1","Variable1", PortailDelph, 54)

by cons I can not seem to update the state of the virtual switch as it wants of 0 and 1 for its status

if PortailDelph == dn then luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", 1, 44) else luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status", 0, 44) end

Do you have an idea to be able to update the status of the virtual switch?

thanks
DAvid

[tt]http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Turn_an_appliance_switch_or_a_Danfoss_thermostat_on_for_device_.235[/tt] ?

And don’t understand

my virtual swith have a variable status and i want to switch on if my variable = up or 0 if =dn
i have test but no result

if (PortailDelph == "dn") then luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 44) else luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 44) end

ok i have a solution thanks

the correct is not switchpower but

luup.call_action(“urn:upnp-org:serviceId:VSwitch1”, “SetTarget”, {newTargetValue = “1”}, 44)